EventManagerTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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 off'ing all listeners for an event
  205. */
  206. public function testRemoveAllListeners()
  207. {
  208. $manager = new EventManager();
  209. $manager->on('fake.event', ['AClass', 'aMethod']);
  210. $manager->on('another.event', ['priority' => 1], 'fakeFunction');
  211. $manager->off('fake.event');
  212. $expected = [
  213. ['callable' => 'fakeFunction']
  214. ];
  215. $this->assertEquals($expected, $manager->listeners('another.event'));
  216. $this->assertEquals([], $manager->listeners('fake.event'));
  217. }
  218. /**
  219. * Tests detaching an event from a event key queue
  220. *
  221. * @return void
  222. */
  223. public function testDetach()
  224. {
  225. $manager = new EventManager();
  226. $manager->attach(['AClass', 'aMethod'], 'fake.event');
  227. $manager->attach(['AClass', 'anotherMethod'], 'another.event');
  228. $manager->attach('fakeFunction', 'another.event', ['priority' => 1]);
  229. $manager->detach(['AClass', 'aMethod'], 'fake.event');
  230. $this->assertEquals([], $manager->listeners('fake.event'));
  231. $manager->detach(['AClass', 'anotherMethod'], 'another.event');
  232. $expected = [
  233. ['callable' => 'fakeFunction']
  234. ];
  235. $this->assertEquals($expected, $manager->listeners('another.event'));
  236. $manager->detach('fakeFunction', 'another.event');
  237. $this->assertEquals([], $manager->listeners('another.event'));
  238. }
  239. /**
  240. * Tests detaching an event from all event queues
  241. *
  242. * @return void
  243. */
  244. public function testDetachFromAll()
  245. {
  246. $manager = new EventManager();
  247. $manager->attach(['AClass', 'aMethod'], 'fake.event');
  248. $manager->attach(['AClass', 'aMethod'], 'another.event');
  249. $manager->attach('fakeFunction', 'another.event', ['priority' => 1]);
  250. $manager->detach(['AClass', 'aMethod']);
  251. $expected = [
  252. ['callable' => 'fakeFunction']
  253. ];
  254. $this->assertEquals($expected, $manager->listeners('another.event'));
  255. $this->assertEquals([], $manager->listeners('fake.event'));
  256. }
  257. /**
  258. * Tests event dispatching
  259. *
  260. * @return void
  261. * @triggers fake.event
  262. */
  263. public function testDispatch()
  264. {
  265. $manager = new EventManager();
  266. $listener = $this->getMock(__NAMESPACE__ . '\EventTestListener');
  267. $anotherListener = $this->getMock(__NAMESPACE__ . '\EventTestListener');
  268. $manager->attach([$listener, 'listenerFunction'], 'fake.event');
  269. $manager->attach([$anotherListener, 'listenerFunction'], 'fake.event');
  270. $event = new Event('fake.event');
  271. $listener->expects($this->once())->method('listenerFunction')->with($event);
  272. $anotherListener->expects($this->once())->method('listenerFunction')->with($event);
  273. $manager->dispatch($event);
  274. }
  275. /**
  276. * Tests event dispatching using event key name
  277. *
  278. * @return void
  279. */
  280. public function testDispatchWithKeyName()
  281. {
  282. $manager = new EventManager();
  283. $listener = new EventTestListener;
  284. $manager->attach([$listener, 'listenerFunction'], 'fake.event');
  285. $event = 'fake.event';
  286. $manager->dispatch($event);
  287. $expected = ['listenerFunction'];
  288. $this->assertEquals($expected, $listener->callStack);
  289. }
  290. /**
  291. * Tests event dispatching with a return value
  292. *
  293. * @return void
  294. * @triggers fake.event
  295. */
  296. public function testDispatchReturnValue()
  297. {
  298. $this->skipIf(
  299. version_compare(\PHPUnit_Runner_Version::id(), '3.7', '<'),
  300. 'These tests fail in PHPUnit 3.6'
  301. );
  302. $manager = new EventManager;
  303. $listener = $this->getMock(__NAMESPACE__ . '\EventTestListener');
  304. $anotherListener = $this->getMock(__NAMESPACE__ . '\EventTestListener');
  305. $manager->attach([$listener, 'listenerFunction'], 'fake.event');
  306. $manager->attach([$anotherListener, 'listenerFunction'], 'fake.event');
  307. $event = new Event('fake.event');
  308. $listener->expects($this->at(0))->method('listenerFunction')
  309. ->with($event)
  310. ->will($this->returnValue('something special'));
  311. $anotherListener->expects($this->at(0))
  312. ->method('listenerFunction')
  313. ->with($event);
  314. $manager->dispatch($event);
  315. $this->assertEquals('something special', $event->result);
  316. }
  317. /**
  318. * Tests that returning false in a callback stops the event
  319. *
  320. * @return void
  321. * @triggers fake.event
  322. */
  323. public function testDispatchFalseStopsEvent()
  324. {
  325. $this->skipIf(
  326. version_compare(\PHPUnit_Runner_Version::id(), '3.7', '<'),
  327. 'These tests fail in PHPUnit 3.6'
  328. );
  329. $manager = new EventManager();
  330. $listener = $this->getMock(__NAMESPACE__ . '\EventTestListener');
  331. $anotherListener = $this->getMock(__NAMESPACE__ . '\EventTestListener');
  332. $manager->attach([$listener, 'listenerFunction'], 'fake.event');
  333. $manager->attach([$anotherListener, 'listenerFunction'], 'fake.event');
  334. $event = new Event('fake.event');
  335. $listener->expects($this->at(0))->method('listenerFunction')
  336. ->with($event)
  337. ->will($this->returnValue(false));
  338. $anotherListener->expects($this->never())
  339. ->method('listenerFunction');
  340. $manager->dispatch($event);
  341. $this->assertTrue($event->isStopped());
  342. }
  343. /**
  344. * Tests event dispatching using priorities
  345. *
  346. * @return void
  347. * @triggers fake.event
  348. */
  349. public function testDispatchPrioritized()
  350. {
  351. $manager = new EventManager();
  352. $listener = new EventTestListener;
  353. $manager->attach([$listener, 'listenerFunction'], 'fake.event');
  354. $manager->attach([$listener, 'secondListenerFunction'], 'fake.event', ['priority' => 5]);
  355. $event = new Event('fake.event');
  356. $manager->dispatch($event);
  357. $expected = ['secondListenerFunction', 'listenerFunction'];
  358. $this->assertEquals($expected, $listener->callStack);
  359. }
  360. /**
  361. * Tests subscribing a listener object and firing the events it subscribed to
  362. *
  363. * @return void
  364. * @triggers fake.event
  365. * @triggers another.event $this, array(some => data)
  366. */
  367. public function testAttachSubscriber()
  368. {
  369. $manager = new EventManager();
  370. $listener = $this->getMock(__NAMESPACE__ . '\CustomTestEventListenerInterface', ['secondListenerFunction']);
  371. $manager->attach($listener);
  372. $event = new Event('fake.event');
  373. $manager->dispatch($event);
  374. $expected = ['listenerFunction'];
  375. $this->assertEquals($expected, $listener->callStack);
  376. $event = new Event('another.event', $this, ['some' => 'data']);
  377. $listener->expects($this->at(0))
  378. ->method('secondListenerFunction')
  379. ->with($event, 'data');
  380. $manager->dispatch($event);
  381. }
  382. /**
  383. * Test implementedEvents binding multiple callbacks to the same event name.
  384. *
  385. * @return void
  386. * @triggers multiple.handlers
  387. */
  388. public function testAttachSubscriberMultiple()
  389. {
  390. $manager = new EventManager();
  391. $listener = $this->getMock(__NAMESPACE__ . '\CustomTestEventListenerInterface', ['listenerFunction', 'thirdListenerFunction']);
  392. $manager->attach($listener);
  393. $event = new Event('multiple.handlers');
  394. $listener->expects($this->once())
  395. ->method('listenerFunction')
  396. ->with($event);
  397. $listener->expects($this->once())
  398. ->method('thirdListenerFunction')
  399. ->with($event);
  400. $manager->dispatch($event);
  401. }
  402. /**
  403. * Tests subscribing a listener object and firing the events it subscribed to
  404. *
  405. * @return void
  406. */
  407. public function testDetachSubscriber()
  408. {
  409. $manager = new EventManager();
  410. $listener = $this->getMock(__NAMESPACE__ . '\CustomTestEventListenerInterface', ['secondListenerFunction']);
  411. $manager->attach($listener);
  412. $expected = [
  413. ['callable' => [$listener, 'secondListenerFunction']]
  414. ];
  415. $this->assertEquals($expected, $manager->listeners('another.event'));
  416. $expected = [
  417. ['callable' => [$listener, 'listenerFunction']]
  418. ];
  419. $this->assertEquals($expected, $manager->listeners('fake.event'));
  420. $manager->detach($listener);
  421. $this->assertEquals([], $manager->listeners('fake.event'));
  422. $this->assertEquals([], $manager->listeners('another.event'));
  423. }
  424. /**
  425. * Tests that it is possible to get/set the manager singleton
  426. *
  427. * @return void
  428. */
  429. public function testGlobalDispatcherGetter()
  430. {
  431. $this->assertInstanceOf('Cake\Event\EventManager', EventManager::instance());
  432. $manager = new EventManager();
  433. EventManager::instance($manager);
  434. $this->assertSame($manager, EventManager::instance());
  435. }
  436. /**
  437. * Tests that the global event manager gets the event too from any other manager
  438. *
  439. * @return void
  440. * @triggers fake.event
  441. */
  442. public function testDispatchWithGlobal()
  443. {
  444. $generalManager = $this->getMock('Cake\Event\EventManager', ['prioritisedListeners']);
  445. $manager = new EventManager();
  446. $event = new Event('fake.event');
  447. EventManager::instance($generalManager);
  448. $generalManager->expects($this->once())->method('prioritisedListeners')->with('fake.event');
  449. $manager->dispatch($event);
  450. EventManager::instance(new EventManager());
  451. }
  452. /**
  453. * Tests that stopping an event will not notify the rest of the listeners
  454. *
  455. * @return void
  456. * @triggers fake.event
  457. */
  458. public function testStopPropagation()
  459. {
  460. $generalManager = $this->getMock('Cake\Event\EventManager');
  461. $manager = new EventManager();
  462. $listener = new EventTestListener();
  463. EventManager::instance($generalManager);
  464. $generalManager->expects($this->any())
  465. ->method('prioritisedListeners')
  466. ->with('fake.event')
  467. ->will($this->returnValue([]));
  468. $manager->attach([$listener, 'listenerFunction'], 'fake.event');
  469. $manager->attach([$listener, 'stopListener'], 'fake.event', ['priority' => 8]);
  470. $manager->attach([$listener, 'secondListenerFunction'], 'fake.event', ['priority' => 5]);
  471. $event = new Event('fake.event');
  472. $manager->dispatch($event);
  473. $expected = ['secondListenerFunction'];
  474. $this->assertEquals($expected, $listener->callStack);
  475. EventManager::instance(new EventManager());
  476. }
  477. /**
  478. * Tests event dispatching using priorities
  479. *
  480. * @return void
  481. * @triggers fake.event
  482. */
  483. public function testDispatchPrioritizedWithGlobal()
  484. {
  485. $generalManager = $this->getMock('Cake\Event\EventManager');
  486. $manager = new EventManager();
  487. $listener = new CustomTestEventListenerInterface();
  488. $event = new Event('fake.event');
  489. EventManager::instance($generalManager);
  490. $generalManager->expects($this->any())
  491. ->method('prioritisedListeners')
  492. ->with('fake.event')
  493. ->will($this->returnValue(
  494. [11 => [
  495. ['callable' => [$listener, 'secondListenerFunction']]
  496. ]]
  497. ));
  498. $manager->attach([$listener, 'listenerFunction'], 'fake.event');
  499. $manager->attach([$listener, 'thirdListenerFunction'], 'fake.event', ['priority' => 15]);
  500. $manager->dispatch($event);
  501. $expected = ['listenerFunction', 'secondListenerFunction', 'thirdListenerFunction'];
  502. $this->assertEquals($expected, $listener->callStack);
  503. EventManager::instance(new EventManager());
  504. }
  505. /**
  506. * Tests event dispatching using priorities
  507. *
  508. * @return void
  509. * @triggers fake.event
  510. */
  511. public function testDispatchGlobalBeforeLocal()
  512. {
  513. $generalManager = $this->getMock('Cake\Event\EventManager');
  514. $manager = new EventManager();
  515. $listener = new CustomTestEventListenerInterface();
  516. $event = new Event('fake.event');
  517. EventManager::instance($generalManager);
  518. $generalManager->expects($this->any())
  519. ->method('prioritisedListeners')
  520. ->with('fake.event')
  521. ->will($this->returnValue(
  522. [10 => [
  523. ['callable' => [$listener, 'listenerFunction']]
  524. ]]
  525. ));
  526. $manager->attach([$listener, 'secondListenerFunction'], 'fake.event');
  527. $manager->dispatch($event);
  528. $expected = ['listenerFunction', 'secondListenerFunction'];
  529. $this->assertEquals($expected, $listener->callStack);
  530. EventManager::instance(new EventManager());
  531. }
  532. /**
  533. * test callback
  534. */
  535. public function onMyEvent($event)
  536. {
  537. $event->data['callback'] = 'ok';
  538. }
  539. /**
  540. * Tests events dispatched by a local manager can be handled by
  541. * handler registered in the global event manager
  542. * @triggers my_event $manager
  543. */
  544. public function testDispatchLocalHandledByGlobal()
  545. {
  546. $callback = [$this, 'onMyEvent'];
  547. EventManager::instance()->attach($callback, 'my_event');
  548. $manager = new EventManager();
  549. $event = new Event('my_event', $manager);
  550. $manager->dispatch($event);
  551. $this->assertEquals('ok', $event->data['callback']);
  552. }
  553. /**
  554. * Test that events are dispatched properly when there are global and local
  555. * listeners at the same priority.
  556. *
  557. * @return void
  558. * @triggers fake.event $this)
  559. */
  560. public function testDispatchWithGlobalAndLocalEvents()
  561. {
  562. $listener = new CustomTestEventListenerInterface();
  563. EventManager::instance()->attach($listener);
  564. $listener2 = new EventTestListener();
  565. $manager = new EventManager();
  566. $manager->attach([$listener2, 'listenerFunction'], 'fake.event');
  567. $manager->dispatch(new Event('fake.event', $this));
  568. $this->assertEquals(['listenerFunction'], $listener->callStack);
  569. $this->assertEquals(['listenerFunction'], $listener2->callStack);
  570. }
  571. }