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