EventManagerTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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. {
  25. public $callStack = [];
  26. /**
  27. * Test function to be used in event dispatching
  28. *
  29. * @return void
  30. */
  31. public function listenerFunction()
  32. {
  33. $this->callStack[] = __FUNCTION__;
  34. }
  35. /**
  36. * Test function to be used in event dispatching
  37. *
  38. * @return void
  39. */
  40. public function secondListenerFunction()
  41. {
  42. $this->callStack[] = __FUNCTION__;
  43. }
  44. /**
  45. * Auxiliary function to help in stopPropagation testing
  46. *
  47. * @param \Cake\Event\Event $event
  48. * @return void
  49. */
  50. public function stopListener($event)
  51. {
  52. $event->stopPropagation();
  53. }
  54. }
  55. /**
  56. * Mock used for testing the subscriber objects
  57. */
  58. class CustomTestEventListenerInterface extends EventTestListener implements EventListenerInterface
  59. {
  60. public function implementedEvents()
  61. {
  62. return [
  63. 'fake.event' => 'listenerFunction',
  64. 'another.event' => ['callable' => 'secondListenerFunction'],
  65. 'multiple.handlers' => [
  66. ['callable' => 'listenerFunction'],
  67. ['callable' => 'thirdListenerFunction']
  68. ]
  69. ];
  70. }
  71. /**
  72. * Test function to be used in event dispatching
  73. *
  74. * @return void
  75. */
  76. public function thirdListenerFunction()
  77. {
  78. $this->callStack[] = __FUNCTION__;
  79. }
  80. }
  81. /**
  82. * Tests the Cake\Event\EventManager class functionality
  83. *
  84. */
  85. class EventManagerTest extends TestCase
  86. {
  87. /**
  88. * Tests the attach() method for a single event key in multiple queues
  89. *
  90. * @return void
  91. */
  92. public function testAttachListeners()
  93. {
  94. $manager = new EventManager();
  95. $manager->attach('fakeFunction', 'fake.event');
  96. $expected = [
  97. ['callable' => 'fakeFunction']
  98. ];
  99. $this->assertEquals($expected, $manager->listeners('fake.event'));
  100. $manager->attach('fakeFunction2', 'fake.event');
  101. $expected[] = ['callable' => 'fakeFunction2'];
  102. $this->assertEquals($expected, $manager->listeners('fake.event'));
  103. $manager->attach('inQ5', 'fake.event', ['priority' => 5]);
  104. $manager->attach('inQ1', 'fake.event', ['priority' => 1]);
  105. $manager->attach('otherInQ5', 'fake.event', ['priority' => 5]);
  106. $expected = array_merge(
  107. [
  108. ['callable' => 'inQ1'],
  109. ['callable' => 'inQ5'],
  110. ['callable' => 'otherInQ5']
  111. ],
  112. $expected
  113. );
  114. $this->assertEquals($expected, $manager->listeners('fake.event'));
  115. }
  116. /**
  117. * Tests the attach() method for multiple event key in multiple queues
  118. *
  119. * @return void
  120. */
  121. public function testAttachMultipleEventKeys()
  122. {
  123. $manager = new EventManager();
  124. $manager->attach('fakeFunction', 'fake.event');
  125. $manager->attach('fakeFunction2', 'another.event');
  126. $manager->attach('fakeFunction3', 'another.event', ['priority' => 1]);
  127. $expected = [
  128. ['callable' => 'fakeFunction']
  129. ];
  130. $this->assertEquals($expected, $manager->listeners('fake.event'));
  131. $expected = [
  132. ['callable' => 'fakeFunction3'],
  133. ['callable' => 'fakeFunction2']
  134. ];
  135. $this->assertEquals($expected, $manager->listeners('another.event'));
  136. }
  137. /**
  138. * Test the on() method for basic callable types.
  139. *
  140. * @return void
  141. */
  142. public function testOn()
  143. {
  144. $count = 1;
  145. $manager = new EventManager();
  146. $manager->on('my.event', 'myfunc');
  147. $expected = [
  148. ['callable' => 'myfunc']
  149. ];
  150. $this->assertSame($expected, $manager->listeners('my.event'));
  151. $manager->on('my.event', ['priority' => 1], 'func2');
  152. $expected = [
  153. ['callable' => 'func2'],
  154. ['callable' => 'myfunc'],
  155. ];
  156. $this->assertSame($expected, $manager->listeners('my.event'));
  157. $listener = new CustomTestEventListenerInterface();
  158. $manager->on($listener);
  159. $expected = [
  160. ['callable' => [$listener, 'listenerFunction']],
  161. ];
  162. $this->assertEquals($expected, $manager->listeners('fake.event'));
  163. }
  164. /**
  165. * Tests off'ing an event from a event key queue
  166. *
  167. * @return void
  168. */
  169. public function testOff()
  170. {
  171. $manager = new EventManager();
  172. $manager->on('fake.event', ['AClass', 'aMethod']);
  173. $manager->on('another.event', ['AClass', 'anotherMethod']);
  174. $manager->on('another.event', ['priority' => 1], 'fakeFunction');
  175. $manager->off('fake.event', ['AClass', 'aMethod']);
  176. $this->assertEquals([], $manager->listeners('fake.event'));
  177. $manager->off('another.event', ['AClass', 'anotherMethod']);
  178. $expected = [
  179. ['callable' => 'fakeFunction']
  180. ];
  181. $this->assertEquals($expected, $manager->listeners('another.event'));
  182. $manager->off('another.event', 'fakeFunction');
  183. $this->assertEquals([], $manager->listeners('another.event'));
  184. }
  185. /**
  186. * Tests off'ing an event from all event queues
  187. *
  188. * @return void
  189. */
  190. public function testOffFromAll()
  191. {
  192. $manager = new EventManager();
  193. $manager->on('fake.event', ['AClass', 'aMethod']);
  194. $manager->on('another.event', ['AClass', 'aMethod']);
  195. $manager->on('another.event', ['priority' => 1], 'fakeFunction');
  196. $manager->off(['AClass', 'aMethod']);
  197. $expected = [
  198. ['callable' => 'fakeFunction']
  199. ];
  200. $this->assertEquals($expected, $manager->listeners('another.event'));
  201. $this->assertEquals([], $manager->listeners('fake.event'));
  202. }
  203. /**
  204. * Tests detaching an event from a event key queue
  205. *
  206. * @return void
  207. */
  208. public function testDetach()
  209. {
  210. $manager = new EventManager();
  211. $manager->attach(['AClass', 'aMethod'], 'fake.event');
  212. $manager->attach(['AClass', 'anotherMethod'], 'another.event');
  213. $manager->attach('fakeFunction', 'another.event', ['priority' => 1]);
  214. $manager->detach(['AClass', 'aMethod'], 'fake.event');
  215. $this->assertEquals([], $manager->listeners('fake.event'));
  216. $manager->detach(['AClass', 'anotherMethod'], 'another.event');
  217. $expected = [
  218. ['callable' => 'fakeFunction']
  219. ];
  220. $this->assertEquals($expected, $manager->listeners('another.event'));
  221. $manager->detach('fakeFunction', 'another.event');
  222. $this->assertEquals([], $manager->listeners('another.event'));
  223. }
  224. /**
  225. * Tests detaching an event from all event queues
  226. *
  227. * @return void
  228. */
  229. public function testDetachFromAll()
  230. {
  231. $manager = new EventManager();
  232. $manager->attach(['AClass', 'aMethod'], 'fake.event');
  233. $manager->attach(['AClass', 'aMethod'], 'another.event');
  234. $manager->attach('fakeFunction', 'another.event', ['priority' => 1]);
  235. $manager->detach(['AClass', 'aMethod']);
  236. $expected = [
  237. ['callable' => 'fakeFunction']
  238. ];
  239. $this->assertEquals($expected, $manager->listeners('another.event'));
  240. $this->assertEquals([], $manager->listeners('fake.event'));
  241. }
  242. /**
  243. * Tests event dispatching
  244. *
  245. * @return void
  246. * @triggers fake.event
  247. */
  248. public function testDispatch()
  249. {
  250. $manager = new EventManager();
  251. $listener = $this->getMock(__NAMESPACE__ . '\EventTestListener');
  252. $anotherListener = $this->getMock(__NAMESPACE__ . '\EventTestListener');
  253. $manager->attach([$listener, 'listenerFunction'], 'fake.event');
  254. $manager->attach([$anotherListener, 'listenerFunction'], 'fake.event');
  255. $event = new Event('fake.event');
  256. $listener->expects($this->once())->method('listenerFunction')->with($event);
  257. $anotherListener->expects($this->once())->method('listenerFunction')->with($event);
  258. $manager->dispatch($event);
  259. }
  260. /**
  261. * Tests event dispatching using event key name
  262. *
  263. * @return void
  264. */
  265. public function testDispatchWithKeyName()
  266. {
  267. $manager = new EventManager();
  268. $listener = new EventTestListener;
  269. $manager->attach([$listener, 'listenerFunction'], 'fake.event');
  270. $event = 'fake.event';
  271. $manager->dispatch($event);
  272. $expected = ['listenerFunction'];
  273. $this->assertEquals($expected, $listener->callStack);
  274. }
  275. /**
  276. * Tests event dispatching with a return value
  277. *
  278. * @return void
  279. * @triggers fake.event
  280. */
  281. public function testDispatchReturnValue()
  282. {
  283. $this->skipIf(
  284. version_compare(\PHPUnit_Runner_Version::id(), '3.7', '<'),
  285. 'These tests fail in PHPUnit 3.6'
  286. );
  287. $manager = new EventManager;
  288. $listener = $this->getMock(__NAMESPACE__ . '\EventTestListener');
  289. $anotherListener = $this->getMock(__NAMESPACE__ . '\EventTestListener');
  290. $manager->attach([$listener, 'listenerFunction'], 'fake.event');
  291. $manager->attach([$anotherListener, 'listenerFunction'], 'fake.event');
  292. $event = new Event('fake.event');
  293. $listener->expects($this->at(0))->method('listenerFunction')
  294. ->with($event)
  295. ->will($this->returnValue('something special'));
  296. $anotherListener->expects($this->at(0))
  297. ->method('listenerFunction')
  298. ->with($event);
  299. $manager->dispatch($event);
  300. $this->assertEquals('something special', $event->result);
  301. }
  302. /**
  303. * Tests that returning false in a callback stops the event
  304. *
  305. * @return void
  306. * @triggers fake.event
  307. */
  308. public function testDispatchFalseStopsEvent()
  309. {
  310. $this->skipIf(
  311. version_compare(\PHPUnit_Runner_Version::id(), '3.7', '<'),
  312. 'These tests fail in PHPUnit 3.6'
  313. );
  314. $manager = new EventManager();
  315. $listener = $this->getMock(__NAMESPACE__ . '\EventTestListener');
  316. $anotherListener = $this->getMock(__NAMESPACE__ . '\EventTestListener');
  317. $manager->attach([$listener, 'listenerFunction'], 'fake.event');
  318. $manager->attach([$anotherListener, 'listenerFunction'], 'fake.event');
  319. $event = new Event('fake.event');
  320. $listener->expects($this->at(0))->method('listenerFunction')
  321. ->with($event)
  322. ->will($this->returnValue(false));
  323. $anotherListener->expects($this->never())
  324. ->method('listenerFunction');
  325. $manager->dispatch($event);
  326. $this->assertTrue($event->isStopped());
  327. }
  328. /**
  329. * Tests event dispatching using priorities
  330. *
  331. * @return void
  332. * @triggers fake.event
  333. */
  334. public function testDispatchPrioritized()
  335. {
  336. $manager = new EventManager();
  337. $listener = new EventTestListener;
  338. $manager->attach([$listener, 'listenerFunction'], 'fake.event');
  339. $manager->attach([$listener, 'secondListenerFunction'], 'fake.event', ['priority' => 5]);
  340. $event = new Event('fake.event');
  341. $manager->dispatch($event);
  342. $expected = ['secondListenerFunction', 'listenerFunction'];
  343. $this->assertEquals($expected, $listener->callStack);
  344. }
  345. /**
  346. * Tests subscribing a listener object and firing the events it subscribed to
  347. *
  348. * @return void
  349. * @triggers fake.event
  350. * @triggers another.event $this, array(some => data)
  351. */
  352. public function testAttachSubscriber()
  353. {
  354. $manager = new EventManager();
  355. $listener = $this->getMock(__NAMESPACE__ . '\CustomTestEventListenerInterface', ['secondListenerFunction']);
  356. $manager->attach($listener);
  357. $event = new Event('fake.event');
  358. $manager->dispatch($event);
  359. $expected = ['listenerFunction'];
  360. $this->assertEquals($expected, $listener->callStack);
  361. $event = new Event('another.event', $this, ['some' => 'data']);
  362. $listener->expects($this->at(0))
  363. ->method('secondListenerFunction')
  364. ->with($event, 'data');
  365. $manager->dispatch($event);
  366. }
  367. /**
  368. * Test implementedEvents binding multiple callbacks to the same event name.
  369. *
  370. * @return void
  371. * @triggers multiple.handlers
  372. */
  373. public function testAttachSubscriberMultiple()
  374. {
  375. $manager = new EventManager();
  376. $listener = $this->getMock(__NAMESPACE__ . '\CustomTestEventListenerInterface', ['listenerFunction', 'thirdListenerFunction']);
  377. $manager->attach($listener);
  378. $event = new Event('multiple.handlers');
  379. $listener->expects($this->once())
  380. ->method('listenerFunction')
  381. ->with($event);
  382. $listener->expects($this->once())
  383. ->method('thirdListenerFunction')
  384. ->with($event);
  385. $manager->dispatch($event);
  386. }
  387. /**
  388. * Tests subscribing a listener object and firing the events it subscribed to
  389. *
  390. * @return void
  391. */
  392. public function testDetachSubscriber()
  393. {
  394. $manager = new EventManager();
  395. $listener = $this->getMock(__NAMESPACE__ . '\CustomTestEventListenerInterface', ['secondListenerFunction']);
  396. $manager->attach($listener);
  397. $expected = [
  398. ['callable' => [$listener, 'secondListenerFunction']]
  399. ];
  400. $this->assertEquals($expected, $manager->listeners('another.event'));
  401. $expected = [
  402. ['callable' => [$listener, 'listenerFunction']]
  403. ];
  404. $this->assertEquals($expected, $manager->listeners('fake.event'));
  405. $manager->detach($listener);
  406. $this->assertEquals([], $manager->listeners('fake.event'));
  407. $this->assertEquals([], $manager->listeners('another.event'));
  408. }
  409. /**
  410. * Tests that it is possible to get/set the manager singleton
  411. *
  412. * @return void
  413. */
  414. public function testGlobalDispatcherGetter()
  415. {
  416. $this->assertInstanceOf('Cake\Event\EventManager', EventManager::instance());
  417. $manager = new EventManager();
  418. EventManager::instance($manager);
  419. $this->assertSame($manager, EventManager::instance());
  420. }
  421. /**
  422. * Tests that the global event manager gets the event too from any other manager
  423. *
  424. * @return void
  425. * @triggers fake.event
  426. */
  427. public function testDispatchWithGlobal()
  428. {
  429. $generalManager = $this->getMock('Cake\Event\EventManager', ['prioritisedListeners']);
  430. $manager = new EventManager();
  431. $event = new Event('fake.event');
  432. EventManager::instance($generalManager);
  433. $generalManager->expects($this->once())->method('prioritisedListeners')->with('fake.event');
  434. $manager->dispatch($event);
  435. EventManager::instance(new EventManager());
  436. }
  437. /**
  438. * Tests that stopping an event will not notify the rest of the listeners
  439. *
  440. * @return void
  441. * @triggers fake.event
  442. */
  443. public function testStopPropagation()
  444. {
  445. $generalManager = $this->getMock('Cake\Event\EventManager');
  446. $manager = new EventManager();
  447. $listener = new EventTestListener();
  448. EventManager::instance($generalManager);
  449. $generalManager->expects($this->any())
  450. ->method('prioritisedListeners')
  451. ->with('fake.event')
  452. ->will($this->returnValue([]));
  453. $manager->attach([$listener, 'listenerFunction'], 'fake.event');
  454. $manager->attach([$listener, 'stopListener'], 'fake.event', ['priority' => 8]);
  455. $manager->attach([$listener, 'secondListenerFunction'], 'fake.event', ['priority' => 5]);
  456. $event = new Event('fake.event');
  457. $manager->dispatch($event);
  458. $expected = ['secondListenerFunction'];
  459. $this->assertEquals($expected, $listener->callStack);
  460. EventManager::instance(new EventManager());
  461. }
  462. /**
  463. * Tests event dispatching using priorities
  464. *
  465. * @return void
  466. * @triggers fake.event
  467. */
  468. public function testDispatchPrioritizedWithGlobal()
  469. {
  470. $generalManager = $this->getMock('Cake\Event\EventManager');
  471. $manager = new EventManager();
  472. $listener = new CustomTestEventListenerInterface();
  473. $event = new Event('fake.event');
  474. EventManager::instance($generalManager);
  475. $generalManager->expects($this->any())
  476. ->method('prioritisedListeners')
  477. ->with('fake.event')
  478. ->will($this->returnValue(
  479. [11 => [
  480. ['callable' => [$listener, 'secondListenerFunction']]
  481. ]]
  482. ));
  483. $manager->attach([$listener, 'listenerFunction'], 'fake.event');
  484. $manager->attach([$listener, 'thirdListenerFunction'], 'fake.event', ['priority' => 15]);
  485. $manager->dispatch($event);
  486. $expected = ['listenerFunction', 'secondListenerFunction', 'thirdListenerFunction'];
  487. $this->assertEquals($expected, $listener->callStack);
  488. EventManager::instance(new EventManager());
  489. }
  490. /**
  491. * Tests event dispatching using priorities
  492. *
  493. * @return void
  494. * @triggers fake.event
  495. */
  496. public function testDispatchGlobalBeforeLocal()
  497. {
  498. $generalManager = $this->getMock('Cake\Event\EventManager');
  499. $manager = new EventManager();
  500. $listener = new CustomTestEventListenerInterface();
  501. $event = new Event('fake.event');
  502. EventManager::instance($generalManager);
  503. $generalManager->expects($this->any())
  504. ->method('prioritisedListeners')
  505. ->with('fake.event')
  506. ->will($this->returnValue(
  507. [10 => [
  508. ['callable' => [$listener, 'listenerFunction']]
  509. ]]
  510. ));
  511. $manager->attach([$listener, 'secondListenerFunction'], 'fake.event');
  512. $manager->dispatch($event);
  513. $expected = ['listenerFunction', 'secondListenerFunction'];
  514. $this->assertEquals($expected, $listener->callStack);
  515. EventManager::instance(new EventManager());
  516. }
  517. /**
  518. * test callback
  519. */
  520. public function onMyEvent($event)
  521. {
  522. $event->data['callback'] = 'ok';
  523. }
  524. /**
  525. * Tests events dispatched by a local manager can be handled by
  526. * handler registered in the global event manager
  527. * @triggers my_event $manager
  528. */
  529. public function testDispatchLocalHandledByGlobal()
  530. {
  531. $callback = [$this, 'onMyEvent'];
  532. EventManager::instance()->attach($callback, 'my_event');
  533. $manager = new EventManager();
  534. $event = new Event('my_event', $manager);
  535. $manager->dispatch($event);
  536. $this->assertEquals('ok', $event->data['callback']);
  537. }
  538. /**
  539. * Test that events are dispatched properly when there are global and local
  540. * listeners at the same priority.
  541. *
  542. * @return void
  543. * @triggers fake.event $this)
  544. */
  545. public function testDispatchWithGlobalAndLocalEvents()
  546. {
  547. $listener = new CustomTestEventListenerInterface();
  548. EventManager::instance()->attach($listener);
  549. $listener2 = new EventTestListener();
  550. $manager = new EventManager();
  551. $manager->attach([$listener2, 'listenerFunction'], 'fake.event');
  552. $manager->dispatch(new Event('fake.event', $this));
  553. $this->assertEquals(['listenerFunction'], $listener->callStack);
  554. $this->assertEquals(['listenerFunction'], $listener2->callStack);
  555. }
  556. }