Skip to content

Commit 2a66f57

Browse files
committed
add to tests - formatting
1 parent 8b01f79 commit 2a66f57

File tree

2 files changed

+40
-36
lines changed

2 files changed

+40
-36
lines changed

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

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,38 @@ public function __construct(Dispatcher $dispatcher, $eventsToFake = [])
4848
$this->eventsToFake = Arr::wrap($eventsToFake);
4949
}
5050

51+
/**
52+
* Assert if an event has a listener attached to it.
53+
*
54+
* @param string $expectedEvent
55+
* @param string $expectedListener
56+
* @return void
57+
*/
58+
public function assertListening($expectedEvent, $expectedListener)
59+
{
60+
foreach ($this->dispatcher->getListeners($expectedEvent) as $listenerClosure) {
61+
$actualListener = (new ReflectionFunction($listenerClosure))
62+
->getStaticVariables()['listener'];
63+
64+
if ($actualListener === $expectedListener ||
65+
($actualListener instanceof Closure &&
66+
$expectedListener === Closure::class)) {
67+
PHPUnit::assertTrue(true);
68+
69+
return;
70+
}
71+
}
72+
73+
PHPUnit::assertTrue(
74+
false,
75+
sprintf(
76+
'Event [%s] does not have the [%s] listener attached to it',
77+
$expectedEvent,
78+
print_r($expectedListener, true)
79+
)
80+
);
81+
}
82+
5183
/**
5284
* Assert if an event was dispatched based on a truth-test callback.
5385
*
@@ -285,36 +317,4 @@ public function until($event, $payload = [])
285317
{
286318
return $this->dispatch($event, $payload, true);
287319
}
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-
}
320320
}

tests/Integration/Events/EventFakeTest.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,18 +128,22 @@ public function testNonFakedHaltedEventGetsProperlyDispatchedAndReturnsResponse(
128128
Event::assertNotDispatched(NonImportantEvent::class);
129129
}
130130

131-
public function testAssertAttached()
131+
public function testAssertListening()
132132
{
133133
Event::fake();
134134
Event::listen('event', 'listener');
135+
Event::listen('event', PostEventSubscriber::class);
136+
Event::listen('event', [PostEventSubscriber::class, 'foo']);
135137
Event::subscribe(PostEventSubscriber::class);
136138
Event::listen(function (NonImportantEvent $event) {
137139
// do something
138140
});
139141

140-
Event::assertAttached('event', 'listener');
141-
Event::assertAttached('post-created', [PostEventSubscriber::class, 'handlePostCreated']);
142-
Event::assertAttached(NonImportantEvent::class, Closure::class);
142+
Event::assertListening('event', 'listener');
143+
Event::assertListening('event', PostEventSubscriber::class);
144+
Event::assertListening('event', [PostEventSubscriber::class, 'foo']);
145+
Event::assertListening('post-created', [PostEventSubscriber::class, 'handlePostCreated']);
146+
Event::assertListening(NonImportantEvent::class, Closure::class);
143147
}
144148
}
145149

0 commit comments

Comments
 (0)