These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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): object |
||
19 | { |
||
20 | return $event; |
||
21 | } |
||
22 | }; |
||
23 | $dispatcher = new BufferedEventDispatcher($internalDispatcher); |
||
24 | |||
25 | $this->expectExceptionObject( |
||
26 | UnableToSubscribeListener::becauseTheEventDispatcherDoesNotAcceptListeners($internalDispatcher) |
||
27 | ); |
||
28 | |||
29 | $scenario($dispatcher); |
||
30 | } |
||
31 | |||
32 | public function dpScenariosWhereSubscribingIsTried(): iterable |
||
33 | { |
||
34 | $listener1 = new ListenerSpy(); |
||
35 | $listener2 = new ListenerSpy(); |
||
36 | $listener3 = new ListenerSpy(); |
||
37 | |||
38 | yield "subscribing" => [ |
||
39 | function (ListenerAcceptor $dispatcher) use ($listener1) { |
||
40 | $dispatcher->subscribeTo('event', $listener1); |
||
41 | }, |
||
42 | $listener1, |
||
43 | ]; |
||
44 | |||
45 | yield "subscribing once" => [ |
||
46 | function (ListenerAcceptor $dispatcher) use ($listener2) { |
||
47 | $dispatcher->subscribeOnceTo('event', $listener2); |
||
48 | }, |
||
49 | $listener2, |
||
50 | ]; |
||
51 | |||
52 | yield "subscribing from subscriber" => [ |
||
53 | function (ListenerAcceptor $dispatcher) use ($listener3) { |
||
54 | $dispatcher->subscribeListenersFrom( |
||
55 | new class ($listener3) implements ListenerSubscriber { |
||
56 | /** @var Listener */ |
||
57 | private $listener; |
||
58 | |||
59 | public function __construct(Listener $listener) |
||
60 | { |
||
61 | $this->listener = $listener; |
||
62 | } |
||
63 | |||
64 | public function subscribeListeners(ListenerAcceptor $acceptor): void |
||
65 | { |
||
66 | $acceptor->subscribeTo('event', $this->listener); |
||
67 | } |
||
68 | } |
||
69 | ); |
||
70 | }, |
||
71 | $listener3, |
||
72 | ]; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @test |
||
77 | * @dataProvider dpScenariosWhereSubscribingIsTried |
||
78 | */ |
||
79 | public function subscribing_with_the_dispatcher(callable $scenario, ListenerSpy $listener): void |
||
80 | { |
||
81 | $dispatcher = new BufferedEventDispatcher(new EventDispatcher()); |
||
82 | |||
83 | $scenario($dispatcher); |
||
84 | |||
85 | $dispatcher->dispatch(new StubNamedEvent('event')); |
||
0 ignored issues
–
show
|
|||
86 | |||
87 | $this->assertEquals(0, $listener->numberOfTimeCalled()); |
||
88 | |||
89 | $dispatcher->dispatchBufferedEvents(); |
||
90 | |||
91 | $this->assertEquals(1, $listener->numberOfTimeCalled()); |
||
92 | |||
93 | $dispatcher->dispatchBufferedEvents(); |
||
94 | |||
95 | $this->assertEquals(1, $listener->numberOfTimeCalled()); |
||
96 | } |
||
97 | } |
||
98 |
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: