Skip to content

Commit fca8835

Browse files
authored
allows Event::assertListening to check for invokable event listeners (#46683)
1 parent c0d09c5 commit fca8835

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/Illuminate/Support/Testing/Fakes/EventFake.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ public function assertListening($expectedEvent, $expectedListener)
9494
if (Str::contains($expectedListener, '@')) {
9595
$normalizedListener = Str::parseCallback($expectedListener);
9696
} else {
97-
$normalizedListener = [$expectedListener, 'handle'];
97+
$normalizedListener = [
98+
$expectedListener,
99+
method_exists($expectedListener, 'handle') ? 'handle' : '__invoke',
100+
];
98101
}
99102
}
100103
}

tests/Integration/Events/EventFakeTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ public function testAssertListening()
147147
'Illuminate\\Tests\\Integration\\Events\\PostAutoEventSubscriber@handle',
148148
PostEventSubscriber::class,
149149
[PostEventSubscriber::class, 'foo'],
150+
InvokableEventSubscriber::class,
150151
]);
151152

152153
foreach ($listenersOfSameEventInRandomOrder as $listener) {
@@ -172,6 +173,7 @@ public function testAssertListening()
172173
Event::assertListening(NonImportantEvent::class, Closure::class);
173174
Event::assertListening('eloquent.saving: '.Post::class, PostObserver::class.'@saving');
174175
Event::assertListening('eloquent.saving: '.Post::class, [PostObserver::class, 'saving']);
176+
Event::assertListening('event', InvokableEventSubscriber::class);
175177
}
176178
}
177179

@@ -231,3 +233,11 @@ public function saving(Post $post)
231233
$post->slug = sprintf('%s-Test', $post->title);
232234
}
233235
}
236+
237+
class InvokableEventSubscriber
238+
{
239+
public function __invoke($event)
240+
{
241+
//
242+
}
243+
}

0 commit comments

Comments
 (0)