EventDispatcherTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\EventDispatcher\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\EventDispatcher\Event;
  13. use Symfony\Component\EventDispatcher\EventDispatcher;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. class EventDispatcherTest extends TestCase
  16. {
  17. /* Some pseudo events */
  18. const preFoo = 'pre.foo';
  19. const postFoo = 'post.foo';
  20. const preBar = 'pre.bar';
  21. const postBar = 'post.bar';
  22. /**
  23. * @var EventDispatcher
  24. */
  25. private $dispatcher;
  26. private $listener;
  27. protected function setUp()
  28. {
  29. $this->dispatcher = $this->createEventDispatcher();
  30. $this->listener = new TestEventListener();
  31. }
  32. protected function tearDown()
  33. {
  34. $this->dispatcher = null;
  35. $this->listener = null;
  36. }
  37. protected function createEventDispatcher()
  38. {
  39. return new EventDispatcher();
  40. }
  41. public function testInitialState()
  42. {
  43. $this->assertEquals([], $this->dispatcher->getListeners());
  44. $this->assertFalse($this->dispatcher->hasListeners(self::preFoo));
  45. $this->assertFalse($this->dispatcher->hasListeners(self::postFoo));
  46. }
  47. public function testAddListener()
  48. {
  49. $this->dispatcher->addListener('pre.foo', [$this->listener, 'preFoo']);
  50. $this->dispatcher->addListener('post.foo', [$this->listener, 'postFoo']);
  51. $this->assertTrue($this->dispatcher->hasListeners());
  52. $this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
  53. $this->assertTrue($this->dispatcher->hasListeners(self::postFoo));
  54. $this->assertCount(1, $this->dispatcher->getListeners(self::preFoo));
  55. $this->assertCount(1, $this->dispatcher->getListeners(self::postFoo));
  56. $this->assertCount(2, $this->dispatcher->getListeners());
  57. }
  58. public function testGetListenersSortsByPriority()
  59. {
  60. $listener1 = new TestEventListener();
  61. $listener2 = new TestEventListener();
  62. $listener3 = new TestEventListener();
  63. $listener1->name = '1';
  64. $listener2->name = '2';
  65. $listener3->name = '3';
  66. $this->dispatcher->addListener('pre.foo', [$listener1, 'preFoo'], -10);
  67. $this->dispatcher->addListener('pre.foo', [$listener2, 'preFoo'], 10);
  68. $this->dispatcher->addListener('pre.foo', [$listener3, 'preFoo']);
  69. $expected = [
  70. [$listener2, 'preFoo'],
  71. [$listener3, 'preFoo'],
  72. [$listener1, 'preFoo'],
  73. ];
  74. $this->assertSame($expected, $this->dispatcher->getListeners('pre.foo'));
  75. }
  76. public function testGetAllListenersSortsByPriority()
  77. {
  78. $listener1 = new TestEventListener();
  79. $listener2 = new TestEventListener();
  80. $listener3 = new TestEventListener();
  81. $listener4 = new TestEventListener();
  82. $listener5 = new TestEventListener();
  83. $listener6 = new TestEventListener();
  84. $this->dispatcher->addListener('pre.foo', $listener1, -10);
  85. $this->dispatcher->addListener('pre.foo', $listener2);
  86. $this->dispatcher->addListener('pre.foo', $listener3, 10);
  87. $this->dispatcher->addListener('post.foo', $listener4, -10);
  88. $this->dispatcher->addListener('post.foo', $listener5);
  89. $this->dispatcher->addListener('post.foo', $listener6, 10);
  90. $expected = [
  91. 'pre.foo' => [$listener3, $listener2, $listener1],
  92. 'post.foo' => [$listener6, $listener5, $listener4],
  93. ];
  94. $this->assertSame($expected, $this->dispatcher->getListeners());
  95. }
  96. public function testGetListenerPriority()
  97. {
  98. $listener1 = new TestEventListener();
  99. $listener2 = new TestEventListener();
  100. $this->dispatcher->addListener('pre.foo', $listener1, -10);
  101. $this->dispatcher->addListener('pre.foo', $listener2);
  102. $this->assertSame(-10, $this->dispatcher->getListenerPriority('pre.foo', $listener1));
  103. $this->assertSame(0, $this->dispatcher->getListenerPriority('pre.foo', $listener2));
  104. $this->assertNull($this->dispatcher->getListenerPriority('pre.bar', $listener2));
  105. $this->assertNull($this->dispatcher->getListenerPriority('pre.foo', function () {}));
  106. }
  107. public function testDispatch()
  108. {
  109. $this->dispatcher->addListener('pre.foo', [$this->listener, 'preFoo']);
  110. $this->dispatcher->addListener('post.foo', [$this->listener, 'postFoo']);
  111. $this->dispatcher->dispatch(self::preFoo);
  112. $this->assertTrue($this->listener->preFooInvoked);
  113. $this->assertFalse($this->listener->postFooInvoked);
  114. $this->assertInstanceOf('Symfony\Component\EventDispatcher\Event', $this->dispatcher->dispatch('noevent'));
  115. $this->assertInstanceOf('Symfony\Component\EventDispatcher\Event', $this->dispatcher->dispatch(self::preFoo));
  116. $event = new Event();
  117. $return = $this->dispatcher->dispatch(self::preFoo, $event);
  118. $this->assertSame($event, $return);
  119. }
  120. public function testDispatchForClosure()
  121. {
  122. $invoked = 0;
  123. $listener = function () use (&$invoked) {
  124. ++$invoked;
  125. };
  126. $this->dispatcher->addListener('pre.foo', $listener);
  127. $this->dispatcher->addListener('post.foo', $listener);
  128. $this->dispatcher->dispatch(self::preFoo);
  129. $this->assertEquals(1, $invoked);
  130. }
  131. public function testStopEventPropagation()
  132. {
  133. $otherListener = new TestEventListener();
  134. // postFoo() stops the propagation, so only one listener should
  135. // be executed
  136. // Manually set priority to enforce $this->listener to be called first
  137. $this->dispatcher->addListener('post.foo', [$this->listener, 'postFoo'], 10);
  138. $this->dispatcher->addListener('post.foo', [$otherListener, 'postFoo']);
  139. $this->dispatcher->dispatch(self::postFoo);
  140. $this->assertTrue($this->listener->postFooInvoked);
  141. $this->assertFalse($otherListener->postFooInvoked);
  142. }
  143. public function testDispatchByPriority()
  144. {
  145. $invoked = [];
  146. $listener1 = function () use (&$invoked) {
  147. $invoked[] = '1';
  148. };
  149. $listener2 = function () use (&$invoked) {
  150. $invoked[] = '2';
  151. };
  152. $listener3 = function () use (&$invoked) {
  153. $invoked[] = '3';
  154. };
  155. $this->dispatcher->addListener('pre.foo', $listener1, -10);
  156. $this->dispatcher->addListener('pre.foo', $listener2);
  157. $this->dispatcher->addListener('pre.foo', $listener3, 10);
  158. $this->dispatcher->dispatch(self::preFoo);
  159. $this->assertEquals(['3', '2', '1'], $invoked);
  160. }
  161. public function testRemoveListener()
  162. {
  163. $this->dispatcher->addListener('pre.bar', $this->listener);
  164. $this->assertTrue($this->dispatcher->hasListeners(self::preBar));
  165. $this->dispatcher->removeListener('pre.bar', $this->listener);
  166. $this->assertFalse($this->dispatcher->hasListeners(self::preBar));
  167. $this->dispatcher->removeListener('notExists', $this->listener);
  168. }
  169. public function testAddSubscriber()
  170. {
  171. $eventSubscriber = new TestEventSubscriber();
  172. $this->dispatcher->addSubscriber($eventSubscriber);
  173. $this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
  174. $this->assertTrue($this->dispatcher->hasListeners(self::postFoo));
  175. }
  176. public function testAddSubscriberWithPriorities()
  177. {
  178. $eventSubscriber = new TestEventSubscriber();
  179. $this->dispatcher->addSubscriber($eventSubscriber);
  180. $eventSubscriber = new TestEventSubscriberWithPriorities();
  181. $this->dispatcher->addSubscriber($eventSubscriber);
  182. $listeners = $this->dispatcher->getListeners('pre.foo');
  183. $this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
  184. $this->assertCount(2, $listeners);
  185. $this->assertInstanceOf('Symfony\Component\EventDispatcher\Tests\TestEventSubscriberWithPriorities', $listeners[0][0]);
  186. }
  187. public function testAddSubscriberWithMultipleListeners()
  188. {
  189. $eventSubscriber = new TestEventSubscriberWithMultipleListeners();
  190. $this->dispatcher->addSubscriber($eventSubscriber);
  191. $listeners = $this->dispatcher->getListeners('pre.foo');
  192. $this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
  193. $this->assertCount(2, $listeners);
  194. $this->assertEquals('preFoo2', $listeners[0][1]);
  195. }
  196. public function testRemoveSubscriber()
  197. {
  198. $eventSubscriber = new TestEventSubscriber();
  199. $this->dispatcher->addSubscriber($eventSubscriber);
  200. $this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
  201. $this->assertTrue($this->dispatcher->hasListeners(self::postFoo));
  202. $this->dispatcher->removeSubscriber($eventSubscriber);
  203. $this->assertFalse($this->dispatcher->hasListeners(self::preFoo));
  204. $this->assertFalse($this->dispatcher->hasListeners(self::postFoo));
  205. }
  206. public function testRemoveSubscriberWithPriorities()
  207. {
  208. $eventSubscriber = new TestEventSubscriberWithPriorities();
  209. $this->dispatcher->addSubscriber($eventSubscriber);
  210. $this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
  211. $this->dispatcher->removeSubscriber($eventSubscriber);
  212. $this->assertFalse($this->dispatcher->hasListeners(self::preFoo));
  213. }
  214. public function testRemoveSubscriberWithMultipleListeners()
  215. {
  216. $eventSubscriber = new TestEventSubscriberWithMultipleListeners();
  217. $this->dispatcher->addSubscriber($eventSubscriber);
  218. $this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
  219. $this->assertCount(2, $this->dispatcher->getListeners(self::preFoo));
  220. $this->dispatcher->removeSubscriber($eventSubscriber);
  221. $this->assertFalse($this->dispatcher->hasListeners(self::preFoo));
  222. }
  223. public function testEventReceivesTheDispatcherInstanceAsArgument()
  224. {
  225. $listener = new TestWithDispatcher();
  226. $this->dispatcher->addListener('test', [$listener, 'foo']);
  227. $this->assertNull($listener->name);
  228. $this->assertNull($listener->dispatcher);
  229. $this->dispatcher->dispatch('test');
  230. $this->assertEquals('test', $listener->name);
  231. $this->assertSame($this->dispatcher, $listener->dispatcher);
  232. }
  233. /**
  234. * @see https://bugs.php.net/bug.php?id=62976
  235. *
  236. * This bug affects:
  237. * - The PHP 5.3 branch for versions < 5.3.18
  238. * - The PHP 5.4 branch for versions < 5.4.8
  239. * - The PHP 5.5 branch is not affected
  240. */
  241. public function testWorkaroundForPhpBug62976()
  242. {
  243. $dispatcher = $this->createEventDispatcher();
  244. $dispatcher->addListener('bug.62976', new CallableClass());
  245. $dispatcher->removeListener('bug.62976', function () {});
  246. $this->assertTrue($dispatcher->hasListeners('bug.62976'));
  247. }
  248. public function testHasListenersWhenAddedCallbackListenerIsRemoved()
  249. {
  250. $listener = function () {};
  251. $this->dispatcher->addListener('foo', $listener);
  252. $this->dispatcher->removeListener('foo', $listener);
  253. $this->assertFalse($this->dispatcher->hasListeners());
  254. }
  255. public function testGetListenersWhenAddedCallbackListenerIsRemoved()
  256. {
  257. $listener = function () {};
  258. $this->dispatcher->addListener('foo', $listener);
  259. $this->dispatcher->removeListener('foo', $listener);
  260. $this->assertSame([], $this->dispatcher->getListeners());
  261. }
  262. public function testHasListenersWithoutEventsReturnsFalseAfterHasListenersWithEventHasBeenCalled()
  263. {
  264. $this->assertFalse($this->dispatcher->hasListeners('foo'));
  265. $this->assertFalse($this->dispatcher->hasListeners());
  266. }
  267. public function testHasListenersIsLazy()
  268. {
  269. $called = 0;
  270. $listener = [function () use (&$called) { ++$called; }, 'onFoo'];
  271. $this->dispatcher->addListener('foo', $listener);
  272. $this->assertTrue($this->dispatcher->hasListeners());
  273. $this->assertTrue($this->dispatcher->hasListeners('foo'));
  274. $this->assertSame(0, $called);
  275. }
  276. public function testDispatchLazyListener()
  277. {
  278. $called = 0;
  279. $factory = function () use (&$called) {
  280. ++$called;
  281. return new TestWithDispatcher();
  282. };
  283. $this->dispatcher->addListener('foo', [$factory, 'foo']);
  284. $this->assertSame(0, $called);
  285. $this->dispatcher->dispatch('foo', new Event());
  286. $this->dispatcher->dispatch('foo', new Event());
  287. $this->assertSame(1, $called);
  288. }
  289. public function testRemoveFindsLazyListeners()
  290. {
  291. $test = new TestWithDispatcher();
  292. $factory = function () use ($test) { return $test; };
  293. $this->dispatcher->addListener('foo', [$factory, 'foo']);
  294. $this->assertTrue($this->dispatcher->hasListeners('foo'));
  295. $this->dispatcher->removeListener('foo', [$test, 'foo']);
  296. $this->assertFalse($this->dispatcher->hasListeners('foo'));
  297. $this->dispatcher->addListener('foo', [$test, 'foo']);
  298. $this->assertTrue($this->dispatcher->hasListeners('foo'));
  299. $this->dispatcher->removeListener('foo', [$factory, 'foo']);
  300. $this->assertFalse($this->dispatcher->hasListeners('foo'));
  301. }
  302. public function testPriorityFindsLazyListeners()
  303. {
  304. $test = new TestWithDispatcher();
  305. $factory = function () use ($test) { return $test; };
  306. $this->dispatcher->addListener('foo', [$factory, 'foo'], 3);
  307. $this->assertSame(3, $this->dispatcher->getListenerPriority('foo', [$test, 'foo']));
  308. $this->dispatcher->removeListener('foo', [$factory, 'foo']);
  309. $this->dispatcher->addListener('foo', [$test, 'foo'], 5);
  310. $this->assertSame(5, $this->dispatcher->getListenerPriority('foo', [$factory, 'foo']));
  311. }
  312. public function testGetLazyListeners()
  313. {
  314. $test = new TestWithDispatcher();
  315. $factory = function () use ($test) { return $test; };
  316. $this->dispatcher->addListener('foo', [$factory, 'foo'], 3);
  317. $this->assertSame([[$test, 'foo']], $this->dispatcher->getListeners('foo'));
  318. $this->dispatcher->removeListener('foo', [$test, 'foo']);
  319. $this->dispatcher->addListener('bar', [$factory, 'foo'], 3);
  320. $this->assertSame(['bar' => [[$test, 'foo']]], $this->dispatcher->getListeners());
  321. }
  322. public function testMutatingWhilePropagationIsStopped()
  323. {
  324. $testLoaded = false;
  325. $test = new TestEventListener();
  326. $this->dispatcher->addListener('foo', [$test, 'postFoo']);
  327. $this->dispatcher->addListener('foo', [function () use ($test, &$testLoaded) {
  328. $testLoaded = true;
  329. return $test;
  330. }, 'preFoo']);
  331. $this->dispatcher->dispatch('foo');
  332. $this->assertTrue($test->postFooInvoked);
  333. $this->assertFalse($test->preFooInvoked);
  334. $this->assertsame(0, $this->dispatcher->getListenerPriority('foo', [$test, 'preFoo']));
  335. $test->preFoo(new Event());
  336. $this->dispatcher->dispatch('foo');
  337. $this->assertTrue($testLoaded);
  338. }
  339. }
  340. class CallableClass
  341. {
  342. public function __invoke()
  343. {
  344. }
  345. }
  346. class TestEventListener
  347. {
  348. public $preFooInvoked = false;
  349. public $postFooInvoked = false;
  350. /* Listener methods */
  351. public function preFoo(Event $e)
  352. {
  353. $this->preFooInvoked = true;
  354. }
  355. public function postFoo(Event $e)
  356. {
  357. $this->postFooInvoked = true;
  358. if (!$this->preFooInvoked) {
  359. $e->stopPropagation();
  360. }
  361. }
  362. }
  363. class TestWithDispatcher
  364. {
  365. public $name;
  366. public $dispatcher;
  367. public function foo(Event $e, $name, $dispatcher)
  368. {
  369. $this->name = $name;
  370. $this->dispatcher = $dispatcher;
  371. }
  372. }
  373. class TestEventSubscriber implements EventSubscriberInterface
  374. {
  375. public static function getSubscribedEvents()
  376. {
  377. return ['pre.foo' => 'preFoo', 'post.foo' => 'postFoo'];
  378. }
  379. }
  380. class TestEventSubscriberWithPriorities implements EventSubscriberInterface
  381. {
  382. public static function getSubscribedEvents()
  383. {
  384. return [
  385. 'pre.foo' => ['preFoo', 10],
  386. 'post.foo' => ['postFoo'],
  387. ];
  388. }
  389. }
  390. class TestEventSubscriberWithMultipleListeners implements EventSubscriberInterface
  391. {
  392. public static function getSubscribedEvents()
  393. {
  394. return ['pre.foo' => [
  395. ['preFoo1'],
  396. ['preFoo2', 10],
  397. ]];
  398. }
  399. }