1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Evenement. |
5
|
|
|
* |
6
|
|
|
* (c) Igor Wiedler <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Evenement\Tests; |
13
|
|
|
|
14
|
|
|
use Evenement\EventEmitter; |
15
|
|
|
use InvalidArgumentException; |
16
|
|
|
use PHPUnit\Framework\TestCase; |
17
|
|
|
|
18
|
|
|
class EventEmitterTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
private $emitter; |
21
|
|
|
|
22
|
|
|
public function setUp() |
23
|
|
|
{ |
24
|
|
|
$this->emitter = new EventEmitter(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testAddListenerWithLambda() |
28
|
|
|
{ |
29
|
|
|
$this->emitter->on('foo', function () {}); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testAddListenerWithMethod() |
33
|
|
|
{ |
34
|
|
|
$listener = new Listener(); |
35
|
|
|
$this->emitter->on('foo', [$listener, 'onFoo']); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testAddListenerWithStaticMethod() |
39
|
|
|
{ |
40
|
|
|
$this->emitter->on('bar', ['Evenement\Tests\Listener', 'onBar']); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testAddListenerWithInvalidListener() |
44
|
|
|
{ |
45
|
|
|
try { |
46
|
|
|
$this->emitter->on('foo', 'not a callable'); |
47
|
|
|
$this->fail(); |
48
|
|
|
} catch (\Exception $e) { |
|
|
|
|
49
|
|
|
} catch (\TypeError $e) { |
|
|
|
|
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testOnce() |
54
|
|
|
{ |
55
|
|
|
$listenerCalled = 0; |
56
|
|
|
|
57
|
|
|
$this->emitter->once('foo', function () use (&$listenerCalled) { |
58
|
|
|
$listenerCalled++; |
59
|
|
|
}); |
60
|
|
|
|
61
|
|
|
$this->assertSame(0, $listenerCalled); |
62
|
|
|
|
63
|
|
|
$this->emitter->emit('foo'); |
64
|
|
|
|
65
|
|
|
$this->assertSame(1, $listenerCalled); |
66
|
|
|
|
67
|
|
|
$this->emitter->emit('foo'); |
68
|
|
|
|
69
|
|
|
$this->assertSame(1, $listenerCalled); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testOnceWithArguments() |
73
|
|
|
{ |
74
|
|
|
$capturedArgs = []; |
75
|
|
|
|
76
|
|
|
$this->emitter->once('foo', function ($a, $b) use (&$capturedArgs) { |
77
|
|
|
$capturedArgs = array($a, $b); |
78
|
|
|
}); |
79
|
|
|
|
80
|
|
|
$this->emitter->emit('foo', array('a', 'b')); |
81
|
|
|
|
82
|
|
|
$this->assertSame(array('a', 'b'), $capturedArgs); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testEmitWithoutArguments() |
86
|
|
|
{ |
87
|
|
|
$listenerCalled = false; |
88
|
|
|
|
89
|
|
|
$this->emitter->on('foo', function () use (&$listenerCalled) { |
90
|
|
|
$listenerCalled = true; |
91
|
|
|
}); |
92
|
|
|
|
93
|
|
|
$this->assertSame(false, $listenerCalled); |
94
|
|
|
$this->emitter->emit('foo'); |
95
|
|
|
$this->assertSame(true, $listenerCalled); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testEmitWithOneArgument() |
99
|
|
|
{ |
100
|
|
|
$test = $this; |
101
|
|
|
|
102
|
|
|
$listenerCalled = false; |
103
|
|
|
|
104
|
|
|
$this->emitter->on('foo', function ($value) use (&$listenerCalled, $test) { |
105
|
|
|
$listenerCalled = true; |
106
|
|
|
|
107
|
|
|
$test->assertSame('bar', $value); |
108
|
|
|
}); |
109
|
|
|
|
110
|
|
|
$this->assertSame(false, $listenerCalled); |
111
|
|
|
$this->emitter->emit('foo', ['bar']); |
112
|
|
|
$this->assertSame(true, $listenerCalled); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function testEmitWithTwoArguments() |
116
|
|
|
{ |
117
|
|
|
$test = $this; |
118
|
|
|
|
119
|
|
|
$listenerCalled = false; |
120
|
|
|
|
121
|
|
|
$this->emitter->on('foo', function ($arg1, $arg2) use (&$listenerCalled, $test) { |
122
|
|
|
$listenerCalled = true; |
123
|
|
|
|
124
|
|
|
$test->assertSame('bar', $arg1); |
125
|
|
|
$test->assertSame('baz', $arg2); |
126
|
|
|
}); |
127
|
|
|
|
128
|
|
|
$this->assertSame(false, $listenerCalled); |
129
|
|
|
$this->emitter->emit('foo', ['bar', 'baz']); |
130
|
|
|
$this->assertSame(true, $listenerCalled); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function testEmitWithNoListeners() |
134
|
|
|
{ |
135
|
|
|
$this->emitter->emit('foo'); |
136
|
|
|
$this->emitter->emit('foo', ['bar']); |
137
|
|
|
$this->emitter->emit('foo', ['bar', 'baz']); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function testEmitWithTwoListeners() |
141
|
|
|
{ |
142
|
|
|
$listenersCalled = 0; |
143
|
|
|
|
144
|
|
|
$this->emitter->on('foo', function () use (&$listenersCalled) { |
145
|
|
|
$listenersCalled++; |
146
|
|
|
}); |
147
|
|
|
|
148
|
|
|
$this->emitter->on('foo', function () use (&$listenersCalled) { |
149
|
|
|
$listenersCalled++; |
150
|
|
|
}); |
151
|
|
|
|
152
|
|
|
$this->assertSame(0, $listenersCalled); |
153
|
|
|
$this->emitter->emit('foo'); |
154
|
|
|
$this->assertSame(2, $listenersCalled); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function testRemoveListenerMatching() |
158
|
|
|
{ |
159
|
|
|
$listenersCalled = 0; |
160
|
|
|
|
161
|
|
|
$listener = function () use (&$listenersCalled) { |
162
|
|
|
$listenersCalled++; |
163
|
|
|
}; |
164
|
|
|
|
165
|
|
|
$this->emitter->on('foo', $listener); |
166
|
|
|
$this->emitter->removeListener('foo', $listener); |
167
|
|
|
|
168
|
|
|
$this->assertSame(0, $listenersCalled); |
169
|
|
|
$this->emitter->emit('foo'); |
170
|
|
|
$this->assertSame(0, $listenersCalled); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function testRemoveListenerNotMatching() |
174
|
|
|
{ |
175
|
|
|
$listenersCalled = 0; |
176
|
|
|
|
177
|
|
|
$listener = function () use (&$listenersCalled) { |
178
|
|
|
$listenersCalled++; |
179
|
|
|
}; |
180
|
|
|
|
181
|
|
|
$this->emitter->on('foo', $listener); |
182
|
|
|
$this->emitter->removeListener('bar', $listener); |
183
|
|
|
|
184
|
|
|
$this->assertSame(0, $listenersCalled); |
185
|
|
|
$this->emitter->emit('foo'); |
186
|
|
|
$this->assertSame(1, $listenersCalled); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function testRemoveAllListenersMatching() |
190
|
|
|
{ |
191
|
|
|
$listenersCalled = 0; |
192
|
|
|
|
193
|
|
|
$this->emitter->on('foo', function () use (&$listenersCalled) { |
194
|
|
|
$listenersCalled++; |
195
|
|
|
}); |
196
|
|
|
|
197
|
|
|
$this->emitter->removeAllListeners('foo'); |
198
|
|
|
|
199
|
|
|
$this->assertSame(0, $listenersCalled); |
200
|
|
|
$this->emitter->emit('foo'); |
201
|
|
|
$this->assertSame(0, $listenersCalled); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function testRemoveAllListenersNotMatching() |
205
|
|
|
{ |
206
|
|
|
$listenersCalled = 0; |
207
|
|
|
|
208
|
|
|
$this->emitter->on('foo', function () use (&$listenersCalled) { |
209
|
|
|
$listenersCalled++; |
210
|
|
|
}); |
211
|
|
|
|
212
|
|
|
$this->emitter->removeAllListeners('bar'); |
213
|
|
|
|
214
|
|
|
$this->assertSame(0, $listenersCalled); |
215
|
|
|
$this->emitter->emit('foo'); |
216
|
|
|
$this->assertSame(1, $listenersCalled); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public function testRemoveAllListenersWithoutArguments() |
220
|
|
|
{ |
221
|
|
|
$listenersCalled = 0; |
222
|
|
|
|
223
|
|
|
$this->emitter->on('foo', function () use (&$listenersCalled) { |
224
|
|
|
$listenersCalled++; |
225
|
|
|
}); |
226
|
|
|
|
227
|
|
|
$this->emitter->on('bar', function () use (&$listenersCalled) { |
228
|
|
|
$listenersCalled++; |
229
|
|
|
}); |
230
|
|
|
|
231
|
|
|
$this->emitter->removeAllListeners(); |
232
|
|
|
|
233
|
|
|
$this->assertSame(0, $listenersCalled); |
234
|
|
|
$this->emitter->emit('foo'); |
235
|
|
|
$this->emitter->emit('bar'); |
236
|
|
|
$this->assertSame(0, $listenersCalled); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
public function testCallablesClosure() |
240
|
|
|
{ |
241
|
|
|
$calledWith = null; |
242
|
|
|
|
243
|
|
|
$this->emitter->on('foo', function ($data) use (&$calledWith) { |
244
|
|
|
$calledWith = $data; |
245
|
|
|
}); |
246
|
|
|
|
247
|
|
|
$this->emitter->emit('foo', ['bar']); |
248
|
|
|
|
249
|
|
|
self::assertSame('bar', $calledWith); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
public function testCallablesClass() |
253
|
|
|
{ |
254
|
|
|
$listener = new Listener(); |
255
|
|
|
$this->emitter->on('foo', [$listener, 'onFoo']); |
256
|
|
|
|
257
|
|
|
$this->emitter->emit('foo', ['bar']); |
258
|
|
|
|
259
|
|
|
self::assertSame(['bar'], $listener->getData()); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
|
263
|
|
|
public function testCallablesClassInvoke() |
264
|
|
|
{ |
265
|
|
|
$listener = new Listener(); |
266
|
|
|
$this->emitter->on('foo', $listener); |
267
|
|
|
|
268
|
|
|
$this->emitter->emit('foo', ['bar']); |
269
|
|
|
|
270
|
|
|
self::assertSame(['bar'], $listener->getMagicData()); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
public function testCallablesStaticClass() |
274
|
|
|
{ |
275
|
|
|
$this->emitter->on('foo', '\Evenement\Tests\Listener::onBar'); |
276
|
|
|
|
277
|
|
|
$this->emitter->emit('foo', ['bar']); |
278
|
|
|
|
279
|
|
|
self::assertSame(['bar'], Listener::getStaticData()); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
public function testCallablesFunction() |
283
|
|
|
{ |
284
|
|
|
$this->emitter->on('foo', '\Evenement\Tests\setGlobalTestData'); |
285
|
|
|
|
286
|
|
|
$this->emitter->emit('foo', ['bar']); |
287
|
|
|
|
288
|
|
|
self::assertSame('bar', $GLOBALS['evenement-evenement-test-data']); |
289
|
|
|
|
290
|
|
|
unset($GLOBALS['evenement-evenement-test-data']); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
public function testListeners() |
294
|
|
|
{ |
295
|
|
|
$onA = function () {}; |
296
|
|
|
$onB = function () {}; |
297
|
|
|
$onC = function () {}; |
298
|
|
|
$onceA = function () {}; |
299
|
|
|
$onceB = function () {}; |
300
|
|
|
$onceC = function () {}; |
301
|
|
|
|
302
|
|
|
self::assertCount(0, $this->emitter->listeners('event')); |
303
|
|
|
$this->emitter->on('event', $onA); |
304
|
|
|
self::assertCount(1, $this->emitter->listeners('event')); |
305
|
|
|
self::assertSame([$onA], $this->emitter->listeners('event')); |
306
|
|
|
$this->emitter->once('event', $onceA); |
307
|
|
|
self::assertCount(2, $this->emitter->listeners('event')); |
308
|
|
|
self::assertSame([$onA, $onceA], $this->emitter->listeners('event')); |
309
|
|
|
$this->emitter->once('event', $onceB); |
310
|
|
|
self::assertCount(3, $this->emitter->listeners('event')); |
311
|
|
|
self::assertSame([$onA, $onceA, $onceB], $this->emitter->listeners('event')); |
312
|
|
|
$this->emitter->on('event', $onB); |
313
|
|
|
self::assertCount(4, $this->emitter->listeners('event')); |
314
|
|
|
self::assertSame([$onA, $onB, $onceA, $onceB], $this->emitter->listeners('event')); |
315
|
|
|
$this->emitter->removeListener('event', $onceA); |
316
|
|
|
self::assertCount(3, $this->emitter->listeners('event')); |
317
|
|
|
self::assertSame([$onA, $onB, $onceB], $this->emitter->listeners('event')); |
318
|
|
|
$this->emitter->once('event', $onceC); |
319
|
|
|
self::assertCount(4, $this->emitter->listeners('event')); |
320
|
|
|
self::assertSame([$onA, $onB, $onceB, $onceC], $this->emitter->listeners('event')); |
321
|
|
|
$this->emitter->on('event', $onC); |
322
|
|
|
self::assertCount(5, $this->emitter->listeners('event')); |
323
|
|
|
self::assertSame([$onA, $onB, $onC, $onceB, $onceC], $this->emitter->listeners('event')); |
324
|
|
|
$this->emitter->once('event', $onceA); |
325
|
|
|
self::assertCount(6, $this->emitter->listeners('event')); |
326
|
|
|
self::assertSame([$onA, $onB, $onC, $onceB, $onceC, $onceA], $this->emitter->listeners('event')); |
327
|
|
|
$this->emitter->removeListener('event', $onB); |
328
|
|
|
self::assertCount(5, $this->emitter->listeners('event')); |
329
|
|
|
self::assertSame([$onA, $onC, $onceB, $onceC, $onceA], $this->emitter->listeners('event')); |
330
|
|
|
$this->emitter->emit('event'); |
331
|
|
|
self::assertCount(2, $this->emitter->listeners('event')); |
332
|
|
|
self::assertSame([$onA, $onC], $this->emitter->listeners('event')); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
public function testOnceCallIsNotRemovedWhenWorkingOverOnceListeners() |
336
|
|
|
{ |
337
|
|
|
$aCalled = false; |
338
|
|
|
$aCallable = function () use (&$aCalled) { |
339
|
|
|
$aCalled = true; |
340
|
|
|
}; |
341
|
|
|
$bCalled = false; |
342
|
|
|
$bCallable = function () use (&$bCalled, $aCallable) { |
343
|
|
|
$bCalled = true; |
344
|
|
|
$this->emitter->once('event', $aCallable); |
345
|
|
|
}; |
346
|
|
|
$this->emitter->once('event', $bCallable); |
347
|
|
|
|
348
|
|
|
self::assertFalse($aCalled); |
349
|
|
|
self::assertFalse($bCalled); |
350
|
|
|
$this->emitter->emit('event'); |
351
|
|
|
|
352
|
|
|
self::assertFalse($aCalled); |
353
|
|
|
self::assertTrue($bCalled); |
354
|
|
|
$this->emitter->emit('event'); |
355
|
|
|
|
356
|
|
|
self::assertTrue($aCalled); |
357
|
|
|
self::assertTrue($bCalled); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
public function testEventNameMustBeStringOn() |
361
|
|
|
{ |
362
|
|
|
self::expectException(InvalidArgumentException::class); |
|
|
|
|
363
|
|
|
self::expectExceptionMessage('event name must not be null'); |
|
|
|
|
364
|
|
|
|
365
|
|
|
$this->emitter->on(null, function () {}); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
public function testEventNameMustBeStringOnce() |
369
|
|
|
{ |
370
|
|
|
self::expectException(InvalidArgumentException::class); |
|
|
|
|
371
|
|
|
self::expectExceptionMessage('event name must not be null'); |
|
|
|
|
372
|
|
|
|
373
|
|
|
$this->emitter->once(null, function () {}); |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
public function testEventNameMustBeStringRemoveListener() |
377
|
|
|
{ |
378
|
|
|
self::expectException(InvalidArgumentException::class); |
|
|
|
|
379
|
|
|
self::expectExceptionMessage('event name must not be null'); |
|
|
|
|
380
|
|
|
|
381
|
|
|
$this->emitter->removeListener(null, function () {}); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
public function testEventNameMustBeStringEmit() |
385
|
|
|
{ |
386
|
|
|
self::expectException(InvalidArgumentException::class); |
|
|
|
|
387
|
|
|
self::expectExceptionMessage('event name must not be null'); |
|
|
|
|
388
|
|
|
|
389
|
|
|
$this->emitter->emit(null); |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
public function testListenersGetAll() |
393
|
|
|
{ |
394
|
|
|
$a = function () {}; |
395
|
|
|
$b = function () {}; |
396
|
|
|
$c = function () {}; |
397
|
|
|
$d = function () {}; |
398
|
|
|
|
399
|
|
|
$this->emitter->once('event2', $c); |
400
|
|
|
$this->emitter->on('event', $a); |
401
|
|
|
$this->emitter->once('event', $b); |
402
|
|
|
$this->emitter->on('event', $c); |
403
|
|
|
$this->emitter->once('event', $d); |
404
|
|
|
|
405
|
|
|
self::assertSame( |
406
|
|
|
[ |
407
|
|
|
'event' => [ |
408
|
|
|
$a, |
409
|
|
|
$c, |
410
|
|
|
$b, |
411
|
|
|
$d, |
412
|
|
|
], |
413
|
|
|
'event2' => [ |
414
|
|
|
$c, |
415
|
|
|
], |
416
|
|
|
], |
417
|
|
|
$this->emitter->listeners() |
418
|
|
|
); |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
public function testOnceNestedCallRegression() |
422
|
|
|
{ |
423
|
|
|
$first = 0; |
424
|
|
|
$second = 0; |
425
|
|
|
|
426
|
|
|
$this->emitter->once('event', function () use (&$first, &$second) { |
427
|
|
|
$first++; |
428
|
|
|
$this->emitter->once('event', function () use (&$second) { |
429
|
|
|
$second++; |
430
|
|
|
}); |
431
|
|
|
$this->emitter->emit('event'); |
432
|
|
|
}); |
433
|
|
|
$this->emitter->emit('event'); |
434
|
|
|
|
435
|
|
|
self::assertSame(1, $first); |
436
|
|
|
self::assertSame(1, $second); |
437
|
|
|
} |
438
|
|
|
} |
439
|
|
|
|