EventManagerTest.php 18 KB

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