| 13532 |
anikendra |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* ControllerTestCaseTest file
|
|
|
4 |
*
|
|
|
5 |
* Test Case for ControllerTestCase class
|
|
|
6 |
*
|
|
|
7 |
* CakePHP : Rapid Development Framework (http://cakephp.org)
|
|
|
8 |
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
9 |
*
|
|
|
10 |
* Licensed under The MIT License
|
|
|
11 |
* For full copyright and license information, please see the LICENSE.txt
|
|
|
12 |
* Redistributions of files must retain the above copyright notice.
|
|
|
13 |
*
|
|
|
14 |
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
15 |
* @link http://cakephp.org CakePHP Project
|
|
|
16 |
* @package Cake.Test.Case.Event
|
|
|
17 |
* @since CakePHP v 2.1
|
|
|
18 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
|
|
19 |
*/
|
|
|
20 |
|
|
|
21 |
App::uses('CakeEvent', 'Event');
|
|
|
22 |
App::uses('CakeEventManager', 'Event');
|
|
|
23 |
App::uses('CakeEventListener', 'Event');
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* Mock class used to test event dispatching
|
|
|
27 |
*
|
|
|
28 |
* @package Cake.Test.Case.Event
|
|
|
29 |
*/
|
|
|
30 |
class CakeEventTestListener {
|
|
|
31 |
|
|
|
32 |
public $callStack = array();
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* Test function to be used in event dispatching
|
|
|
36 |
*
|
|
|
37 |
* @return void
|
|
|
38 |
*/
|
|
|
39 |
public function listenerFunction() {
|
|
|
40 |
$this->callStack[] = __FUNCTION__;
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* Test function to be used in event dispatching
|
|
|
45 |
*
|
|
|
46 |
* @return void
|
|
|
47 |
*/
|
|
|
48 |
public function secondListenerFunction() {
|
|
|
49 |
$this->callStack[] = __FUNCTION__;
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* Auxiliary function to help in stopPropagation testing
|
|
|
54 |
*
|
|
|
55 |
* @param CakeEvent $event
|
|
|
56 |
* @return void
|
|
|
57 |
*/
|
|
|
58 |
public function stopListener($event) {
|
|
|
59 |
$event->stopPropagation();
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* Mock used for testing the subscriber objects
|
|
|
66 |
*
|
|
|
67 |
* @package Cake.Test.Case.Event
|
|
|
68 |
*/
|
|
|
69 |
class CustomTestEventListener extends CakeEventTestListener implements CakeEventListener {
|
|
|
70 |
|
|
|
71 |
public function implementedEvents() {
|
|
|
72 |
return array(
|
|
|
73 |
'fake.event' => 'listenerFunction',
|
|
|
74 |
'another.event' => array('callable' => 'secondListenerFunction', 'passParams' => true),
|
|
|
75 |
'multiple.handlers' => array(
|
|
|
76 |
array('callable' => 'listenerFunction'),
|
|
|
77 |
array('callable' => 'thirdListenerFunction')
|
|
|
78 |
)
|
|
|
79 |
);
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* Test function to be used in event dispatching
|
|
|
84 |
*
|
|
|
85 |
* @return void
|
|
|
86 |
*/
|
|
|
87 |
public function thirdListenerFunction() {
|
|
|
88 |
$this->callStack[] = __FUNCTION__;
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
/**
|
|
|
94 |
* Tests the CakeEventManager class functionality
|
|
|
95 |
*
|
|
|
96 |
*/
|
|
|
97 |
class CakeEventManagerTest extends CakeTestCase {
|
|
|
98 |
|
|
|
99 |
/**
|
|
|
100 |
* Tests the attach() method for a single event key in multiple queues
|
|
|
101 |
*
|
|
|
102 |
* @return void
|
|
|
103 |
*/
|
|
|
104 |
public function testAttachListeners() {
|
|
|
105 |
$manager = new CakeEventManager;
|
|
|
106 |
$manager->attach('fakeFunction', 'fake.event');
|
|
|
107 |
$expected = array(
|
|
|
108 |
array('callable' => 'fakeFunction', 'passParams' => false)
|
|
|
109 |
);
|
|
|
110 |
$this->assertEquals($expected, $manager->listeners('fake.event'));
|
|
|
111 |
|
|
|
112 |
$manager->attach('fakeFunction2', 'fake.event');
|
|
|
113 |
$expected[] = array('callable' => 'fakeFunction2', 'passParams' => false);
|
|
|
114 |
$this->assertEquals($expected, $manager->listeners('fake.event'));
|
|
|
115 |
|
|
|
116 |
$manager->attach('inQ5', 'fake.event', array('priority' => 5));
|
|
|
117 |
$manager->attach('inQ1', 'fake.event', array('priority' => 1));
|
|
|
118 |
$manager->attach('otherInQ5', 'fake.event', array('priority' => 5));
|
|
|
119 |
|
|
|
120 |
$expected = array_merge(
|
|
|
121 |
array(
|
|
|
122 |
array('callable' => 'inQ1', 'passParams' => false),
|
|
|
123 |
array('callable' => 'inQ5', 'passParams' => false),
|
|
|
124 |
array('callable' => 'otherInQ5', 'passParams' => false)
|
|
|
125 |
),
|
|
|
126 |
$expected
|
|
|
127 |
);
|
|
|
128 |
$this->assertEquals($expected, $manager->listeners('fake.event'));
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
/**
|
|
|
132 |
* Tests the attach() method for multiple event key in multiple queues
|
|
|
133 |
*
|
|
|
134 |
* @return void
|
|
|
135 |
*/
|
|
|
136 |
public function testAttachMultipleEventKeys() {
|
|
|
137 |
$manager = new CakeEventManager;
|
|
|
138 |
$manager->attach('fakeFunction', 'fake.event');
|
|
|
139 |
$manager->attach('fakeFunction2', 'another.event');
|
|
|
140 |
$manager->attach('fakeFunction3', 'another.event', array('priority' => 1, 'passParams' => true));
|
|
|
141 |
$expected = array(
|
|
|
142 |
array('callable' => 'fakeFunction', 'passParams' => false)
|
|
|
143 |
);
|
|
|
144 |
$this->assertEquals($expected, $manager->listeners('fake.event'));
|
|
|
145 |
|
|
|
146 |
$expected = array(
|
|
|
147 |
array('callable' => 'fakeFunction3', 'passParams' => true),
|
|
|
148 |
array('callable' => 'fakeFunction2', 'passParams' => false)
|
|
|
149 |
);
|
|
|
150 |
$this->assertEquals($expected, $manager->listeners('another.event'));
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
/**
|
|
|
154 |
* Tests detaching an event from a event key queue
|
|
|
155 |
*
|
|
|
156 |
* @return void
|
|
|
157 |
*/
|
|
|
158 |
public function testDetach() {
|
|
|
159 |
$manager = new CakeEventManager;
|
|
|
160 |
$manager->attach(array('AClass', 'aMethod'), 'fake.event');
|
|
|
161 |
$manager->attach(array('AClass', 'anotherMethod'), 'another.event');
|
|
|
162 |
$manager->attach('fakeFunction', 'another.event', array('priority' => 1));
|
|
|
163 |
|
|
|
164 |
$manager->detach(array('AClass', 'aMethod'), 'fake.event');
|
|
|
165 |
$this->assertEquals(array(), $manager->listeners('fake.event'));
|
|
|
166 |
|
|
|
167 |
$manager->detach(array('AClass', 'anotherMethod'), 'another.event');
|
|
|
168 |
$expected = array(
|
|
|
169 |
array('callable' => 'fakeFunction', 'passParams' => false)
|
|
|
170 |
);
|
|
|
171 |
$this->assertEquals($expected, $manager->listeners('another.event'));
|
|
|
172 |
|
|
|
173 |
$manager->detach('fakeFunction', 'another.event');
|
|
|
174 |
$this->assertEquals(array(), $manager->listeners('another.event'));
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
/**
|
|
|
178 |
* Tests detaching an event from all event queues
|
|
|
179 |
*
|
|
|
180 |
* @return void
|
|
|
181 |
*/
|
|
|
182 |
public function testDetachFromAll() {
|
|
|
183 |
$manager = new CakeEventManager;
|
|
|
184 |
$manager->attach(array('AClass', 'aMethod'), 'fake.event');
|
|
|
185 |
$manager->attach(array('AClass', 'aMethod'), 'another.event');
|
|
|
186 |
$manager->attach('fakeFunction', 'another.event', array('priority' => 1));
|
|
|
187 |
|
|
|
188 |
$manager->detach(array('AClass', 'aMethod'));
|
|
|
189 |
$expected = array(
|
|
|
190 |
array('callable' => 'fakeFunction', 'passParams' => false)
|
|
|
191 |
);
|
|
|
192 |
$this->assertEquals($expected, $manager->listeners('another.event'));
|
|
|
193 |
$this->assertEquals(array(), $manager->listeners('fake.event'));
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
/**
|
|
|
197 |
* Tests event dispatching
|
|
|
198 |
*
|
|
|
199 |
* @return void
|
|
|
200 |
*/
|
|
|
201 |
public function testDispatch() {
|
|
|
202 |
$manager = new CakeEventManager;
|
|
|
203 |
$listener = $this->getMock('CakeEventTestListener');
|
|
|
204 |
$anotherListener = $this->getMock('CakeEventTestListener');
|
|
|
205 |
$manager->attach(array($listener, 'listenerFunction'), 'fake.event');
|
|
|
206 |
$manager->attach(array($anotherListener, 'listenerFunction'), 'fake.event');
|
|
|
207 |
$event = new CakeEvent('fake.event');
|
|
|
208 |
|
|
|
209 |
$listener->expects($this->once())->method('listenerFunction')->with($event);
|
|
|
210 |
$anotherListener->expects($this->once())->method('listenerFunction')->with($event);
|
|
|
211 |
$manager->dispatch($event);
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
/**
|
|
|
215 |
* Tests event dispatching using event key name
|
|
|
216 |
*
|
|
|
217 |
* @return void
|
|
|
218 |
*/
|
|
|
219 |
public function testDispatchWithKeyName() {
|
|
|
220 |
$manager = new CakeEventManager;
|
|
|
221 |
$listener = new CakeEventTestListener;
|
|
|
222 |
$manager->attach(array($listener, 'listenerFunction'), 'fake.event');
|
|
|
223 |
$event = 'fake.event';
|
|
|
224 |
$manager->dispatch($event);
|
|
|
225 |
|
|
|
226 |
$expected = array('listenerFunction');
|
|
|
227 |
$this->assertEquals($expected, $listener->callStack);
|
|
|
228 |
}
|
|
|
229 |
|
|
|
230 |
/**
|
|
|
231 |
* Tests event dispatching with a return value
|
|
|
232 |
*
|
|
|
233 |
* @return void
|
|
|
234 |
*/
|
|
|
235 |
public function testDispatchReturnValue() {
|
|
|
236 |
$this->skipIf(
|
|
|
237 |
version_compare(PHPUnit_Runner_Version::id(), '3.7', '<'),
|
|
|
238 |
'These tests fail in PHPUnit 3.6'
|
|
|
239 |
);
|
|
|
240 |
$manager = new CakeEventManager;
|
|
|
241 |
$listener = $this->getMock('CakeEventTestListener');
|
|
|
242 |
$anotherListener = $this->getMock('CakeEventTestListener');
|
|
|
243 |
$manager->attach(array($listener, 'listenerFunction'), 'fake.event');
|
|
|
244 |
$manager->attach(array($anotherListener, 'listenerFunction'), 'fake.event');
|
|
|
245 |
$event = new CakeEvent('fake.event');
|
|
|
246 |
|
|
|
247 |
$listener->expects($this->at(0))->method('listenerFunction')
|
|
|
248 |
->with($event)
|
|
|
249 |
->will($this->returnValue('something special'));
|
|
|
250 |
$anotherListener->expects($this->at(0))
|
|
|
251 |
->method('listenerFunction')
|
|
|
252 |
->with($event);
|
|
|
253 |
$manager->dispatch($event);
|
|
|
254 |
$this->assertEquals('something special', $event->result);
|
|
|
255 |
}
|
|
|
256 |
|
|
|
257 |
/**
|
|
|
258 |
* Tests that returning false in a callback stops the event
|
|
|
259 |
*
|
|
|
260 |
* @return void
|
|
|
261 |
*/
|
|
|
262 |
public function testDispatchFalseStopsEvent() {
|
|
|
263 |
$this->skipIf(
|
|
|
264 |
version_compare(PHPUnit_Runner_Version::id(), '3.7', '<'),
|
|
|
265 |
'These tests fail in PHPUnit 3.6'
|
|
|
266 |
);
|
|
|
267 |
|
|
|
268 |
$manager = new CakeEventManager;
|
|
|
269 |
$listener = $this->getMock('CakeEventTestListener');
|
|
|
270 |
$anotherListener = $this->getMock('CakeEventTestListener');
|
|
|
271 |
$manager->attach(array($listener, 'listenerFunction'), 'fake.event');
|
|
|
272 |
$manager->attach(array($anotherListener, 'listenerFunction'), 'fake.event');
|
|
|
273 |
$event = new CakeEvent('fake.event');
|
|
|
274 |
|
|
|
275 |
$listener->expects($this->at(0))->method('listenerFunction')
|
|
|
276 |
->with($event)
|
|
|
277 |
->will($this->returnValue(false));
|
|
|
278 |
$anotherListener->expects($this->never())
|
|
|
279 |
->method('listenerFunction');
|
|
|
280 |
$manager->dispatch($event);
|
|
|
281 |
$this->assertTrue($event->isStopped());
|
|
|
282 |
}
|
|
|
283 |
|
|
|
284 |
/**
|
|
|
285 |
* Tests event dispatching using priorities
|
|
|
286 |
*
|
|
|
287 |
* @return void
|
|
|
288 |
*/
|
|
|
289 |
public function testDispatchPrioritized() {
|
|
|
290 |
$manager = new CakeEventManager;
|
|
|
291 |
$listener = new CakeEventTestListener;
|
|
|
292 |
$manager->attach(array($listener, 'listenerFunction'), 'fake.event');
|
|
|
293 |
$manager->attach(array($listener, 'secondListenerFunction'), 'fake.event', array('priority' => 5));
|
|
|
294 |
$event = new CakeEvent('fake.event');
|
|
|
295 |
$manager->dispatch($event);
|
|
|
296 |
|
|
|
297 |
$expected = array('secondListenerFunction', 'listenerFunction');
|
|
|
298 |
$this->assertEquals($expected, $listener->callStack);
|
|
|
299 |
}
|
|
|
300 |
|
|
|
301 |
/**
|
|
|
302 |
* Tests event dispatching with passed params
|
|
|
303 |
*
|
|
|
304 |
* @return void
|
|
|
305 |
*/
|
|
|
306 |
public function testDispatchPassingParams() {
|
|
|
307 |
$manager = new CakeEventManager;
|
|
|
308 |
$listener = $this->getMock('CakeEventTestListener');
|
|
|
309 |
$anotherListener = $this->getMock('CakeEventTestListener');
|
|
|
310 |
$manager->attach(array($listener, 'listenerFunction'), 'fake.event');
|
|
|
311 |
$manager->attach(array($anotherListener, 'secondListenerFunction'), 'fake.event', array('passParams' => true));
|
|
|
312 |
$event = new CakeEvent('fake.event', $this, array('some' => 'data'));
|
|
|
313 |
|
|
|
314 |
$listener->expects($this->once())->method('listenerFunction')->with($event);
|
|
|
315 |
$anotherListener->expects($this->once())->method('secondListenerFunction')->with('data');
|
|
|
316 |
$manager->dispatch($event);
|
|
|
317 |
}
|
|
|
318 |
|
|
|
319 |
/**
|
|
|
320 |
* Tests subscribing a listener object and firing the events it subscribed to
|
|
|
321 |
*
|
|
|
322 |
* @return void
|
|
|
323 |
*/
|
|
|
324 |
public function testAttachSubscriber() {
|
|
|
325 |
$manager = new CakeEventManager;
|
|
|
326 |
$listener = $this->getMock('CustomTestEventListener', array('secondListenerFunction'));
|
|
|
327 |
$manager->attach($listener);
|
|
|
328 |
$event = new CakeEvent('fake.event');
|
|
|
329 |
|
|
|
330 |
$manager->dispatch($event);
|
|
|
331 |
|
|
|
332 |
$expected = array('listenerFunction');
|
|
|
333 |
$this->assertEquals($expected, $listener->callStack);
|
|
|
334 |
|
|
|
335 |
$listener->expects($this->at(0))->method('secondListenerFunction')->with('data');
|
|
|
336 |
$event = new CakeEvent('another.event', $this, array('some' => 'data'));
|
|
|
337 |
$manager->dispatch($event);
|
|
|
338 |
|
|
|
339 |
$manager = new CakeEventManager;
|
|
|
340 |
$listener = $this->getMock('CustomTestEventListener', array('listenerFunction', 'thirdListenerFunction'));
|
|
|
341 |
$manager->attach($listener);
|
|
|
342 |
$event = new CakeEvent('multiple.handlers');
|
|
|
343 |
$listener->expects($this->once())->method('listenerFunction')->with($event);
|
|
|
344 |
$listener->expects($this->once())->method('thirdListenerFunction')->with($event);
|
|
|
345 |
$manager->dispatch($event);
|
|
|
346 |
}
|
|
|
347 |
|
|
|
348 |
/**
|
|
|
349 |
* Tests subscribing a listener object and firing the events it subscribed to
|
|
|
350 |
*
|
|
|
351 |
* @return void
|
|
|
352 |
*/
|
|
|
353 |
public function testDetachSubscriber() {
|
|
|
354 |
$manager = new CakeEventManager;
|
|
|
355 |
$listener = $this->getMock('CustomTestEventListener', array('secondListenerFunction'));
|
|
|
356 |
$manager->attach($listener);
|
|
|
357 |
$expected = array(
|
|
|
358 |
array('callable' => array($listener, 'secondListenerFunction'), 'passParams' => true)
|
|
|
359 |
);
|
|
|
360 |
$this->assertEquals($expected, $manager->listeners('another.event'));
|
|
|
361 |
$expected = array(
|
|
|
362 |
array('callable' => array($listener, 'listenerFunction'), 'passParams' => false)
|
|
|
363 |
);
|
|
|
364 |
$this->assertEquals($expected, $manager->listeners('fake.event'));
|
|
|
365 |
$manager->detach($listener);
|
|
|
366 |
$this->assertEquals(array(), $manager->listeners('fake.event'));
|
|
|
367 |
$this->assertEquals(array(), $manager->listeners('another.event'));
|
|
|
368 |
}
|
|
|
369 |
|
|
|
370 |
/**
|
|
|
371 |
* Tests that it is possible to get/set the manager singleton
|
|
|
372 |
*
|
|
|
373 |
* @return void
|
|
|
374 |
*/
|
|
|
375 |
public function testGlobalDispatcherGetter() {
|
|
|
376 |
$this->assertInstanceOf('CakeEventManager', CakeEventManager::instance());
|
|
|
377 |
$manager = new CakeEventManager;
|
|
|
378 |
|
|
|
379 |
CakeEventManager::instance($manager);
|
|
|
380 |
$this->assertSame($manager, CakeEventManager::instance());
|
|
|
381 |
}
|
|
|
382 |
|
|
|
383 |
/**
|
|
|
384 |
* Tests that the global event manager gets the event too from any other manager
|
|
|
385 |
*
|
|
|
386 |
* @return void
|
|
|
387 |
*/
|
|
|
388 |
public function testDispatchWithGlobal() {
|
|
|
389 |
$generalManager = $this->getMock('CakeEventManager', array('dispatch'));
|
|
|
390 |
$manager = new CakeEventManager;
|
|
|
391 |
$event = new CakeEvent('fake.event');
|
|
|
392 |
CakeEventManager::instance($generalManager);
|
|
|
393 |
|
|
|
394 |
$generalManager->expects($this->once())->method('dispatch')->with($event);
|
|
|
395 |
$manager->dispatch($event);
|
|
|
396 |
}
|
|
|
397 |
|
|
|
398 |
/**
|
|
|
399 |
* Tests that stopping an event will not notify the rest of the listeners
|
|
|
400 |
*
|
|
|
401 |
* @return void
|
|
|
402 |
*/
|
|
|
403 |
public function testStopPropagation() {
|
|
|
404 |
$manager = new CakeEventManager;
|
|
|
405 |
$listener = new CakeEventTestListener;
|
|
|
406 |
$manager->attach(array($listener, 'listenerFunction'), 'fake.event');
|
|
|
407 |
$manager->attach(array($listener, 'stopListener'), 'fake.event', array('priority' => 8));
|
|
|
408 |
$manager->attach(array($listener, 'secondListenerFunction'), 'fake.event', array('priority' => 5));
|
|
|
409 |
$event = new CakeEvent('fake.event');
|
|
|
410 |
$manager->dispatch($event);
|
|
|
411 |
|
|
|
412 |
$expected = array('secondListenerFunction');
|
|
|
413 |
$this->assertEquals($expected, $listener->callStack);
|
|
|
414 |
}
|
|
|
415 |
}
|