Skip to content

Commit 8b01f79

Browse files
committed
Added Event::assertAttached to EventFake class
1 parent 72ea328 commit 8b01f79

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Support\Arr;
88
use Illuminate\Support\Traits\ReflectsClosures;
99
use PHPUnit\Framework\Assert as PHPUnit;
10+
use ReflectionFunction;
1011

1112
class EventFake implements Dispatcher
1213
{
@@ -284,4 +285,36 @@ public function until($event, $payload = [])
284285
{
285286
return $this->dispatch($event, $payload, true);
286287
}
288+
289+
/**
290+
* Assert if an event has a listener attached to it.
291+
*
292+
* @param string $expectedEvent
293+
* @param string $expectedListener
294+
* @return void
295+
*/
296+
public function assertAttached($expectedEvent, $expectedListener)
297+
{
298+
foreach ($this->dispatcher->getListeners($expectedEvent) as $listenerClosure) {
299+
$reflection = new ReflectionFunction($listenerClosure);
300+
$actualListener = $reflection->getStaticVariables()['listener'];
301+
302+
if ($actualListener === $expectedListener) {
303+
PHPUnit::assertTrue(true);
304+
305+
return;
306+
}
307+
308+
if ($actualListener instanceof Closure && $expectedListener === Closure::class) {
309+
PHPUnit::assertTrue(true);
310+
311+
return;
312+
}
313+
}
314+
315+
PHPUnit::assertTrue(
316+
false,
317+
sprintf('Event %s does not have the %s listener attached to it', $expectedEvent, print_r($expectedListener, true))
318+
);
319+
}
287320
}

tests/Integration/Events/EventFakeTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Illuminate\Tests\Integration\Events;
44

5+
use Closure;
56
use Illuminate\Database\Eloquent\Model;
67
use Illuminate\Database\Schema\Blueprint;
78
use Illuminate\Support\Facades\Event;
@@ -126,6 +127,20 @@ public function testNonFakedHaltedEventGetsProperlyDispatchedAndReturnsResponse(
126127

127128
Event::assertNotDispatched(NonImportantEvent::class);
128129
}
130+
131+
public function testAssertAttached()
132+
{
133+
Event::fake();
134+
Event::listen('event', 'listener');
135+
Event::subscribe(PostEventSubscriber::class);
136+
Event::listen(function (NonImportantEvent $event) {
137+
// do something
138+
});
139+
140+
Event::assertAttached('event', 'listener');
141+
Event::assertAttached('post-created', [PostEventSubscriber::class, 'handlePostCreated']);
142+
Event::assertAttached(NonImportantEvent::class, Closure::class);
143+
}
129144
}
130145

131146
class Post extends Model
@@ -138,6 +153,21 @@ class NonImportantEvent
138153
//
139154
}
140155

156+
class PostEventSubscriber
157+
{
158+
public function handlePostCreated($event)
159+
{
160+
}
161+
162+
public function subscribe($events)
163+
{
164+
$events->listen(
165+
'post-created',
166+
[PostEventSubscriber::class, 'handlePostCreated']
167+
);
168+
}
169+
}
170+
141171
class PostObserver
142172
{
143173
public function saving(Post $post)

0 commit comments

Comments
 (0)