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

src/BufferedEventDispatcherTest.php (5 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\EventDispatcherInterface;
9
use stdClass;
10
11
class BufferedEventDispatcherTest extends TestCase
12
{
13
    /**
14
     * @test
15
     * @dataProvider dpScenariosWhereSubscribingIsTried
16
     */
17
    public function subscribing_does_not_work_when_the_underlying_dispatcher_does_not_allow_subscribing(
18
        callable $scenario
19
    ): void {
20
        $internalDispatcher = new class() implements EventDispatcherInterface {
21
            public function dispatch(object $event): object
22
            {
23
                return $event;
24
            }
25
        };
26
        $dispatcher = new BufferedEventDispatcher($internalDispatcher);
27
28
        $this->expectExceptionObject(
29
            UnableToSubscribeListener::becauseTheEventDispatcherDoesNotAcceptListeners($internalDispatcher)
30
        );
31
32
        $scenario($dispatcher);
33
    }
34
35
    public function dpScenariosWhereSubscribingIsTried(): iterable
36
    {
37
        $listener1 = new ListenerSpy();
38
        $listener2 = new ListenerSpy();
39
        $listener3 = new ListenerSpy();
40
41
        yield "subscribing" => [
42
            function (ListenerRegistry $dispatcher) use ($listener1) {
43
                $dispatcher->subscribeTo('event', $listener1);
44
            },
45
            $listener1,
46
        ];
47
48
        yield "subscribing once" => [
49
            function (ListenerRegistry $dispatcher) use ($listener2) {
50
                $dispatcher->subscribeOnceTo('event', $listener2);
51
            },
52
            $listener2,
53
        ];
54
55
        yield "subscribing from subscriber" => [
56
            function (ListenerRegistry $dispatcher) use ($listener3) {
57
                $dispatcher->subscribeListenersFrom(
58
                    new class($listener3) implements ListenerSubscriber {
59
                        /**  @var Listener */
60
                        private $listener;
61
62
                        public function __construct(Listener $listener)
63
                        {
64
                            $this->listener = $listener;
65
                        }
66
67
                        public function subscribeListeners(ListenerRegistry $acceptor): void
68
                        {
69
                            $acceptor->subscribeTo('event', $this->listener);
70
                        }
71
                    }
72
                );
73
            },
74
            $listener3,
75
        ];
76
    }
77
78
    /**
79
     * @test
80
     * @dataProvider dpScenariosWhereSubscribingIsTried
81
     */
82
    public function subscribing_with_the_dispatcher(callable $scenario, ListenerSpy $listener): void
83
    {
84
        $dispatcher = new BufferedEventDispatcher(new EventDispatcher());
85
86
        $scenario($dispatcher);
87
        $dispatcher->dispatch($event = new StubNamedEvent('event'));
0 ignored issues
show
$event = new \League\Eve...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...
88
89
        // Assert dispatching is not done yet
90
        $this->assertEquals(0, $listener->numberOfTimeCalled());
91
92
        // Triggering a dispatch
93
        $dispatcher->dispatchBufferedEvents();
94
95
        $this->assertEquals(1, $listener->numberOfTimeCalled());
96
97
        $dispatcher->dispatchBufferedEvents();
98
99
        $this->assertEquals(1, $listener->numberOfTimeCalled());
100
    }
101
102
    /**
103
     * @test
104
     */
105
    public function dispatching_buffered_events_returns_the_events_in_the_order_of_dispatching(): void
106
    {
107
        $dispatcher = new BufferedEventDispatcher(new EventDispatcher());
108
        $dispatcher->dispatch($first = new stdClass());
0 ignored issues
show
$first = 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...
109
        $dispatcher->dispatch($second = new stdClass());
0 ignored issues
show
$second = 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...
110
        $dispatcher->dispatch($third = new stdClass());
0 ignored issues
show
$third = 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...
111
112
        $dispatchedEvents = $dispatcher->dispatchBufferedEvents();
113
114
        $this->assertIsArray($dispatchedEvents);
115
        $this->assertCount(3, $dispatchedEvents);
0 ignored issues
show
$dispatchedEvents is of type array<integer,object>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

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...
116
        $this->assertContainsOnlyInstancesOf(stdClass::class, $dispatchedEvents);
117
        $this->assertSame($first, $dispatchedEvents[0]);
118
        $this->assertSame($second, $dispatchedEvents[1]);
119
        $this->assertSame($third, $dispatchedEvents[2]);
120
    }
121
}
122