Passed
Pull Request — master (#2)
by Koldo
02:07
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\MockObject\MockObject;
11
use PHPUnit\Framework\TestCase;
12
use Psr\EventDispatcher\ListenerProviderInterface;
13
14
class EventDispatcherTest extends TestCase
15
{
16
    /** @var EventInterface|MockObject */
17
    private $event;
18
    /** @var ListenerProviderInterface|MockObject */
19
    private $listenerProvider;
20
    /** @var ListenerInterface|MockObject */
21
    private $listener1;
22
    /** @var ListenerInterface|MockObject */
23
    private $listener2;
24
25
    public function testItShouldDispatchEvents(): void
26
    {
27
        $this->givenAnEvent();
28
        $this->havingAListenerProvider();
29
        $this->havingListenersForEventInListenerProvider();
30
        $this->eventIsHandledByConfiguredListenersExpectsBeCalled(2);
31
        $this->whenEventIsDispatched();
32
    }
33
34
    public function testItShouldNotHandleAnyEventWhenPropagationIsStopped(): void
35
    {
36
        $this->givenAnEvent();
37
        $this->givenEventHasStoppedPropagation();
38
        $this->havingAListenerProvider();
39
        $this->havingListenersForStoppedEventInListenerProvider();
40
        $this->eventIsHandledByConfiguredListenersExpectsBeCalled(1);
41
        $this->whenEventIsDispatched();
42
    }
43
44
    private function givenAnEvent(): void
45
    {
46
        $this->event = $this->createMock(EventInterface::class);
47
    }
48
49
    private function havingAListenerProvider(): void
50
    {
51
        $this->listenerProvider = $this->createMock(ListenerProviderInterface::class);
52
    }
53
54
    private function havingListenersForEventInListenerProvider(): void
55
    {
56
        $this->listener1 = $this->makeListener(1);
57
        $this->listener2 = $this->makeListener(1);
58
59
        $this->listenerProvider
60
            ->expects($this->once())
1 ignored issue
show
Bug introduced by
The method expects() does not exist on Psr\EventDispatcher\ListenerProviderInterface. ( Ignorable by Annotation )

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

60
            ->/** @scrutinizer ignore-call */ 
61
              expects($this->once())

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...
61
            ->method('getListenersForEvent')
62
            ->with($this->event)
63
            ->willReturn([
64
                $this->listener1,
65
                $this->listener2,
66
            ]);
67
    }
68
    private function havingListenersForStoppedEventInListenerProvider(): void
69
    {
70
        $this->listener1 = $this->makeListener(0);
71
        $this->listener2 = $this->makeListener(0);
72
73
        $this->listenerProvider
74
            ->expects($this->once())
75
            ->method('getListenersForEvent')
76
            ->with($this->event)
77
            ->willReturn([
78
                $this->listener1,
79
                $this->listener2,
80
            ]);
81
    }
82
83
84
    private function eventIsHandledByConfiguredListenersExpectsBeCalled(int $callTimes): void
85
    {
86
        $this->event
87
            ->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

87
            ->/** @scrutinizer ignore-call */ 
88
              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...
88
            ->method('isPropagationStopped');
89
    }
90
91
    private function whenEventIsDispatched(): void
92
    {
93
        $eventDispatcher = new EventDispatcher($this->listenerProvider);
94
        $eventDispatcher->dispatch($this->event);
95
    }
96
97
    private function makeListener(int $callTimes): ListenerInterface
98
    {
99
        $listener = $this->createMock(ListenerInterface::class);
100
        $listener
101
            ->expects($this->exactly($callTimes))
102
            ->method('__invoke')
103
            ->with($this->event);
104
105
        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...
106
    }
107
108
    private function givenEventHasStoppedPropagation(): void
109
    {
110
        $this->event
111
            ->method('isPropagationStopped')
2 ignored issues
show
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

111
            ->/** @scrutinizer ignore-call */ 
112
              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 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

111
            ->/** @scrutinizer ignore-call */ 
112
              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...
112
            ->willReturn(true);
113
    }
114
}
115