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