diff --git a/src/Illuminate/Support/Testing/Fakes/EventFake.php b/src/Illuminate/Support/Testing/Fakes/EventFake.php index 7f226a786faf..0b71a9725f6a 100644 --- a/src/Illuminate/Support/Testing/Fakes/EventFake.php +++ b/src/Illuminate/Support/Testing/Fakes/EventFake.php @@ -85,30 +85,27 @@ public function except($eventsToDispatch) */ public function assertListening($expectedEvent, $expectedListener) { + $normalizedListener = $expectedListener; + + if (is_string($normalizedListener) && Str::contains($normalizedListener, '@')) { + $normalizedListener = Str::parseCallback($normalizedListener); + } + foreach ($this->dispatcher->getListeners($expectedEvent) as $listenerClosure) { $actualListener = (new ReflectionFunction($listenerClosure)) ->getStaticVariables()['listener']; - $normalizedListener = $expectedListener; - if (is_string($actualListener) && Str::contains($actualListener, '@')) { $actualListener = Str::parseCallback($actualListener); + } - if (is_string($expectedListener)) { - if (Str::contains($expectedListener, '@')) { - $normalizedListener = Str::parseCallback($expectedListener); - } else { - $normalizedListener = [ - $expectedListener, - method_exists($expectedListener, 'handle') ? 'handle' : '__invoke', - ]; - } - } + if (is_string($normalizedListener) && is_array($actualListener)) { + $actualListener = $actualListener[0]; } if ($actualListener === $normalizedListener || ($actualListener instanceof Closure && - $normalizedListener === Closure::class)) { + $normalizedListener === Closure::class)) { PHPUnit::assertTrue(true); return; diff --git a/tests/Integration/Events/EventFakeTest.php b/tests/Integration/Events/EventFakeTest.php index 1f8eaa3999e6..d57121ffff15 100644 --- a/tests/Integration/Events/EventFakeTest.php +++ b/tests/Integration/Events/EventFakeTest.php @@ -177,6 +177,8 @@ public function testAssertListening() Event::assertListening('eloquent.saving: '.Post::class, PostObserver::class.'@saving'); Event::assertListening('eloquent.saving: '.Post::class, [PostObserver::class, 'saving']); Event::assertListening('event', InvokableEventSubscriber::class); + Event::assertListening('post-created', PostEventSubscriber::class.'@handlePostCreated'); + Event::assertListening('post-deleted', PostEventSubscriber::class.'@handlePostDeleted'); } public function testMissingMethodsAreForwarded()