|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use Suricate\EventDispatcher; |
|
4
|
|
|
|
|
5
|
|
|
require_once 'stubs/Event.php'; |
|
6
|
|
|
require_once 'stubs/EventListener1.php'; |
|
7
|
|
|
require_once 'stubs/EventListener2.php'; |
|
8
|
|
|
require_once 'stubs/EventListener3.php'; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @SuppressWarnings("StaticAccess") |
|
12
|
|
|
*/ |
|
13
|
|
|
class EventDispatcherTest extends \PHPUnit\Framework\TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
public function testConfigure() |
|
16
|
|
|
{ |
|
17
|
|
|
$parameters = [ |
|
18
|
|
|
[ |
|
19
|
|
|
'event' => 'my.event', |
|
20
|
|
|
'listeners' => ['listener1', 'listener2', 'listener3|50'] |
|
21
|
|
|
] |
|
22
|
|
|
]; |
|
23
|
|
|
$dispatcher = new EventDispatcher(); |
|
24
|
|
|
$dispatcher->configure($parameters); |
|
25
|
|
|
|
|
26
|
|
|
$reflector = new ReflectionClass(get_class($dispatcher)); |
|
27
|
|
|
$property = $reflector->getProperty('listeners'); |
|
28
|
|
|
$property->setAccessible(true); |
|
29
|
|
|
|
|
30
|
|
|
$result = [ |
|
31
|
|
|
'my.event' => [0 => ['listener1', 'listener2'], 50 => ['listener3']] |
|
32
|
|
|
]; |
|
33
|
|
|
$this->assertSame($result, $property->getValue($dispatcher)); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function testAddListener() |
|
37
|
|
|
{ |
|
38
|
|
|
$dispatcher = new EventDispatcher(); |
|
39
|
|
|
|
|
40
|
|
|
$reflector = new ReflectionClass(get_class($dispatcher)); |
|
41
|
|
|
$property = $reflector->getProperty('listeners'); |
|
42
|
|
|
$property->setAccessible(true); |
|
43
|
|
|
$this->assertSame([], $property->getValue($dispatcher)); |
|
44
|
|
|
|
|
45
|
|
|
$dispatcher->addListener('dummy.event', '\MyProject\MyListener', 12); |
|
46
|
|
|
$result = ['dummy.event' => [12 => ['\MyProject\MyListener']]]; |
|
47
|
|
|
$this->assertSame($result, $property->getValue($dispatcher)); |
|
48
|
|
|
|
|
49
|
|
|
$dispatcher->addListener( |
|
50
|
|
|
'dummy.event', |
|
51
|
|
|
'\MyProject\MySecondListener', |
|
52
|
|
|
10 |
|
53
|
|
|
); |
|
54
|
|
|
$result = [ |
|
55
|
|
|
'dummy.event' => [ |
|
56
|
|
|
12 => ['\MyProject\MyListener'], |
|
57
|
|
|
10 => ['\MyProject\MySecondListener'] |
|
58
|
|
|
] |
|
59
|
|
|
]; |
|
60
|
|
|
$this->assertSame($result, $property->getValue($dispatcher)); |
|
61
|
|
|
|
|
62
|
|
|
$dispatcher->addListener('TestEvent', '\MyProject\MyThirdListener', 12); |
|
63
|
|
|
$result = [ |
|
64
|
|
|
'dummy.event' => [ |
|
65
|
|
|
12 => ['\MyProject\MyListener'], |
|
66
|
|
|
10 => ['\MyProject\MySecondListener'] |
|
67
|
|
|
], |
|
68
|
|
|
'my.test.event' => [12 => ['\MyProject\MyThirdListener']] |
|
69
|
|
|
]; |
|
70
|
|
|
$this->assertSame($result, $property->getValue($dispatcher)); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function testFire() |
|
74
|
|
|
{ |
|
75
|
|
|
$dispatcher = new EventDispatcher(); |
|
76
|
|
|
$dispatcher->addListener('test.event', 'EventListener1'); |
|
77
|
|
|
$dispatcher->addListener('test.event', 'EventListener2'); |
|
78
|
|
|
$dispatcher->fire('test.event', 'payload_string'); |
|
79
|
|
|
$this->expectOutputString( |
|
80
|
|
|
"payload for listerner1 is : payload_string\npayload for listerner2 is : payload_string\n" |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function testFireObjet() |
|
85
|
|
|
{ |
|
86
|
|
|
$dispatcher = new EventDispatcher(); |
|
87
|
|
|
$dispatcher->addListener('my.test.event', 'EventListener3'); |
|
88
|
|
|
$dispatcher->addListener('test.event', 'EventListener2'); |
|
89
|
|
|
$dispatcher->fire(new TestEvent()); |
|
90
|
|
|
$this->expectOutputString("payload for listerner3 is : lorem ipsum\n"); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function testFireException() |
|
94
|
|
|
{ |
|
95
|
|
|
$dispatcher = new EventDispatcher(); |
|
96
|
|
|
$this->expectException('InvalidArgumentException'); |
|
97
|
|
|
|
|
98
|
|
|
$dispatcher->fire(new \Stdclass()); |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function testGetImpactedListeners() |
|
102
|
|
|
{ |
|
103
|
|
|
$dispatcher = new EventDispatcher(); |
|
104
|
|
|
|
|
105
|
|
|
$dispatcher->addListener('dummy.event', '\MyProject\MyListener', 12); |
|
106
|
|
|
$dispatcher->addListener('dummy.event', '\MyProject\MyListener2', 10); |
|
107
|
|
|
$dispatcher->addListener('another.event', '\MyProject\MyListener3', 12); |
|
108
|
|
|
|
|
109
|
|
|
$reflection = new \ReflectionClass(get_class($dispatcher)); |
|
110
|
|
|
$method = $reflection->getMethod('getImpactedListeners'); |
|
111
|
|
|
$method->setAccessible(true); |
|
112
|
|
|
$methodResult = $method->invoke($dispatcher, 'dummy.event'); |
|
113
|
|
|
|
|
114
|
|
|
$this->assertSame( |
|
115
|
|
|
['\MyProject\MyListener2', '\MyProject\MyListener'], |
|
116
|
|
|
$methodResult |
|
117
|
|
|
); |
|
118
|
|
|
|
|
119
|
|
|
$methodResult = $method->invoke($dispatcher, 'dummy.event'); |
|
120
|
|
|
$this->assertSame( |
|
121
|
|
|
['\MyProject\MyListener2', '\MyProject\MyListener'], |
|
122
|
|
|
$methodResult |
|
123
|
|
|
); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function testSortListeners() |
|
127
|
|
|
{ |
|
128
|
|
|
$dispatcher = new EventDispatcher(); |
|
129
|
|
|
|
|
130
|
|
|
$dispatcher->addListener('dummy.event', '\MyProject\MyListener', 12); |
|
131
|
|
|
$dispatcher->addListener('dummy.event', '\MyProject\MyListener2', 10); |
|
132
|
|
|
$dispatcher->addListener('another.event', '\MyProject\MyListener3', 12); |
|
133
|
|
|
|
|
134
|
|
|
$reflection = new \ReflectionClass(get_class($dispatcher)); |
|
135
|
|
|
$method = $reflection->getMethod('sortListeners'); |
|
136
|
|
|
$method->setAccessible(true); |
|
137
|
|
|
$method->invoke($dispatcher, 'dummy.event'); |
|
138
|
|
|
|
|
139
|
|
|
$property = $reflection->getProperty('sortedListeners'); |
|
140
|
|
|
$property->setAccessible(true); |
|
141
|
|
|
|
|
142
|
|
|
$result = [ |
|
143
|
|
|
'dummy.event' => ['\MyProject\MyListener2', '\MyProject\MyListener'] |
|
144
|
|
|
]; |
|
145
|
|
|
$this->assertEquals($result, $property->getValue($dispatcher)); |
|
146
|
|
|
|
|
147
|
|
|
$method->invoke($dispatcher, 'unknown.event'); |
|
148
|
|
|
$this->assertEquals($result, $property->getValue($dispatcher)); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|