Skip to content

[11.x] Fix normalization in EventFake::assertListening #54951

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: 11.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 20 additions & 19 deletions src/Illuminate/Support/Testing/Fakes/EventFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand All @@ -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.
*
Expand Down
6 changes: 5 additions & 1 deletion tests/Integration/Events/EventFakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ public function testAssertListening()
'Illuminate\\Tests\\Integration\\Events\\PostAutoEventSubscriber@handle',
PostEventSubscriber::class,
[PostEventSubscriber::class, 'foo'],
PostEventSubscriber::class.'@bar',
InvokableEventSubscriber::class,
]);

Expand All @@ -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);
Expand Down
Loading