diff --git a/src/Illuminate/Support/Testing/Fakes/EventFake.php b/src/Illuminate/Support/Testing/Fakes/EventFake.php index 88fcb84cea48..d21227ef6fdc 100644 --- a/src/Illuminate/Support/Testing/Fakes/EventFake.php +++ b/src/Illuminate/Support/Testing/Fakes/EventFake.php @@ -7,6 +7,7 @@ use Illuminate\Support\Arr; use Illuminate\Support\Traits\ReflectsClosures; use PHPUnit\Framework\Assert as PHPUnit; +use ReflectionFunction; class EventFake implements Dispatcher { @@ -284,4 +285,36 @@ public function until($event, $payload = []) { return $this->dispatch($event, $payload, true); } + + /** + * Assert if an event has a listener attached to it. + * + * @param string $expectedEvent + * @param string $expectedListener + * @return void + */ + public function assertAttached($expectedEvent, $expectedListener) + { + foreach ($this->dispatcher->getListeners($expectedEvent) as $listenerClosure) { + $reflection = new ReflectionFunction($listenerClosure); + $actualListener = $reflection->getStaticVariables()['listener']; + + if ($actualListener === $expectedListener) { + PHPUnit::assertTrue(true); + + return; + } + + if ($actualListener instanceof Closure && $expectedListener === Closure::class) { + PHPUnit::assertTrue(true); + + return; + } + } + + PHPUnit::assertTrue( + false, + sprintf('Event %s does not have the %s listener attached to it', $expectedEvent, print_r($expectedListener, true)) + ); + } } diff --git a/tests/Integration/Events/EventFakeTest.php b/tests/Integration/Events/EventFakeTest.php index b69b86e8c88e..411764685efb 100644 --- a/tests/Integration/Events/EventFakeTest.php +++ b/tests/Integration/Events/EventFakeTest.php @@ -2,6 +2,7 @@ namespace Illuminate\Tests\Integration\Events; +use Closure; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Event; @@ -126,6 +127,20 @@ public function testNonFakedHaltedEventGetsProperlyDispatchedAndReturnsResponse( Event::assertNotDispatched(NonImportantEvent::class); } + + public function testAssertAttached() + { + Event::fake(); + Event::listen('event', 'listener'); + Event::subscribe(PostEventSubscriber::class); + Event::listen(function (NonImportantEvent $event) { + // do something + }); + + Event::assertAttached('event', 'listener'); + Event::assertAttached('post-created', [PostEventSubscriber::class, 'handlePostCreated']); + Event::assertAttached(NonImportantEvent::class, Closure::class); + } } class Post extends Model @@ -138,6 +153,21 @@ class NonImportantEvent // } +class PostEventSubscriber +{ + public function handlePostCreated($event) + { + } + + public function subscribe($events) + { + $events->listen( + 'post-created', + [PostEventSubscriber::class, 'handlePostCreated'] + ); + } +} + class PostObserver { public function saving(Post $post)