From 8599adec15d173c7c3fcc0d6bff38c7fc53f98a0 Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Mon, 4 Apr 2022 17:14:19 +0200 Subject: [PATCH] [8.x] Fix assertListening check with auto discovery (#41820) * Fix assertListening check with auto discovery * wip --- src/Illuminate/Support/Testing/Fakes/EventFake.php | 5 +++++ tests/Integration/Events/EventFakeTest.php | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/Illuminate/Support/Testing/Fakes/EventFake.php b/src/Illuminate/Support/Testing/Fakes/EventFake.php index ed5014f15519..436173e9d3ae 100644 --- a/src/Illuminate/Support/Testing/Fakes/EventFake.php +++ b/src/Illuminate/Support/Testing/Fakes/EventFake.php @@ -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; @@ -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)) { diff --git a/tests/Integration/Events/EventFakeTest.php b/tests/Integration/Events/EventFakeTest.php index e6b7ab33c308..5964f1b1e604 100644 --- a/tests/Integration/Events/EventFakeTest.php +++ b/tests/Integration/Events/EventFakeTest.php @@ -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) { @@ -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); @@ -165,6 +167,14 @@ public function subscribe($events) } } +class PostAutoEventSubscriber +{ + public function handle($event) + { + // + } +} + class PostObserver { public function saving(Post $post)