AbstractEventDispatcherTest.php 16KB

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