1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace League\Event; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface; |
7
|
|
|
|
8
|
|
|
class BufferedEventDispatcherTest extends TestCase |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @test |
12
|
|
|
* @dataProvider dpScenariosWhereSubscribingIsTried |
13
|
|
|
*/ |
14
|
|
|
public function subscribing_does_not_work_when_the_underlying_dispatcher_does_not_allow_subscribing( |
15
|
|
|
callable $scenario |
16
|
|
|
): void { |
17
|
|
|
$internalDispatcher = new class() implements EventDispatcherInterface { |
18
|
|
|
public function dispatch(object $event): iterable |
19
|
|
|
{ |
20
|
|
|
} |
21
|
|
|
}; |
22
|
|
|
$dispatcher = new BufferedEventDispatcher($internalDispatcher); |
23
|
|
|
|
24
|
|
|
$this->expectExceptionObject( |
25
|
|
|
UnableToSubscribeListener::becauseTheEventDispatcherDoesNotAcceptListeners($internalDispatcher) |
26
|
|
|
); |
27
|
|
|
|
28
|
|
|
$scenario($dispatcher); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function dpScenariosWhereSubscribingIsTried(): iterable |
32
|
|
|
{ |
33
|
|
|
$listener1 = new ListenerSpy(); |
34
|
|
|
$listener2 = new ListenerSpy(); |
35
|
|
|
$listener3 = new ListenerSpy(); |
36
|
|
|
|
37
|
|
|
yield "subscribing" => [ |
38
|
|
|
function (ListenerAcceptor $dispatcher) use ($listener1) { |
39
|
|
|
$dispatcher->subscribeTo('event', $listener1); |
40
|
|
|
}, |
41
|
|
|
$listener1, |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
yield "subscribing once" => [ |
45
|
|
|
function (ListenerAcceptor $dispatcher) use ($listener2) { |
46
|
|
|
$dispatcher->subscribeOnceTo('event', $listener2); |
47
|
|
|
}, |
48
|
|
|
$listener2, |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
yield "subscribing from subscriber" => [ |
52
|
|
|
function (ListenerAcceptor $dispatcher) use ($listener3) { |
53
|
|
|
$dispatcher->subscribeListenersFrom( |
54
|
|
|
new class ($listener3) implements ListenerSubscriber { |
55
|
|
|
/** @var Listener */ |
56
|
|
|
private $listener; |
57
|
|
|
|
58
|
|
|
public function __construct(Listener $listener) |
59
|
|
|
{ |
60
|
|
|
$this->listener = $listener; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function subscribeListeners(ListenerAcceptor $acceptor): void |
64
|
|
|
{ |
65
|
|
|
$acceptor->subscribeTo('event', $this->listener); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
); |
69
|
|
|
}, |
70
|
|
|
$listener3, |
71
|
|
|
]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @test |
76
|
|
|
* @dataProvider dpScenariosWhereSubscribingIsTried |
77
|
|
|
*/ |
78
|
|
|
public function subscribing_with_the_dispatcher(callable $scenario, ListenerSpy $listener): void |
79
|
|
|
{ |
80
|
|
|
$dispatcher = new BufferedEventDispatcher(new EventDispatcher()); |
81
|
|
|
|
82
|
|
|
$scenario($dispatcher); |
83
|
|
|
|
84
|
|
|
$dispatcher->dispatch(new StubNamedEvent('event')); |
|
|
|
|
85
|
|
|
|
86
|
|
|
$this->assertEquals(0, $listener->numberOfTimeCalled()); |
87
|
|
|
|
88
|
|
|
$dispatcher->dispatchBufferedEvents(); |
89
|
|
|
|
90
|
|
|
$this->assertEquals(1, $listener->numberOfTimeCalled()); |
91
|
|
|
|
92
|
|
|
$dispatcher->dispatchBufferedEvents(); |
93
|
|
|
|
94
|
|
|
$this->assertEquals(1, $listener->numberOfTimeCalled()); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: