Completed
Push — master ( 9ebc9b...36e003 )
by Frank
01:12
created

src/EventDispatcherTest.php (19 issues)

Upgrade to new PHP Analysis Engine

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\ListenerProviderInterface;
9
use stdClass;
10
11
class EventDispatcherTest extends TestCase
12
{
13
    /**
14
     * @test
15
     */
16
    public function listening_to_a_plain_object_event(): void
17
    {
18
        $dispatcher = new EventDispatcher();
19
        $listenerSpy = new ListenerSpy();
20
        $event = new stdClass;
21
22
        $dispatcher->subscribeTo(stdClass::class, $listenerSpy);
23
        $dispatcher->dispatch($event);
0 ignored issues
show
$event is of type object<stdClass>, but the function expects a object<League\Event\object>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
24
25
        $this->assertTrue($listenerSpy->wasCalledWith($event));
0 ignored issues
show
$event is of type object<stdClass>, but the function expects a object<League\Event\object>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
26
    }
27
28
    /**
29
     * @test
30
     */
31
    public function dispatching_returns_the_event_object(): void
32
    {
33
        $event = new stdClass();
34
        $dispatcher = new  EventDispatcher();
35
36
        $returnedEvent = $dispatcher->dispatch($event);
0 ignored issues
show
$event is of type object<stdClass>, but the function expects a object<League\Event\object>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
37
38
        $this->assertSame($event, $returnedEvent);
39
    }
40
41
    /**
42
     * @test
43
     */
44
    public function listening_to_a_named_event(): void
45
    {
46
        $dispatcher = new EventDispatcher();
47
        $listenerSpy = new ListenerSpy();
48
        $event = new StubNamedEvent('event.name');
49
50
        $dispatcher->subscribeTo('event.name', $listenerSpy);
51
        $dispatcher->dispatch($event);
0 ignored issues
show
$event is of type object<League\Event\StubNamedEvent>, but the function expects a object<League\Event\object>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
52
53
        $this->assertTrue($listenerSpy->wasCalledWith($event));
0 ignored issues
show
$event is of type object<League\Event\StubNamedEvent>, but the function expects a object<League\Event\object>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
54
    }
55
56
    /**
57
     * @test
58
     */
59
    public function listening_to_a_named_event_ignores_other_names(): void
60
    {
61
        $dispatcher = new EventDispatcher();
62
        $listenerSpy = new ListenerSpy();
63
        $dispatcher->subscribeTo('event.name', $listenerSpy);
64
        $dispatcher->dispatch(new StubNamedEvent('event.name'));
0 ignored issues
show
new \League\Event\StubNamedEvent('event.name') is of type object<League\Event\StubNamedEvent>, but the function expects a object<League\Event\object>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
65
        $dispatcher->dispatch(new StubNamedEvent('other.event.name'));
0 ignored issues
show
new \League\Event\StubNa...ent('other.event.name') is of type object<League\Event\StubNamedEvent>, but the function expects a object<League\Event\object>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
66
67
        $this->assertEquals(1, $listenerSpy->numberOfTimeCalled());
68
    }
69
70
    /**
71
     * @test
72
     */
73 View Code Duplication
    public function it_uses_a_provided_listener_provider(): void
74
    {
75
        $listenerSpy = new ListenerSpy();
76
        $provider = new PrioritizedListenerRegistry();
77
        $provider->subscribeTo(stdClass::class, $listenerSpy);
78
        $dispatcher = new EventDispatcher($provider);
79
        $event = new stdClass();
80
81
        $dispatcher->dispatch($event);
0 ignored issues
show
$event is of type object<stdClass>, but the function expects a object<League\Event\object>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
82
83
        $this->assertTrue($listenerSpy->wasCalledWith($event));
0 ignored issues
show
$event is of type object<stdClass>, but the function expects a object<League\Event\object>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
84
    }
85
86
    /**
87
     * @test
88
     */
89 View Code Duplication
    public function it_only_keeps_notifying_handlers_when_the_event_propagation_is_not_stopped(): void
90
    {
91
        $dispatcher = new EventDispatcher();
92
        $listenerSpy = new ListenerSpy();
93
        $event = new StubStoppableEvent();
94
95
        $dispatcher->subscribeTo(
96
            StubStoppableEvent::class,
97
            function (StubStoppableEvent $event) {
98
                $event->stopPropagation();
99
            }
100
        );
101
        $dispatcher->subscribeTo(StubStoppableEvent::class, $listenerSpy);
102
        $dispatcher->dispatch($event);
0 ignored issues
show
$event is of type object<League\Event\StubStoppableEvent>, but the function expects a object<League\Event\object>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
103
104
        $this->assertFalse($listenerSpy->wasCalledWith($event));
0 ignored issues
show
$event is of type object<League\Event\StubStoppableEvent>, but the function expects a object<League\Event\object>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
105
    }
106
107
    /**
108
     * @test
109
     */
110
    public function it_calls_one_time_listeners_one_time(): void
111
    {
112
        $normalListener = new ListenerSpy();
113
        $oneTimeListener = new ListenerSpy();
114
115
        $dispatcher = new EventDispatcher();
116
        $dispatcher->subscribeTo(stdClass::class, $normalListener);
117
        $dispatcher->subscribeOnceTo(stdClass::class, $oneTimeListener);
118
119
        $dispatcher->dispatch(new stdClass());
0 ignored issues
show
new \stdClass() is of type object<stdClass>, but the function expects a object<League\Event\object>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
120
        $dispatcher->dispatch(new stdClass());
0 ignored issues
show
new \stdClass() is of type object<stdClass>, but the function expects a object<League\Event\object>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
121
        $dispatcher->dispatch(new stdClass());
0 ignored issues
show
new \stdClass() is of type object<stdClass>, but the function expects a object<League\Event\object>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
122
123
        $this->assertEquals(1, $oneTimeListener->numberOfTimeCalled());
124
        $this->assertEquals(3, $normalListener->numberOfTimeCalled());
125
    }
126
127
    /**
128
     * @test
129
     * @dataProvider dpScenariosCausingSubscribingFailure
130
     */
131
    public function subscribing_does_not_work_when_the_underlying_provider_does_not_allow_subscribing(
132
        callable $scenario
133
    ): void {
134
        $provider = new class() implements ListenerProviderInterface {
135
            public function getListenersForEvent(object $event): iterable
136
            {
137
                return [];
138
            }
139
        };
140
        $dispatcher = new EventDispatcher($provider);
141
142
        $this->expectExceptionObject(
143
            UnableToSubscribeListener::becauseTheListenerProviderDoesNotAcceptListeners($provider)
144
        );
145
146
        $scenario($dispatcher);
147
    }
148
149
    public function dpScenariosCausingSubscribingFailure(): iterable
150
    {
151
        yield "subscribing" => [
152
            function (EventDispatcher $dispatcher) {
153
                $dispatcher->subscribeTo(
154
                    'event',
155
                    function () {
156
                    }
157
                );
158
            },
159
        ];
160
161
        yield "subscribing once" => [
162
            function (EventDispatcher $dispatcher) {
163
                $dispatcher->subscribeOnceTo(
164
                    'event',
165
                    function () {
166
                    }
167
                );
168
            },
169
        ];
170
171
        yield "subscribing from subscriber" => [
172
            function (EventDispatcher $dispatcher) {
173
                $dispatcher->subscribeListenersFrom(
174
                    new class() implements ListenerSubscriber {
175
                        public function subscribeListeners(ListenerRegistry $acceptor): void
176
                        {
177
                        }
178
                    }
179
                );
180
            },
181
        ];
182
    }
183
184
    /**
185
     * @test
186
     */
187
    public function listeners_are_prioritized(): void
188
    {
189
        $dispatcher = new EventDispatcher();
190
        $event = new StubMutableEvent('Hi!');
191
        $append = static function (string $value) {
192
            return static function (StubMutableEvent $event) use ($value) {
193
                $event->append(' ' . $value);
194
            };
195
        };
196
        $appendHello = $append('Hello,');
197
        $appendWorld = $append('World!');
198
        $appendGoodBye = $append('Good bye!');
199
        $dispatcher->subscribeTo(StubMutableEvent::class, $appendWorld, 0);
200
        $dispatcher->subscribeTo(StubMutableEvent::class, $appendHello, 10);
201
        $dispatcher->subscribeTo(StubMutableEvent::class, $appendGoodBye, -10);
202
203
        $dispatcher->dispatch($event);
0 ignored issues
show
$event is of type object<League\Event\StubMutableEvent>, but the function expects a object<League\Event\object>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
204
205
        $this->assertEquals('Hi! Hello, World! Good bye!', $event->value());
206
    }
207
208
    /**
209
     * @test
210
     */
211
    public function events_from_an_event_generator_can_be_dispatched(): void
212
    {
213
        $dispatcher = new EventDispatcher();
214
        $listener = new ListenerSpy();
215
        $dispatcher->subscribeTo(stdClass::class, $listener);
216
217
        $eventGenerator = new class() implements EventGenerator {
218
            use EventGeneratorBehavior {
219
                recordEvent as public;
220
            }
221
        };
222
        $eventGenerator->recordEvent(new stdClass());
0 ignored issues
show
new \stdClass() is of type object<stdClass>, but the function expects a object<League\Event\object>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
223
        $eventGenerator->recordEvent(new stdClass());
0 ignored issues
show
new \stdClass() is of type object<stdClass>, but the function expects a object<League\Event\object>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
224
        $eventGenerator->recordEvent(new stdClass());
0 ignored issues
show
new \stdClass() is of type object<stdClass>, but the function expects a object<League\Event\object>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
225
        $dispatcher->dispatchGeneratedEvents($eventGenerator);
226
227
        $this->assertEquals(3, $listener->numberOfTimeCalled());
228
    }
229
230
    /**
231
     * @test
232
     */
233
    public function listeners_can_be_subscribed_through_a_subscriber(): void
234
    {
235
        $subscriber = new class() implements ListenerSubscriber {
236
            public function subscribeListeners(ListenerRegistry $acceptor): void
237
            {
238
                $acceptor->subscribeTo(
239
                    StubMutableEvent::class,
240
                    function (StubMutableEvent $event) {
241
                        $event->append(' mutated');
242
                    }
243
                );
244
            }
245
        };
246
        $dispatcher = new EventDispatcher();
247
        $dispatcher->subscribeListenersFrom($subscriber);
248
        $event = new StubMutableEvent('this is');
249
        $dispatcher->dispatch($event);
0 ignored issues
show
$event is of type object<League\Event\StubMutableEvent>, but the function expects a object<League\Event\object>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
250
251
        $this->assertEquals('this is mutated', $event->value());
252
    }
253
}
254