Skip to content

Commit

Permalink
[8.x] Fix assertListening check with auto discovery (#41820)
Browse files Browse the repository at this point in the history
* Fix assertListening check with auto discovery

* wip
  • Loading branch information
driesvints committed Apr 4, 2022
1 parent 8ca1471 commit 8599ade
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Illuminate/Support/Testing/Fakes/EventFake.php
Expand Up @@ -5,6 +5,7 @@
use Closure;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\ReflectsClosures;
use PHPUnit\Framework\Assert as PHPUnit;
use ReflectionFunction;
Expand Down Expand Up @@ -61,6 +62,10 @@ public function assertListening($expectedEvent, $expectedListener)
$actualListener = (new ReflectionFunction($listenerClosure))
->getStaticVariables()['listener'];

if (is_string($actualListener) && Str::endsWith($actualListener, '@handle')) {
$actualListener = Str::parseCallback($actualListener)[0];
}

if ($actualListener === $expectedListener ||
($actualListener instanceof Closure &&
$expectedListener === Closure::class)) {
Expand Down
10 changes: 10 additions & 0 deletions tests/Integration/Events/EventFakeTest.php
Expand Up @@ -126,6 +126,7 @@ public function testAssertListening()
Event::fake();
Event::listen('event', 'listener');
Event::listen('event', PostEventSubscriber::class);
Event::listen('event', 'Illuminate\\Tests\\Integration\\Events\\PostAutoEventSubscriber@handle');
Event::listen('event', [PostEventSubscriber::class, 'foo']);
Event::subscribe(PostEventSubscriber::class);
Event::listen(function (NonImportantEvent $event) {
Expand All @@ -134,6 +135,7 @@ public function testAssertListening()

Event::assertListening('event', 'listener');
Event::assertListening('event', PostEventSubscriber::class);
Event::assertListening('event', PostAutoEventSubscriber::class);
Event::assertListening('event', [PostEventSubscriber::class, 'foo']);
Event::assertListening('post-created', [PostEventSubscriber::class, 'handlePostCreated']);
Event::assertListening(NonImportantEvent::class, Closure::class);
Expand Down Expand Up @@ -165,6 +167,14 @@ public function subscribe($events)
}
}

class PostAutoEventSubscriber
{
public function handle($event)
{
//
}
}

class PostObserver
{
public function saving(Post $post)
Expand Down

0 comments on commit 8599ade

Please sign in to comment.