Skip to content

Commit 19e98d6

Browse files
authored
[9.x] Improve EventFake::assertListening() support for subscribers (#42193)
* Add tests * Update SupportTestingEventFakeTest.php * Assert correct listeners * Create ArraySubscriber.php * Create NonHandleMethodSubscriber.php * Support non-standard event subscriber handle methods * Apply StyleCI changes * Remove the array listener transform Should fix prior test (but doesn't break functionality - only highlights that the other test is perhaps overly strict?) * Remove unnecessary stub See `tests/Integration/Events/EventFakeTest.php` * Remove redundant stub See `tests/Integration/Events/EventFakeTest.php` * Remove redundant tests * Add test for string listener in subscriber * Fix assertion * Handle assertions where the method isn't explicit * Style CI
1 parent 6181142 commit 19e98d6

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,12 @@ public function assertListening($expectedEvent, $expectedListener)
6262
$actualListener = (new ReflectionFunction($listenerClosure))
6363
->getStaticVariables()['listener'];
6464

65-
if (is_string($actualListener) && Str::endsWith($actualListener, '@handle')) {
66-
$actualListener = Str::parseCallback($actualListener)[0];
65+
if (is_string($actualListener) && Str::contains($actualListener, '@')) {
66+
$actualListener = Str::parseCallback($actualListener);
67+
68+
if (is_string($expectedListener) && ! Str::contains($expectedListener, '@')) {
69+
$expectedListener = [$expectedListener, 'handle'];
70+
}
6771
}
6872

6973
if ($actualListener === $expectedListener ||

tests/Integration/Events/EventFakeTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ public function testAssertListening()
138138
Event::assertListening('event', PostAutoEventSubscriber::class);
139139
Event::assertListening('event', [PostEventSubscriber::class, 'foo']);
140140
Event::assertListening('post-created', [PostEventSubscriber::class, 'handlePostCreated']);
141+
Event::assertListening('post-deleted', [PostEventSubscriber::class, 'handlePostDeleted']);
141142
Event::assertListening(NonImportantEvent::class, Closure::class);
142143
}
143144
}
@@ -158,12 +159,21 @@ public function handlePostCreated($event)
158159
{
159160
}
160161

162+
public function handlePostDeleted($event)
163+
{
164+
}
165+
161166
public function subscribe($events)
162167
{
163168
$events->listen(
164169
'post-created',
165170
[PostEventSubscriber::class, 'handlePostCreated']
166171
);
172+
173+
$events->listen(
174+
'post-deleted',
175+
PostEventSubscriber::class.'@handlePostDeleted'
176+
);
167177
}
168178
}
169179

0 commit comments

Comments
 (0)