Completed
Push — master ( ae6a92...7b43ed )
by Frank
01:13
created

subscribing_with_the_dispatcher()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 1
nc 1
nop 2
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'));
0 ignored issues
show
Documentation introduced by Frank de Jonge
new \League\Event\StubNamedEvent('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...
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