EventManagerTest.php 15 KB

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