From d7152ea74e142c885f5e599eb4eef1701bc00ce8 Mon Sep 17 00:00:00 2001 From: Pandi Selvam Date: Wed, 5 Mar 2025 18:38:29 +0530 Subject: [PATCH] Fix: EventFake does not match Event:listen --- .../Support/Testing/Fakes/EventFake.php | 23 ++++++++----------- tests/Integration/Events/EventFakeTest.php | 2 ++ 2 files changed, 12 insertions(+), 13 deletions(-) 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()