Skip to content

Commit

Permalink
allows Event::assertListening to check for invokable event listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmastech committed Apr 4, 2023
1 parent d68fefe commit f329ee2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Illuminate/Support/Testing/Fakes/EventFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ public function assertListening($expectedEvent, $expectedListener)
if (Str::contains($expectedListener, '@')) {
$normalizedListener = Str::parseCallback($expectedListener);
} else {
$normalizedListener = [$expectedListener, 'handle'];
$normalizedListener = [
$expectedListener,
! method_exists($expectedListener, 'handle') ? '__invoke' : 'handle',
];
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions tests/Integration/Events/EventFakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ public function testAssertListening()
'Illuminate\\Tests\\Integration\\Events\\PostAutoEventSubscriber@handle',
PostEventSubscriber::class,
[PostEventSubscriber::class, 'foo'],
InvokableEventSubscriber::class,
]);

foreach ($listenersOfSameEventInRandomOrder as $listener) {
Expand All @@ -172,6 +173,7 @@ public function testAssertListening()
Event::assertListening(NonImportantEvent::class, Closure::class);
Event::assertListening('eloquent.saving: '.Post::class, PostObserver::class.'@saving');
Event::assertListening('eloquent.saving: '.Post::class, [PostObserver::class, 'saving']);
Event::assertListening('event', InvokableEventSubscriber::class);
}
}

Expand Down Expand Up @@ -231,3 +233,11 @@ public function saving(Post $post)
$post->slug = sprintf('%s-Test', $post->title);
}
}

class InvokableEventSubscriber
{
public function __invoke($event)
{
//
}
}

0 comments on commit f329ee2

Please sign in to comment.