@@ -48,6 +48,38 @@ public function __construct(Dispatcher $dispatcher, $eventsToFake = [])
48
48
$ this ->eventsToFake = Arr::wrap ($ eventsToFake );
49
49
}
50
50
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
+
51
83
/**
52
84
* Assert if an event was dispatched based on a truth-test callback.
53
85
*
@@ -285,36 +317,4 @@ public function until($event, $payload = [])
285
317
{
286
318
return $ this ->dispatch ($ event , $ payload , true );
287
319
}
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
- }
320
320
}
0 commit comments