Passed
Pull Request — master (#2)
by Koldo
02:14
created

EventDispatcherTest::givenAnEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AntidotTest\Event;
6
7
use Antidot\Event\EventDispatcher;
8
use Antidot\Event\EventInterface;
9
use Antidot\Event\ListenerInterface;
10
use PHPUnit\Framework\TestCase;
11
use Psr\EventDispatcher\ListenerProviderInterface;
12
13
class EventDispatcherTest extends TestCase
14
{
15
    /** @var EventInterface|\PHPUnit\Framework\MockObject\MockObject */
16
    private $event;
17
    /** @var \PHPUnit\Framework\MockObject\MockObject */
18
    private $listenerProvider;
19
    /** @var ListenerInterface */
20
    private $listener1;
21
    /** @var ListenerInterface */
22
    private $listener2;
23
24
    public function testItShouldDispatchEvents(): void
25
    {
26
        $this->givenAnEvent();
27
        $this->havingAListenerProvider();
28
        $this->havingListenersForEventInListenerProvider();
29
        $this->eventIsHandledByConfiguredListenersExpectsBeCalled(2);
30
        $this->whenEventIsDispatched();
31
    }
32
33
    public function testItShouldNotHandleAnyEventWhenPropagationIsStopped(): void
34
    {
35
        $this->givenAnEvent();
36
        $this->givenEventHasStoppedPropagation();
37
        $this->havingAListenerProvider();
38
        $this->havingListenersForStoppedEventInListenerProvider();
39
        $this->eventIsHandledByConfiguredListenersExpectsBeCalled(1);
40
        $this->whenEventIsDispatched();
41
    }
42
43
    private function givenAnEvent(): void
44
    {
45
        $this->event = $this->createMock(EventInterface::class);
46
    }
47
48
    private function havingAListenerProvider(): void
49
    {
50
        $this->listenerProvider = $this->createMock(ListenerProviderInterface::class);
51
    }
52
53
    private function havingListenersForEventInListenerProvider(): void
54
    {
55
        $this->listener1 = $this->makeListener(1);
56
        $this->listener2 = $this->makeListener(1);
57
58
        $this->listenerProvider
59
            ->expects($this->once())
60
            ->method('getListenersForEvent')
61
            ->with($this->event)
62
            ->willReturn([
63
                $this->listener1,
64
                $this->listener2,
65
            ]);
66
    }
67
    private function havingListenersForStoppedEventInListenerProvider(): void
68
    {
69
        $this->listener1 = $this->makeListener(0);
70
        $this->listener2 = $this->makeListener(0);
71
72
        $this->listenerProvider
73
            ->expects($this->once())
74
            ->method('getListenersForEvent')
75
            ->with($this->event)
76
            ->willReturn([
77
                $this->listener1,
78
                $this->listener2,
79
            ]);
80
    }
81
82
83
    private function eventIsHandledByConfiguredListenersExpectsBeCalled(int $callTimes): void
84
    {
85
        $this->event
86
            ->expects($this->exactly($callTimes))
1 ignored issue
show
Bug introduced by
The method expects() does not exist on Antidot\Event\EventInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

86
            ->/** @scrutinizer ignore-call */ 
87
              expects($this->exactly($callTimes))

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
87
            ->method('isPropagationStopped');
88
    }
89
90
    private function whenEventIsDispatched(): void
91
    {
92
        $eventDispatcher = new EventDispatcher($this->listenerProvider);
93
        $eventDispatcher->dispatch($this->event);
94
    }
95
96
    private function makeListener(int $callTimes): ListenerInterface
97
    {
98
        $listener = $this->createMock(ListenerInterface::class);
99
        $listener
100
            ->expects($this->exactly($callTimes))
101
            ->method('__invoke')
102
            ->with($this->event);
103
104
        return $listener;
1 ignored issue
show
Bug Best Practice introduced by
The expression return $listener returns the type PHPUnit\Framework\MockObject\MockObject which is incompatible with the type-hinted return Antidot\Event\ListenerInterface.
Loading history...
105
    }
106
107
    private function givenEventHasStoppedPropagation(): void
108
    {
109
        $this->event
110
            ->method('isPropagationStopped')
2 ignored issues
show
Bug introduced by
The method method() does not exist on Antidot\Event\EventInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

110
            ->/** @scrutinizer ignore-call */ 
111
              method('isPropagationStopped')

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method method() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

110
            ->/** @scrutinizer ignore-call */ 
111
              method('isPropagationStopped')

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
111
            ->willReturn(true);
112
    }
113
}
114