Skip to content

Commit

Permalink
[9.x] Ensures view creators and composers are called when * is pres…
Browse files Browse the repository at this point in the history
…ent (#44636)

* Ensures view creators and composers are called when `*` is present

* Improves fix for nested wildcards

* Adds tests on nested wildcards

* Apply fixes from StyleCI

* More tests

Co-authored-by: StyleCI Bot <[email protected]>
  • Loading branch information
nunomaduro and StyleCIBot authored Oct 18, 2022
1 parent 09f1609 commit c57cacd
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/Illuminate/View/Concerns/ManagesEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ trait ManagesEvents
public function creator($views, $callback)
{
if (is_array($this->shouldCallCreators)) {
if ($views == '*') {
$this->shouldCallCreators = true;
} else {
foreach (Arr::wrap($views) as $view) {
$this->shouldCallCreators[$this->normalizeName($view)] = true;
foreach (Arr::wrap($views) as $view) {
if (str_contains($view, '*')) {
$this->shouldCallCreators = true;

break;
}

$this->shouldCallCreators[$this->normalizeName($view)] = true;
}
}

Expand Down Expand Up @@ -78,12 +80,14 @@ public function composers(array $composers)
public function composer($views, $callback)
{
if (is_array($this->shouldCallComposers)) {
if ($views == '*') {
$this->shouldCallComposers = true;
} else {
foreach (Arr::wrap($views) as $view) {
$this->shouldCallComposers[$this->normalizeName($view)] = true;
foreach (Arr::wrap($views) as $view) {
if (str_contains($view, '*')) {
$this->shouldCallComposers = true;

break;
}

$this->shouldCallComposers[$this->normalizeName($view)] = true;
}
}

Expand Down
115 changes: 115 additions & 0 deletions tests/View/ViewFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,53 @@ public function testCallCreatorsDoesDispatchEventsWhenIsNecessary()
$factory->callCreator($view);
}

public function testCallCreatorsDoesDispatchEventsWhenIsNecessaryUsingNamespacedWildcards()
{
$factory = $this->getFactory();
$factory->getDispatcher()
->shouldReceive('listen')
->with('creating: namespaced::*', m::type(Closure::class))
->once();

$factory->getDispatcher()
->shouldReceive('dispatch')
->with('creating: namespaced::my-package-view', m::type('array'))
->once();

$view = m::mock(View::class);
$view->shouldReceive('name')->once()->andReturn('namespaced::my-package-view');

$factory->creator('namespaced::*', fn () => true);

$factory->callCreator($view);
}

public function testCallCreatorsDoesDispatchEventsWhenIsNecessaryUsingNamespacedNestedWildcards()
{
$factory = $this->getFactory();
$factory->getDispatcher()
->shouldReceive('listen')
->with('creating: namespaced::*', m::type(Closure::class))
->once();

$factory->getDispatcher()
->shouldReceive('listen')
->with('creating: welcome', m::type(Closure::class))
->once();

$factory->getDispatcher()
->shouldReceive('dispatch')
->with('creating: namespaced::my-package-view', m::type('array'))
->once();

$view = m::mock(View::class);
$view->shouldReceive('name')->once()->andReturn('namespaced::my-package-view');

$factory->creator(['namespaced::*', 'welcome'], fn () => true);

$factory->callCreator($view);
}

public function testCallCreatorsDoesDispatchEventsWhenIsNecessaryUsingWildcards()
{
$factory = $this->getFactory();
Expand Down Expand Up @@ -290,6 +337,74 @@ public function testCallComposerDoesDispatchEventsWhenIsNecessary()
$factory->callComposer($view);
}

public function testCallComposerDoesDispatchEventsWhenIsNecessaryAndUsingTheArrayFormat()
{
$factory = $this->getFactory();
$factory->getDispatcher()
->shouldReceive('listen')
->with('composing: name', m::type(Closure::class))
->once();

$factory->getDispatcher()
->shouldReceive('dispatch')
->with('composing: name', m::type('array'))
->once();

$view = m::mock(View::class);
$view->shouldReceive('name')->twice()->andReturn('name');

$factory->composer(['name'], fn () => true);

$factory->callComposer($view);
}

public function testCallComposersDoesDispatchEventsWhenIsNecessaryUsingNamespacedWildcards()
{
$factory = $this->getFactory();
$factory->getDispatcher()
->shouldReceive('listen')
->with('composing: namespaced::*', m::type(Closure::class))
->once();

$factory->getDispatcher()
->shouldReceive('dispatch')
->with('composing: namespaced::my-package-view', m::type('array'))
->once();

$view = m::mock(View::class);
$view->shouldReceive('name')->once()->andReturn('namespaced::my-package-view');

$factory->composer('namespaced::*', fn () => true);

$factory->callComposer($view);
}

public function testCallComposersDoesDispatchEventsWhenIsNecessaryUsingNamespacedNestedWildcards()
{
$factory = $this->getFactory();
$factory->getDispatcher()
->shouldReceive('listen')
->with('composing: namespaced::*', m::type(Closure::class))
->once();

$factory->getDispatcher()
->shouldReceive('listen')
->with('composing: welcome', m::type(Closure::class))
->once();

$factory->getDispatcher()
->shouldReceive('dispatch')
->with('composing: namespaced::my-package-view', m::type('array'))
->once();

$view = m::mock(View::class);
$view->shouldReceive('name')->once()->andReturn('namespaced::my-package-view');

$factory->composer(['namespaced::*', 'welcome'], fn () => true);

$factory->callComposer($view);
}

public function testCallComposersDoesDispatchEventsWhenIsNecessaryUsingWildcards()
{
$factory = $this->getFactory();
Expand Down

0 comments on commit c57cacd

Please sign in to comment.