Skip to content

Commit 8599ade

Browse files
authored
[8.x] Fix assertListening check with auto discovery (#41820)
* Fix assertListening check with auto discovery * wip
1 parent 8ca1471 commit 8599ade

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Closure;
66
use Illuminate\Contracts\Events\Dispatcher;
77
use Illuminate\Support\Arr;
8+
use Illuminate\Support\Str;
89
use Illuminate\Support\Traits\ReflectsClosures;
910
use PHPUnit\Framework\Assert as PHPUnit;
1011
use ReflectionFunction;
@@ -61,6 +62,10 @@ public function assertListening($expectedEvent, $expectedListener)
6162
$actualListener = (new ReflectionFunction($listenerClosure))
6263
->getStaticVariables()['listener'];
6364

65+
if (is_string($actualListener) && Str::endsWith($actualListener, '@handle')) {
66+
$actualListener = Str::parseCallback($actualListener)[0];
67+
}
68+
6469
if ($actualListener === $expectedListener ||
6570
($actualListener instanceof Closure &&
6671
$expectedListener === Closure::class)) {

tests/Integration/Events/EventFakeTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ public function testAssertListening()
126126
Event::fake();
127127
Event::listen('event', 'listener');
128128
Event::listen('event', PostEventSubscriber::class);
129+
Event::listen('event', 'Illuminate\\Tests\\Integration\\Events\\PostAutoEventSubscriber@handle');
129130
Event::listen('event', [PostEventSubscriber::class, 'foo']);
130131
Event::subscribe(PostEventSubscriber::class);
131132
Event::listen(function (NonImportantEvent $event) {
@@ -134,6 +135,7 @@ public function testAssertListening()
134135

135136
Event::assertListening('event', 'listener');
136137
Event::assertListening('event', PostEventSubscriber::class);
138+
Event::assertListening('event', PostAutoEventSubscriber::class);
137139
Event::assertListening('event', [PostEventSubscriber::class, 'foo']);
138140
Event::assertListening('post-created', [PostEventSubscriber::class, 'handlePostCreated']);
139141
Event::assertListening(NonImportantEvent::class, Closure::class);
@@ -165,6 +167,14 @@ public function subscribe($events)
165167
}
166168
}
167169

170+
class PostAutoEventSubscriber
171+
{
172+
public function handle($event)
173+
{
174+
//
175+
}
176+
}
177+
168178
class PostObserver
169179
{
170180
public function saving(Post $post)

0 commit comments

Comments
 (0)