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
|
|
|
final class ListenerCollectionTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
public function testImplementsListenerCollectionInterface(): void |
14
|
|
|
{ |
15
|
|
|
$collection = new ListenerCollection(); |
16
|
|
|
|
17
|
|
|
$this->assertInstanceOf(ListenerCollectionInterface::class, $collection); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function testGetIteratorWillReturnCloneOfListenerQueue(): void |
21
|
|
|
{ |
22
|
|
|
$collection = new ListenerCollection(); |
23
|
|
|
|
24
|
|
|
$listeners = [ |
25
|
|
|
'Foo' => static function () { |
26
|
|
|
return 'Foo'; |
27
|
|
|
}, |
28
|
|
|
'Bar' => static function () { |
29
|
|
|
return 'Bar'; |
30
|
|
|
}, |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
$collection->addListeners($listeners); |
34
|
|
|
|
35
|
|
|
$results = []; |
36
|
|
|
foreach ($collection as $listener) { |
37
|
|
|
$results[] = $listener(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$this->assertSame(['Foo', 'Bar'], $results); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testCountWillReturnIntegerMatchingTheNumberOfEventListeners(): void |
44
|
|
|
{ |
45
|
|
|
$collection = new ListenerCollection(); |
46
|
|
|
|
47
|
|
|
/** @var callable[] $listeners */ |
48
|
|
|
$listeners = [ |
49
|
|
|
static function () { |
50
|
|
|
}, |
51
|
|
|
static function () { |
52
|
|
|
}, |
53
|
|
|
static function () { |
54
|
|
|
}, |
55
|
|
|
static function () { |
56
|
|
|
}, |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
$collection->addListeners($listeners); |
60
|
|
|
|
61
|
|
|
$this->assertSame(count($listeners), $collection->count()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testEventListenersCanBeAddedViaConstructor(): void |
65
|
|
|
{ |
66
|
|
|
$expected = [ |
67
|
|
|
'foo', |
68
|
|
|
'bar', |
69
|
|
|
'baz', |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
$listeners = [ |
73
|
|
|
static function () { |
74
|
|
|
return 'foo'; |
75
|
|
|
}, |
76
|
|
|
static function () { |
77
|
|
|
return 'bar'; |
78
|
|
|
}, |
79
|
|
|
static function () { |
80
|
|
|
return 'baz'; |
81
|
|
|
}, |
82
|
|
|
]; |
83
|
|
|
|
84
|
|
|
$collection = new ListenerCollection($listeners); |
85
|
|
|
|
86
|
|
|
$results = []; |
87
|
|
|
foreach ($collection as $listener) { |
88
|
|
|
$results[] = $listener(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$this->assertSame($expected, $results); |
92
|
|
|
$this->assertSame(count($listeners), $collection->count()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testListenerPriorities(): void |
96
|
|
|
{ |
97
|
|
|
$listeners = [ |
98
|
|
|
static function () { |
99
|
|
|
return 5; |
100
|
|
|
}, |
101
|
|
|
static function () { |
102
|
|
|
return 1; |
103
|
|
|
}, |
104
|
|
|
static function () { |
105
|
|
|
return 3; |
106
|
|
|
}, |
107
|
|
|
static function () { |
108
|
|
|
return 7; |
109
|
|
|
}, |
110
|
|
|
static function () { |
111
|
|
|
return 4; |
112
|
|
|
}, |
113
|
|
|
static function () { |
114
|
|
|
return 8; |
115
|
|
|
}, |
116
|
|
|
static function () { |
117
|
|
|
return 6; |
118
|
|
|
}, |
119
|
|
|
static function () { |
120
|
|
|
return 2; |
121
|
|
|
}, |
122
|
|
|
]; |
123
|
|
|
|
124
|
|
|
$collection = new ListenerCollection(); |
125
|
|
|
|
126
|
|
|
$collection->addListener($listeners[0], 300); // 5 |
127
|
|
|
$collection->addListener($listeners[1], 700); // 1 |
128
|
|
|
$collection->addListener($listeners[2], 500); // 3 |
129
|
|
|
$collection->addListener($listeners[3], 101); // 7 |
130
|
|
|
$collection->addListener($listeners[4], 400); // 4 |
131
|
|
|
$collection->addListener($listeners[5], 100); // 8 |
132
|
|
|
$collection->addListener($listeners[6], 200); // 6 |
133
|
|
|
$collection->addListener($listeners[7], 600); // 2 |
134
|
|
|
|
135
|
|
|
$results = []; |
136
|
|
|
foreach ($collection as $item) { |
137
|
|
|
$results[] = $item(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$this->assertSame([1, 2, 3, 4, 5, 6, 7, 8], $results); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function testListenerPrioritiesRespectNaturalOrderWhenPrioritiesAreTheSame(): void |
144
|
|
|
{ |
145
|
|
|
$listeners = [ |
146
|
|
|
static function () { |
147
|
|
|
return 5; |
148
|
|
|
}, |
149
|
|
|
static function () { |
150
|
|
|
return 1; |
151
|
|
|
}, |
152
|
|
|
static function () { |
153
|
|
|
return 3; |
154
|
|
|
}, |
155
|
|
|
static function () { |
156
|
|
|
return 7; |
157
|
|
|
}, |
158
|
|
|
static function () { |
159
|
|
|
return 4; |
160
|
|
|
}, |
161
|
|
|
static function () { |
162
|
|
|
return 8; |
163
|
|
|
}, |
164
|
|
|
static function () { |
165
|
|
|
return 6; |
166
|
|
|
}, |
167
|
|
|
static function () { |
168
|
|
|
return 2; |
169
|
|
|
}, |
170
|
|
|
]; |
171
|
|
|
|
172
|
|
|
$collection = new ListenerCollection(); |
173
|
|
|
|
174
|
|
|
$collection->addListener($listeners[0]); // 5 |
175
|
|
|
$collection->addListener($listeners[1]); // 1 |
176
|
|
|
$collection->addListener($listeners[2]); // 3 |
177
|
|
|
$collection->addListener($listeners[3]); // 7 |
178
|
|
|
$collection->addListener($listeners[4]); // 4 |
179
|
|
|
$collection->addListener($listeners[5]); // 8 |
180
|
|
|
$collection->addListener($listeners[6]); // 6 |
181
|
|
|
$collection->addListener($listeners[7]); // 2 |
182
|
|
|
|
183
|
|
|
$results = []; |
184
|
|
|
foreach ($collection as $item) { |
185
|
|
|
$results[] = $item(); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
$this->assertSame([5, 1, 3, 7, 4, 8, 6, 2], $results); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|