1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ArpTest\EventDispatcher\Listener; |
6
|
|
|
|
7
|
|
|
use Arp\EventDispatcher\Listener\ListenerCollection; |
8
|
|
|
use Arp\EventDispatcher\Listener\ListenerCollectionInterface; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Alex Patterson <[email protected]> |
13
|
|
|
* @package ArpTest\EventDispatcher\Listener |
14
|
|
|
*/ |
15
|
|
|
final class ListenerCollectionTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Assert that the listener collection implements ListenerCollectionInterface. |
19
|
|
|
* |
20
|
|
|
* @covers \Arp\EventDispatcher\Listener\ListenerCollection |
21
|
|
|
*/ |
22
|
|
|
public function testImplementsListenerCollectionInterface(): void |
23
|
|
|
{ |
24
|
|
|
$collection = new ListenerCollection(); |
25
|
|
|
|
26
|
|
|
$this->assertInstanceOf(ListenerCollectionInterface::class, $collection); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Assert that getIterator() will return a clone of the listener priority queue. |
31
|
|
|
* |
32
|
|
|
* @covers \Arp\EventDispatcher\Listener\ListenerCollection::getIterator |
33
|
|
|
*/ |
34
|
|
|
public function testGetIteratorWillReturnCloneOfListenerQueue(): void |
35
|
|
|
{ |
36
|
|
|
$collection = new ListenerCollection(); |
37
|
|
|
|
38
|
|
|
$listeners = [ |
39
|
|
|
'Foo' => static function () { |
40
|
|
|
return 'Foo'; |
41
|
|
|
}, |
42
|
|
|
'Bar' => static function () { |
43
|
|
|
return 'Bar'; |
44
|
|
|
}, |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
$collection->addListeners($listeners, 1); |
48
|
|
|
|
49
|
|
|
$results = []; |
50
|
|
|
foreach ($collection as $index => $listener) { |
51
|
|
|
$results[] = $listener(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$this->assertSame(['Foo', 'Bar'], $results); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Assert that the count() method will return an integer matching the number of listeners added to the collection. |
59
|
|
|
* |
60
|
|
|
* @covers \Arp\EventDispatcher\Listener\ListenerCollection::addListeners |
61
|
|
|
*/ |
62
|
|
|
public function testCountWillReturnIntegerMatchingTheNumberOfEventListeners(): void |
63
|
|
|
{ |
64
|
|
|
$collection = new ListenerCollection(); |
65
|
|
|
|
66
|
|
|
/** @var callable[] $listeners */ |
67
|
|
|
$listeners = [ |
68
|
|
|
static function () { |
69
|
|
|
}, |
70
|
|
|
static function () { |
71
|
|
|
}, |
72
|
|
|
static function () { |
73
|
|
|
}, |
74
|
|
|
static function () { |
75
|
|
|
}, |
76
|
|
|
]; |
77
|
|
|
|
78
|
|
|
$collection->addListeners($listeners); |
79
|
|
|
|
80
|
|
|
$this->assertSame(count($listeners), $collection->count()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Assert that we can add a collection of event listeners via the __construct. |
85
|
|
|
* |
86
|
|
|
* @covers \Arp\EventDispatcher\Listener\ListenerCollection |
87
|
|
|
*/ |
88
|
|
|
public function testEventListenersCanBeAddedViaConstructor(): void |
89
|
|
|
{ |
90
|
|
|
$expected = [ |
91
|
|
|
'foo', |
92
|
|
|
'bar', |
93
|
|
|
'baz', |
94
|
|
|
]; |
95
|
|
|
|
96
|
|
|
$listeners = [ |
97
|
|
|
static function () { |
98
|
|
|
return 'foo'; |
99
|
|
|
}, |
100
|
|
|
static function () { |
101
|
|
|
return 'bar'; |
102
|
|
|
}, |
103
|
|
|
static function () { |
104
|
|
|
return 'baz'; |
105
|
|
|
}, |
106
|
|
|
]; |
107
|
|
|
|
108
|
|
|
$collection = new ListenerCollection($listeners); |
109
|
|
|
|
110
|
|
|
$results = []; |
111
|
|
|
foreach ($collection as $index => $listener) { |
112
|
|
|
$results[] = $listener(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$this->assertSame($expected, $results); |
116
|
|
|
$this->assertSame(count($listeners), $collection->count()); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Assert that the listeners priorities are respected, regardless of when the listener is registered with the |
121
|
|
|
* collection. |
122
|
|
|
* |
123
|
|
|
* @covers \Arp\EventDispatcher\Listener\ListenerCollection::addListener |
124
|
|
|
* @covers \Arp\EventDispatcher\Listener\ListenerCollection::addListeners |
125
|
|
|
*/ |
126
|
|
|
public function testListenerPriorities(): void |
127
|
|
|
{ |
128
|
|
|
$listeners = [ |
129
|
|
|
static function () { |
130
|
|
|
return 5; |
131
|
|
|
}, |
132
|
|
|
static function () { |
133
|
|
|
return 1; |
134
|
|
|
}, |
135
|
|
|
static function () { |
136
|
|
|
return 3; |
137
|
|
|
}, |
138
|
|
|
static function () { |
139
|
|
|
return 7; |
140
|
|
|
}, |
141
|
|
|
static function () { |
142
|
|
|
return 4; |
143
|
|
|
}, |
144
|
|
|
static function () { |
145
|
|
|
return 8; |
146
|
|
|
}, |
147
|
|
|
static function () { |
148
|
|
|
return 6; |
149
|
|
|
}, |
150
|
|
|
static function () { |
151
|
|
|
return 2; |
152
|
|
|
}, |
153
|
|
|
]; |
154
|
|
|
|
155
|
|
|
$collection = new ListenerCollection(); |
156
|
|
|
|
157
|
|
|
$collection->addListener($listeners[0], 300); // 5 |
158
|
|
|
$collection->addListener($listeners[1], 700); // 1 |
159
|
|
|
$collection->addListener($listeners[2], 500); // 3 |
160
|
|
|
$collection->addListener($listeners[3], 101); // 7 |
161
|
|
|
$collection->addListener($listeners[4], 400); // 4 |
162
|
|
|
$collection->addListener($listeners[5], 100); // 8 |
163
|
|
|
$collection->addListener($listeners[6], 200); // 6 |
164
|
|
|
$collection->addListener($listeners[7], 600); // 2 |
165
|
|
|
|
166
|
|
|
$results = []; |
167
|
|
|
foreach ($collection as $item) { |
168
|
|
|
$results[] = $item(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$this->assertSame([1, 2, 3, 4, 5, 6, 7, 8], $results); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Assert that the listeners natural order is respected when provided with event listeners with the same |
176
|
|
|
* priorities. This means that the collection operates on a first in first out basis. |
177
|
|
|
* |
178
|
|
|
* @covers \Arp\EventDispatcher\Listener\ListenerCollection::addListener |
179
|
|
|
* @covers \Arp\EventDispatcher\Listener\ListenerCollection::addListeners |
180
|
|
|
*/ |
181
|
|
|
public function testListenerPrioritiesRespectNaturalOrderWhenPrioritiesAreTheSame(): void |
182
|
|
|
{ |
183
|
|
|
$listeners = [ |
184
|
|
|
static function () { |
185
|
|
|
return 5; |
186
|
|
|
}, |
187
|
|
|
static function () { |
188
|
|
|
return 1; |
189
|
|
|
}, |
190
|
|
|
static function () { |
191
|
|
|
return 3; |
192
|
|
|
}, |
193
|
|
|
static function () { |
194
|
|
|
return 7; |
195
|
|
|
}, |
196
|
|
|
static function () { |
197
|
|
|
return 4; |
198
|
|
|
}, |
199
|
|
|
static function () { |
200
|
|
|
return 8; |
201
|
|
|
}, |
202
|
|
|
static function () { |
203
|
|
|
return 6; |
204
|
|
|
}, |
205
|
|
|
static function () { |
206
|
|
|
return 2; |
207
|
|
|
}, |
208
|
|
|
]; |
209
|
|
|
|
210
|
|
|
$collection = new ListenerCollection(); |
211
|
|
|
|
212
|
|
|
$collection->addListener($listeners[0], 1); // 5 |
213
|
|
|
$collection->addListener($listeners[1], 1); // 1 |
214
|
|
|
$collection->addListener($listeners[2], 1); // 3 |
215
|
|
|
$collection->addListener($listeners[3], 1); // 7 |
216
|
|
|
$collection->addListener($listeners[4], 1); // 4 |
217
|
|
|
$collection->addListener($listeners[5], 1); // 8 |
218
|
|
|
$collection->addListener($listeners[6], 1); // 6 |
219
|
|
|
$collection->addListener($listeners[7], 1); // 2 |
220
|
|
|
|
221
|
|
|
$results = []; |
222
|
|
|
foreach ($collection as $item) { |
223
|
|
|
$results[] = $item(); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
$this->assertSame([5, 1, 3, 7, 4, 8, 6, 2], $results); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|