diff --git a/src/Illuminate/Support/Testing/Fakes/EventFake.php b/src/Illuminate/Support/Testing/Fakes/EventFake.php index 7f226a786faf..2d0bc62f70e1 100644 --- a/src/Illuminate/Support/Testing/Fakes/EventFake.php +++ b/src/Illuminate/Support/Testing/Fakes/EventFake.php @@ -85,26 +85,11 @@ public function except($eventsToDispatch) */ public function assertListening($expectedEvent, $expectedListener) { + $normalizedListener = $this->parseListener($expectedListener); + 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', - ]; - } - } - } + $actualListener = $this->parseListener((new ReflectionFunction($listenerClosure)) + ->getStaticVariables()['listener']); if ($actualListener === $normalizedListener || ($actualListener instanceof Closure && @@ -125,6 +110,22 @@ public function assertListening($expectedEvent, $expectedListener) ); } + protected function parseListener($listener) + { + if (is_string($listener)) { + if (Str::contains($listener, '@')) { + $listener = Str::parseCallback($listener); + } elseif ($listener !== Closure::class) { + $listener = [ + $listener, + method_exists($listener, 'handle') ? 'handle' : '__invoke', + ]; + } + } + + return $listener; + } + /** * Assert if an event was dispatched based on a truth-test callback. * diff --git a/tests/Integration/Events/EventFakeTest.php b/tests/Integration/Events/EventFakeTest.php index 1f8eaa3999e6..11aefe2f7ae5 100644 --- a/tests/Integration/Events/EventFakeTest.php +++ b/tests/Integration/Events/EventFakeTest.php @@ -150,6 +150,7 @@ public function testAssertListening() 'Illuminate\\Tests\\Integration\\Events\\PostAutoEventSubscriber@handle', PostEventSubscriber::class, [PostEventSubscriber::class, 'foo'], + PostEventSubscriber::class.'@bar', InvokableEventSubscriber::class, ]); @@ -168,9 +169,12 @@ public function testAssertListening() (new Post)->save(); Event::assertListening('event', 'listener'); - Event::assertListening('event', PostEventSubscriber::class); Event::assertListening('event', PostAutoEventSubscriber::class); + Event::assertListening('event', PostEventSubscriber::class); Event::assertListening('event', [PostEventSubscriber::class, 'foo']); + Event::assertListening('event', PostEventSubscriber::class.'@foo'); + Event::assertListening('event', [PostEventSubscriber::class, 'bar']); + Event::assertListening('event', PostEventSubscriber::class.'@bar'); Event::assertListening('post-created', [PostEventSubscriber::class, 'handlePostCreated']); Event::assertListening('post-deleted', [PostEventSubscriber::class, 'handlePostDeleted']); Event::assertListening(NonImportantEvent::class, Closure::class);