Skip to content
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

[8.x] Fix assertListening check with auto discovery #41820

Merged
merged 2 commits into from Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Illuminate/Support/Testing/Fakes/EventFake.php
Expand Up @@ -5,6 +5,7 @@
use Closure;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\ReflectsClosures;
use PHPUnit\Framework\Assert as PHPUnit;
use ReflectionFunction;
Expand Down Expand Up @@ -61,6 +62,10 @@ public function assertListening($expectedEvent, $expectedListener)
$actualListener = (new ReflectionFunction($listenerClosure))
->getStaticVariables()['listener'];

if (is_string($actualListener) && Str::endsWith($actualListener, '@handle')) {
$actualListener = Str::parseCallback($actualListener)[0];
}

if ($actualListener === $expectedListener ||
($actualListener instanceof Closure &&
$expectedListener === Closure::class)) {
Expand Down
10 changes: 10 additions & 0 deletions tests/Integration/Events/EventFakeTest.php
Expand Up @@ -126,6 +126,7 @@ public function testAssertListening()
Event::fake();
Event::listen('event', 'listener');
Event::listen('event', PostEventSubscriber::class);
Event::listen('event', 'Illuminate\\Tests\\Integration\\Events\\PostAutoEventSubscriber@handle');
Event::listen('event', [PostEventSubscriber::class, 'foo']);
Event::subscribe(PostEventSubscriber::class);
Event::listen(function (NonImportantEvent $event) {
Expand All @@ -134,6 +135,7 @@ public function testAssertListening()

Event::assertListening('event', 'listener');
Event::assertListening('event', PostEventSubscriber::class);
Event::assertListening('event', PostAutoEventSubscriber::class);
Event::assertListening('event', [PostEventSubscriber::class, 'foo']);
Event::assertListening('post-created', [PostEventSubscriber::class, 'handlePostCreated']);
Event::assertListening(NonImportantEvent::class, Closure::class);
Expand Down Expand Up @@ -165,6 +167,14 @@ public function subscribe($events)
}
}

class PostAutoEventSubscriber
{
public function handle($event)
{
//
}
}

class PostObserver
{
public function saving(Post $post)
Expand Down