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)) |
|
|
|
|
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; |
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
private function givenEventHasStoppedPropagation(): void |
108
|
|
|
{ |
109
|
|
|
$this->event |
110
|
|
|
->method('isPropagationStopped') |
|
|
|
|
111
|
|
|
->willReturn(true); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
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.