EventManagerTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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\EventListenerInterface;
  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 CustomTestEventListenerInterface extends EventTestListener implements EventListenerInterface {
  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. * @triggers fake.event
  169. */
  170. public function testDispatch() {
  171. $manager = new EventManager();
  172. $listener = $this->getMock(__NAMESPACE__ . '\EventTestListener');
  173. $anotherListener = $this->getMock(__NAMESPACE__ . '\EventTestListener');
  174. $manager->attach(array($listener, 'listenerFunction'), 'fake.event');
  175. $manager->attach(array($anotherListener, 'listenerFunction'), 'fake.event');
  176. $event = new Event('fake.event');
  177. $listener->expects($this->once())->method('listenerFunction')->with($event);
  178. $anotherListener->expects($this->once())->method('listenerFunction')->with($event);
  179. $manager->dispatch($event);
  180. }
  181. /**
  182. * Tests event dispatching using event key name
  183. *
  184. * @return void
  185. */
  186. public function testDispatchWithKeyName() {
  187. $manager = new EventManager();
  188. $listener = new EventTestListener;
  189. $manager->attach(array($listener, 'listenerFunction'), 'fake.event');
  190. $event = 'fake.event';
  191. $manager->dispatch($event);
  192. $expected = array('listenerFunction');
  193. $this->assertEquals($expected, $listener->callStack);
  194. }
  195. /**
  196. * Tests event dispatching with a return value
  197. *
  198. * @return void
  199. * @triggers fake.event
  200. */
  201. public function testDispatchReturnValue() {
  202. $this->skipIf(
  203. version_compare(\PHPUnit_Runner_Version::id(), '3.7', '<'),
  204. 'These tests fail in PHPUnit 3.6'
  205. );
  206. $manager = new EventManager;
  207. $listener = $this->getMock(__NAMESPACE__ . '\EventTestListener');
  208. $anotherListener = $this->getMock(__NAMESPACE__ . '\EventTestListener');
  209. $manager->attach(array($listener, 'listenerFunction'), 'fake.event');
  210. $manager->attach(array($anotherListener, 'listenerFunction'), 'fake.event');
  211. $event = new Event('fake.event');
  212. $listener->expects($this->at(0))->method('listenerFunction')
  213. ->with($event)
  214. ->will($this->returnValue('something special'));
  215. $anotherListener->expects($this->at(0))
  216. ->method('listenerFunction')
  217. ->with($event);
  218. $manager->dispatch($event);
  219. $this->assertEquals('something special', $event->result);
  220. }
  221. /**
  222. * Tests that returning false in a callback stops the event
  223. *
  224. * @return void
  225. * @triggers fake.event
  226. */
  227. public function testDispatchFalseStopsEvent() {
  228. $this->skipIf(
  229. version_compare(\PHPUnit_Runner_Version::id(), '3.7', '<'),
  230. 'These tests fail in PHPUnit 3.6'
  231. );
  232. $manager = new EventManager();
  233. $listener = $this->getMock(__NAMESPACE__ . '\EventTestListener');
  234. $anotherListener = $this->getMock(__NAMESPACE__ . '\EventTestListener');
  235. $manager->attach(array($listener, 'listenerFunction'), 'fake.event');
  236. $manager->attach(array($anotherListener, 'listenerFunction'), 'fake.event');
  237. $event = new Event('fake.event');
  238. $listener->expects($this->at(0))->method('listenerFunction')
  239. ->with($event)
  240. ->will($this->returnValue(false));
  241. $anotherListener->expects($this->never())
  242. ->method('listenerFunction');
  243. $manager->dispatch($event);
  244. $this->assertTrue($event->isStopped());
  245. }
  246. /**
  247. * Tests event dispatching using priorities
  248. *
  249. * @return void
  250. * @triggers fake.event
  251. */
  252. public function testDispatchPrioritized() {
  253. $manager = new EventManager();
  254. $listener = new EventTestListener;
  255. $manager->attach(array($listener, 'listenerFunction'), 'fake.event');
  256. $manager->attach(array($listener, 'secondListenerFunction'), 'fake.event', array('priority' => 5));
  257. $event = new Event('fake.event');
  258. $manager->dispatch($event);
  259. $expected = array('secondListenerFunction', 'listenerFunction');
  260. $this->assertEquals($expected, $listener->callStack);
  261. }
  262. /**
  263. * Tests subscribing a listener object and firing the events it subscribed to
  264. *
  265. * @return void
  266. * @triggers fake.event
  267. * @triggers another.event $this, array(some => data)
  268. */
  269. public function testAttachSubscriber() {
  270. $manager = new EventManager();
  271. $listener = $this->getMock(__NAMESPACE__ . '\CustomTestEventListenerInterface', array('secondListenerFunction'));
  272. $manager->attach($listener);
  273. $event = new Event('fake.event');
  274. $manager->dispatch($event);
  275. $expected = array('listenerFunction');
  276. $this->assertEquals($expected, $listener->callStack);
  277. $event = new Event('another.event', $this, array('some' => 'data'));
  278. $listener->expects($this->at(0))
  279. ->method('secondListenerFunction')
  280. ->with($event, 'data');
  281. $manager->dispatch($event);
  282. }
  283. /**
  284. * Test implementedEvents binding multiple callbacks to the same event name.
  285. *
  286. * @return void
  287. * @triggers multiple.handlers
  288. */
  289. public function testAttachSubscriberMultiple() {
  290. $manager = new EventManager();
  291. $listener = $this->getMock(__NAMESPACE__ . '\CustomTestEventListenerInterface', array('listenerFunction', 'thirdListenerFunction'));
  292. $manager->attach($listener);
  293. $event = new Event('multiple.handlers');
  294. $listener->expects($this->once())
  295. ->method('listenerFunction')
  296. ->with($event);
  297. $listener->expects($this->once())
  298. ->method('thirdListenerFunction')
  299. ->with($event);
  300. $manager->dispatch($event);
  301. }
  302. /**
  303. * Tests subscribing a listener object and firing the events it subscribed to
  304. *
  305. * @return void
  306. */
  307. public function testDetachSubscriber() {
  308. $manager = new EventManager();
  309. $listener = $this->getMock(__NAMESPACE__ . '\CustomTestEventListenerInterface', array('secondListenerFunction'));
  310. $manager->attach($listener);
  311. $expected = array(
  312. array('callable' => array($listener, 'secondListenerFunction'))
  313. );
  314. $this->assertEquals($expected, $manager->listeners('another.event'));
  315. $expected = array(
  316. array('callable' => array($listener, 'listenerFunction'))
  317. );
  318. $this->assertEquals($expected, $manager->listeners('fake.event'));
  319. $manager->detach($listener);
  320. $this->assertEquals(array(), $manager->listeners('fake.event'));
  321. $this->assertEquals(array(), $manager->listeners('another.event'));
  322. }
  323. /**
  324. * Tests that it is possible to get/set the manager singleton
  325. *
  326. * @return void
  327. */
  328. public function testGlobalDispatcherGetter() {
  329. $this->assertInstanceOf('Cake\Event\EventManager', EventManager::instance());
  330. $manager = new EventManager();
  331. EventManager::instance($manager);
  332. $this->assertSame($manager, EventManager::instance());
  333. }
  334. /**
  335. * Tests that the global event manager gets the event too from any other manager
  336. *
  337. * @return void
  338. * @triggers fake.event
  339. */
  340. public function testDispatchWithGlobal() {
  341. $generalManager = $this->getMock('Cake\Event\EventManager', array('prioritisedListeners'));
  342. $manager = new EventManager();
  343. $event = new Event('fake.event');
  344. EventManager::instance($generalManager);
  345. $generalManager->expects($this->once())->method('prioritisedListeners')->with('fake.event');
  346. $manager->dispatch($event);
  347. EventManager::instance(new EventManager());
  348. }
  349. /**
  350. * Tests that stopping an event will not notify the rest of the listeners
  351. *
  352. * @return void
  353. * @triggers fake.event
  354. */
  355. public function testStopPropagation() {
  356. $generalManager = $this->getMock('Cake\Event\EventManager');
  357. $manager = new EventManager();
  358. $listener = new EventTestListener();
  359. EventManager::instance($generalManager);
  360. $generalManager->expects($this->any())
  361. ->method('prioritisedListeners')
  362. ->with('fake.event')
  363. ->will($this->returnValue(array()));
  364. $manager->attach(array($listener, 'listenerFunction'), 'fake.event');
  365. $manager->attach(array($listener, 'stopListener'), 'fake.event', array('priority' => 8));
  366. $manager->attach(array($listener, 'secondListenerFunction'), 'fake.event', array('priority' => 5));
  367. $event = new Event('fake.event');
  368. $manager->dispatch($event);
  369. $expected = array('secondListenerFunction');
  370. $this->assertEquals($expected, $listener->callStack);
  371. EventManager::instance(new EventManager());
  372. }
  373. /**
  374. * Tests event dispatching using priorities
  375. *
  376. * @return void
  377. * @triggers fake.event
  378. */
  379. public function testDispatchPrioritizedWithGlobal() {
  380. $generalManager = $this->getMock('Cake\Event\EventManager');
  381. $manager = new EventManager();
  382. $listener = new CustomTestEventListenerInterface();
  383. $event = new Event('fake.event');
  384. EventManager::instance($generalManager);
  385. $generalManager->expects($this->any())
  386. ->method('prioritisedListeners')
  387. ->with('fake.event')
  388. ->will($this->returnValue(
  389. array(11 => array(
  390. array('callable' => array($listener, 'secondListenerFunction'))
  391. ))
  392. ));
  393. $manager->attach(array($listener, 'listenerFunction'), 'fake.event');
  394. $manager->attach(array($listener, 'thirdListenerFunction'), 'fake.event', array('priority' => 15));
  395. $manager->dispatch($event);
  396. $expected = array('listenerFunction', 'secondListenerFunction', 'thirdListenerFunction');
  397. $this->assertEquals($expected, $listener->callStack);
  398. EventManager::instance(new EventManager());
  399. }
  400. /**
  401. * Tests event dispatching using priorities
  402. *
  403. * @return void
  404. * @triggers fake.event
  405. */
  406. public function testDispatchGlobalBeforeLocal() {
  407. $generalManager = $this->getMock('Cake\Event\EventManager');
  408. $manager = new EventManager();
  409. $listener = new CustomTestEventListenerInterface();
  410. $event = new Event('fake.event');
  411. EventManager::instance($generalManager);
  412. $generalManager->expects($this->any())
  413. ->method('prioritisedListeners')
  414. ->with('fake.event')
  415. ->will($this->returnValue(
  416. array(10 => array(
  417. array('callable' => array($listener, 'listenerFunction'))
  418. ))
  419. ));
  420. $manager->attach(array($listener, 'secondListenerFunction'), 'fake.event');
  421. $manager->dispatch($event);
  422. $expected = array('listenerFunction', 'secondListenerFunction');
  423. $this->assertEquals($expected, $listener->callStack);
  424. EventManager::instance(new EventManager());
  425. }
  426. /**
  427. * test callback
  428. */
  429. public function onMyEvent($event) {
  430. $event->data['callback'] = 'ok';
  431. }
  432. /**
  433. * Tests events dispatched by a local manager can be handled by
  434. * handler registered in the global event manager
  435. * @triggers my_event $manager
  436. */
  437. public function testDispatchLocalHandledByGlobal() {
  438. $callback = array($this, 'onMyEvent');
  439. EventManager::instance()->attach($callback, 'my_event');
  440. $manager = new EventManager();
  441. $event = new Event('my_event', $manager);
  442. $manager->dispatch($event);
  443. $this->assertEquals('ok', $event->data['callback']);
  444. }
  445. /**
  446. * Test that events are dispatched properly when there are global and local
  447. * listeners at the same priority.
  448. *
  449. * @return void
  450. * @triggers fake.event $this)
  451. */
  452. public function testDispatchWithGlobalAndLocalEvents() {
  453. $listener = new CustomTestEventListenerInterface();
  454. EventManager::instance()->attach($listener);
  455. $listener2 = new EventTestListener();
  456. $manager = new EventManager();
  457. $manager->attach(array($listener2, 'listenerFunction'), 'fake.event');
  458. $manager->dispatch(new Event('fake.event', $this));
  459. $this->assertEquals(array('listenerFunction'), $listener->callStack);
  460. $this->assertEquals(array('listenerFunction'), $listener2->callStack);
  461. }
  462. }