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] Added Event::assertAttached to EventFake class #36690

Merged
merged 1 commit into from
Mar 21, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/Illuminate/Support/Testing/Fakes/EventFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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))
);
}
}
30 changes: 30 additions & 0 deletions tests/Integration/Events/EventFakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down