1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Onoi\EventDispatcher\Tests; |
4
|
|
|
|
5
|
|
|
use Onoi\EventDispatcher\Dispatcher\GenericEventDispatcher; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @covers \Onoi\EventDispatcher\Dispatcher\GenericEventDispatcher |
9
|
|
|
* |
10
|
|
|
* @group onoi-event-dispatcher |
11
|
|
|
* |
12
|
|
|
* @license GNU GPL v2+ |
13
|
|
|
* @since 1.0 |
14
|
|
|
* |
15
|
|
|
* @author mwjames |
16
|
|
|
*/ |
17
|
|
|
class GenericEventDispatcherTest extends \PHPUnit_Framework_TestCase { |
18
|
|
|
|
19
|
|
|
public function testCanConstruct() { |
20
|
|
|
|
21
|
|
|
$this->assertInstanceOf( |
22
|
|
|
'\Onoi\EventDispatcher\EventDispatcher', |
23
|
|
|
new GenericEventDispatcher() |
24
|
|
|
); |
25
|
|
|
|
26
|
|
|
$this->assertInstanceOf( |
27
|
|
|
'\Onoi\EventDispatcher\Dispatcher\GenericEventDispatcher', |
28
|
|
|
new GenericEventDispatcher() |
29
|
|
|
); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testTryAddingListenerUsingInvalidEventIdentifierThrowsException() { |
33
|
|
|
|
34
|
|
|
$instance = new GenericEventDispatcher(); |
35
|
|
|
|
36
|
|
|
$eventListener = $this->getMockBuilder( '\Onoi\EventDispatcher\EventListener' ) |
37
|
|
|
->disableOriginalConstructor() |
38
|
|
|
->getMock(); |
39
|
|
|
|
40
|
|
|
$this->setExpectedException( 'InvalidArgumentException' ); |
41
|
|
|
$instance->addListener( new \stdClass, $eventListener ); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testAddListener() { |
45
|
|
|
|
46
|
|
|
$instance = new GenericEventDispatcher(); |
47
|
|
|
|
48
|
|
|
$eventListener = $this->getMockBuilder( '\Onoi\EventDispatcher\EventListener' ) |
49
|
|
|
->disableOriginalConstructor() |
50
|
|
|
->getMock(); |
51
|
|
|
|
52
|
|
|
$instance->addListener( 'foo', $eventListener ); |
53
|
|
|
|
54
|
|
|
$this->assertTrue( |
55
|
|
|
$instance->hasEvent( 'FOO' ) |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testRemoveSpecificListener() { |
60
|
|
|
|
61
|
|
|
$instance = new GenericEventDispatcher(); |
62
|
|
|
|
63
|
|
|
$eventListener = $this->getMockBuilder( '\Onoi\EventDispatcher\EventListener' ) |
64
|
|
|
->disableOriginalConstructor() |
65
|
|
|
->getMock(); |
66
|
|
|
|
67
|
|
|
$genericCallbackEventListener = $this->getMockBuilder( '\Onoi\EventDispatcher\Listener\GenericCallbackEventListener' ) |
68
|
|
|
->disableOriginalConstructor() |
69
|
|
|
->getMock(); |
70
|
|
|
|
71
|
|
|
$instance->addListener( 'foo', $eventListener ); |
72
|
|
|
$instance->addListener( 'foo', $genericCallbackEventListener ); |
73
|
|
|
|
74
|
|
|
$this->assertTrue( |
75
|
|
|
$instance->hasEvent( 'FOO' ) |
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
$instance->removeListener( 'foo', $eventListener ); |
79
|
|
|
|
80
|
|
|
$this->assertTrue( |
81
|
|
|
$instance->hasEvent( 'FOO' ) |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
$instance->removeListener( 'foo', $genericCallbackEventListener ); |
85
|
|
|
|
86
|
|
|
$this->assertFalse( |
87
|
|
|
$instance->hasEvent( 'FOO' ) |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testRemoveAllListenerForSpecificEvent() { |
92
|
|
|
|
93
|
|
|
$instance = new GenericEventDispatcher(); |
94
|
|
|
|
95
|
|
|
$eventListener = $this->getMockBuilder( '\Onoi\EventDispatcher\EventListener' ) |
96
|
|
|
->disableOriginalConstructor() |
97
|
|
|
->getMock(); |
98
|
|
|
|
99
|
|
|
$genericCallbackEventListener = $this->getMockBuilder( '\Onoi\EventDispatcher\Listener\GenericCallbackEventListener' ) |
100
|
|
|
->disableOriginalConstructor() |
101
|
|
|
->getMock(); |
102
|
|
|
|
103
|
|
|
$instance->addListener( 'foo', $eventListener ); |
104
|
|
|
$instance->addListener( 'foo', $genericCallbackEventListener ); |
105
|
|
|
|
106
|
|
|
$this->assertTrue( |
107
|
|
|
$instance->hasEvent( 'FOO' ) |
108
|
|
|
); |
109
|
|
|
|
110
|
|
|
$instance->removeListener( 'foo' ); |
111
|
|
|
|
112
|
|
|
$this->assertFalse( |
113
|
|
|
$instance->hasEvent( 'FOO' ) |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testTryRemovalOfListenersForUnknownEvent() { |
118
|
|
|
|
119
|
|
|
$instance = new GenericEventDispatcher(); |
120
|
|
|
|
121
|
|
|
$eventListener = $this->getMockBuilder( '\Onoi\EventDispatcher\EventListener' ) |
122
|
|
|
->disableOriginalConstructor() |
123
|
|
|
->getMock(); |
124
|
|
|
|
125
|
|
|
$instance->addListener( 'foo', $eventListener ); |
126
|
|
|
|
127
|
|
|
$instance->removeListener( 'bar', $eventListener ); |
128
|
|
|
|
129
|
|
|
$this->assertTrue( |
130
|
|
|
$instance->hasEvent( 'FOO' ) |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function testDispatchEvent() { |
135
|
|
|
|
136
|
|
|
$instance = new GenericEventDispatcher(); |
137
|
|
|
|
138
|
|
|
$eventListener = $this->getMockBuilder( '\Onoi\EventDispatcher\EventListener' ) |
139
|
|
|
->disableOriginalConstructor() |
140
|
|
|
->getMock(); |
141
|
|
|
|
142
|
|
|
$eventListener->expects( $this->once() ) |
143
|
|
|
->method( 'execute' ); |
144
|
|
|
|
145
|
|
|
$eventListener->expects( $this->once() ) |
146
|
|
|
->method( 'isPropagationStopped' ); |
147
|
|
|
|
148
|
|
|
$instance->addListener( 'foo', $eventListener ); |
149
|
|
|
$instance->dispatch( 'foo' ); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function testDispatchEventWithContextToOverrideListenerPropagationStopState() { |
153
|
|
|
|
154
|
|
|
$instance = new GenericEventDispatcher(); |
155
|
|
|
|
156
|
|
|
$dispatchContext = $this->getMockBuilder( '\Onoi\EventDispatcher\DispatchContext' ) |
157
|
|
|
->disableOriginalConstructor() |
158
|
|
|
->getMock(); |
159
|
|
|
|
160
|
|
|
$dispatchContext->expects( $this->once() ) |
161
|
|
|
->method( 'isPropagationStopped' ) |
162
|
|
|
->will( $this->returnValue( true ) ); |
163
|
|
|
|
164
|
|
|
$eventListener = $this->getMockBuilder( '\Onoi\EventDispatcher\EventListener' ) |
165
|
|
|
->disableOriginalConstructor() |
166
|
|
|
->getMock(); |
167
|
|
|
|
168
|
|
|
$eventListener->expects( $this->once() ) |
169
|
|
|
->method( 'execute' ) |
170
|
|
|
->with( $this->identicalTo( $dispatchContext ) ); |
171
|
|
|
|
172
|
|
|
$eventListener->expects( $this->once() ) |
173
|
|
|
->method( 'isPropagationStopped' ) |
174
|
|
|
->will( $this->returnValue( false ) ); |
175
|
|
|
|
176
|
|
|
$instance->addListener( 'foo', $eventListener ); |
177
|
|
|
|
178
|
|
|
$instance->dispatch( 'foo', $dispatchContext ); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function testDispatchFromListenerCollection() { |
182
|
|
|
|
183
|
|
|
$instance = new GenericEventDispatcher(); |
184
|
|
|
|
185
|
|
|
$dispatchContext = $this->getMockBuilder( '\Onoi\EventDispatcher\DispatchContext' ) |
186
|
|
|
->disableOriginalConstructor() |
187
|
|
|
->getMock(); |
188
|
|
|
|
189
|
|
|
$eventListener = $this->getMockBuilder( '\Onoi\EventDispatcher\EventListener' ) |
190
|
|
|
->disableOriginalConstructor() |
191
|
|
|
->getMock(); |
192
|
|
|
|
193
|
|
|
$eventListener->expects( $this->once() ) |
194
|
|
|
->method( 'execute' ) |
195
|
|
|
->with( $this->identicalTo( $dispatchContext ) ); |
196
|
|
|
|
197
|
|
|
$eventListenerCollection = $this->getMockBuilder( '\Onoi\EventDispatcher\EventListenerCollection' ) |
198
|
|
|
->disableOriginalConstructor() |
199
|
|
|
->getMock(); |
200
|
|
|
|
201
|
|
|
$eventListenerCollection->expects( $this->once() ) |
202
|
|
|
->method( 'getCollection' ) |
203
|
|
|
->will( $this->returnValue( |
204
|
|
|
array( |
205
|
|
|
'foo' => array( $eventListener ), |
206
|
|
|
'bar' => array( $eventListener ) ) ) ); |
207
|
|
|
|
208
|
|
|
$instance->addListenerCollection( $eventListenerCollection ); |
209
|
|
|
|
210
|
|
|
$this->assertTrue( |
211
|
|
|
$instance->hasEvent( 'FOO' ) |
212
|
|
|
); |
213
|
|
|
|
214
|
|
|
$this->assertTrue( |
215
|
|
|
$instance->hasEvent( 'bAr' ) |
216
|
|
|
); |
217
|
|
|
|
218
|
|
|
$instance->dispatch( 'foo', $dispatchContext ); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function testDispatchListenerWithArrayContext() { |
222
|
|
|
|
223
|
|
|
$instance = new GenericEventDispatcher(); |
224
|
|
|
|
225
|
|
|
$eventListener = $this->getMockBuilder( '\Onoi\EventDispatcher\EventListener' ) |
226
|
|
|
->disableOriginalConstructor() |
227
|
|
|
->getMock(); |
228
|
|
|
|
229
|
|
|
$eventListener->expects( $this->once() ) |
230
|
|
|
->method( 'execute' ) |
231
|
|
|
->with( $this->callback( function( $dispatchContext ){ return $dispatchContext->get( 'Bar' ) == 123; } ) ); |
232
|
|
|
|
233
|
|
|
$instance->addListener( 'FOO', $eventListener ); |
234
|
|
|
$instance->dispatch( 'foo', [ 'Bar' => 123 ] ); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
public function testMissingListenerForEventThrowsException() { |
238
|
|
|
|
239
|
|
|
$instance = new GenericEventDispatcher(); |
240
|
|
|
$instance->throwOnMissingEvent( true ); |
241
|
|
|
|
242
|
|
|
$this->setExpectedException( '\Onoi\EventDispatcher\Exception\EventNotDispatchableException' ); |
243
|
|
|
$instance->dispatch( 'foo' ); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
public function testRegisterNonTraversableCollectionThrowsException() { |
247
|
|
|
|
248
|
|
|
$instance = new GenericEventDispatcher(); |
249
|
|
|
|
250
|
|
|
$eventListenerCollection = $this->getMockBuilder( '\Onoi\EventDispatcher\EventListenerCollection' ) |
251
|
|
|
->disableOriginalConstructor() |
252
|
|
|
->getMock(); |
253
|
|
|
|
254
|
|
|
$eventListenerCollection->expects( $this->once() ) |
255
|
|
|
->method( 'getCollection' ) |
256
|
|
|
->will( $this->returnValue( false ) ); |
257
|
|
|
|
258
|
|
|
$this->setExpectedException( 'RuntimeException' ); |
259
|
|
|
$instance->addListenerCollection( $eventListenerCollection ); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
} |
263
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: