AbstractEventDispatcherTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. /**
  118. * @group legacy
  119. */
  120. public function testLegacyDispatch()
  121. {
  122. $event = new Event();
  123. $this->dispatcher->dispatch(self::preFoo, $event);
  124. $this->assertEquals('pre.foo', $event->getName());
  125. }
  126. public function testDispatchForClosure()
  127. {
  128. $invoked = 0;
  129. $listener = function () use (&$invoked) {
  130. ++$invoked;
  131. };
  132. $this->dispatcher->addListener('pre.foo', $listener);
  133. $this->dispatcher->addListener('post.foo', $listener);
  134. $this->dispatcher->dispatch(self::preFoo);
  135. $this->assertEquals(1, $invoked);
  136. }
  137. public function testStopEventPropagation()
  138. {
  139. $otherListener = new TestEventListener();
  140. // postFoo() stops the propagation, so only one listener should
  141. // be executed
  142. // Manually set priority to enforce $this->listener to be called first
  143. $this->dispatcher->addListener('post.foo', array($this->listener, 'postFoo'), 10);
  144. $this->dispatcher->addListener('post.foo', array($otherListener, 'postFoo'));
  145. $this->dispatcher->dispatch(self::postFoo);
  146. $this->assertTrue($this->listener->postFooInvoked);
  147. $this->assertFalse($otherListener->postFooInvoked);
  148. }
  149. public function testDispatchByPriority()
  150. {
  151. $invoked = array();
  152. $listener1 = function () use (&$invoked) {
  153. $invoked[] = '1';
  154. };
  155. $listener2 = function () use (&$invoked) {
  156. $invoked[] = '2';
  157. };
  158. $listener3 = function () use (&$invoked) {
  159. $invoked[] = '3';
  160. };
  161. $this->dispatcher->addListener('pre.foo', $listener1, -10);
  162. $this->dispatcher->addListener('pre.foo', $listener2);
  163. $this->dispatcher->addListener('pre.foo', $listener3, 10);
  164. $this->dispatcher->dispatch(self::preFoo);
  165. $this->assertEquals(array('3', '2', '1'), $invoked);
  166. }
  167. public function testRemoveListener()
  168. {
  169. $this->dispatcher->addListener('pre.bar', $this->listener);
  170. $this->assertTrue($this->dispatcher->hasListeners(self::preBar));
  171. $this->dispatcher->removeListener('pre.bar', $this->listener);
  172. $this->assertFalse($this->dispatcher->hasListeners(self::preBar));
  173. $this->dispatcher->removeListener('notExists', $this->listener);
  174. }
  175. public function testAddSubscriber()
  176. {
  177. $eventSubscriber = new TestEventSubscriber();
  178. $this->dispatcher->addSubscriber($eventSubscriber);
  179. $this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
  180. $this->assertTrue($this->dispatcher->hasListeners(self::postFoo));
  181. }
  182. public function testAddSubscriberWithPriorities()
  183. {
  184. $eventSubscriber = new TestEventSubscriber();
  185. $this->dispatcher->addSubscriber($eventSubscriber);
  186. $eventSubscriber = new TestEventSubscriberWithPriorities();
  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->assertInstanceOf('Symfony\Component\EventDispatcher\Tests\TestEventSubscriberWithPriorities', $listeners[0][0]);
  192. }
  193. public function testAddSubscriberWithMultipleListeners()
  194. {
  195. $eventSubscriber = new TestEventSubscriberWithMultipleListeners();
  196. $this->dispatcher->addSubscriber($eventSubscriber);
  197. $listeners = $this->dispatcher->getListeners('pre.foo');
  198. $this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
  199. $this->assertCount(2, $listeners);
  200. $this->assertEquals('preFoo2', $listeners[0][1]);
  201. }
  202. public function testRemoveSubscriber()
  203. {
  204. $eventSubscriber = new TestEventSubscriber();
  205. $this->dispatcher->addSubscriber($eventSubscriber);
  206. $this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
  207. $this->assertTrue($this->dispatcher->hasListeners(self::postFoo));
  208. $this->dispatcher->removeSubscriber($eventSubscriber);
  209. $this->assertFalse($this->dispatcher->hasListeners(self::preFoo));
  210. $this->assertFalse($this->dispatcher->hasListeners(self::postFoo));
  211. }
  212. public function testRemoveSubscriberWithPriorities()
  213. {
  214. $eventSubscriber = new TestEventSubscriberWithPriorities();
  215. $this->dispatcher->addSubscriber($eventSubscriber);
  216. $this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
  217. $this->dispatcher->removeSubscriber($eventSubscriber);
  218. $this->assertFalse($this->dispatcher->hasListeners(self::preFoo));
  219. }
  220. public function testRemoveSubscriberWithMultipleListeners()
  221. {
  222. $eventSubscriber = new TestEventSubscriberWithMultipleListeners();
  223. $this->dispatcher->addSubscriber($eventSubscriber);
  224. $this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
  225. $this->assertCount(2, $this->dispatcher->getListeners(self::preFoo));
  226. $this->dispatcher->removeSubscriber($eventSubscriber);
  227. $this->assertFalse($this->dispatcher->hasListeners(self::preFoo));
  228. }
  229. /**
  230. * @group legacy
  231. */
  232. public function testLegacyEventReceivesTheDispatcherInstance()
  233. {
  234. $dispatcher = null;
  235. $this->dispatcher->addListener('test', function ($event) use (&$dispatcher) {
  236. $dispatcher = $event->getDispatcher();
  237. });
  238. $this->dispatcher->dispatch('test');
  239. $this->assertSame($this->dispatcher, $dispatcher);
  240. }
  241. public function testEventReceivesTheDispatcherInstanceAsArgument()
  242. {
  243. $listener = new TestWithDispatcher();
  244. $this->dispatcher->addListener('test', array($listener, 'foo'));
  245. $this->assertNull($listener->name);
  246. $this->assertNull($listener->dispatcher);
  247. $this->dispatcher->dispatch('test');
  248. $this->assertEquals('test', $listener->name);
  249. $this->assertSame($this->dispatcher, $listener->dispatcher);
  250. }
  251. /**
  252. * @see https://bugs.php.net/bug.php?id=62976
  253. *
  254. * This bug affects:
  255. * - The PHP 5.3 branch for versions < 5.3.18
  256. * - The PHP 5.4 branch for versions < 5.4.8
  257. * - The PHP 5.5 branch is not affected
  258. */
  259. public function testWorkaroundForPhpBug62976()
  260. {
  261. $dispatcher = $this->createEventDispatcher();
  262. $dispatcher->addListener('bug.62976', new CallableClass());
  263. $dispatcher->removeListener('bug.62976', function () {});
  264. $this->assertTrue($dispatcher->hasListeners('bug.62976'));
  265. }
  266. public function testHasListenersWhenAddedCallbackListenerIsRemoved()
  267. {
  268. $listener = function () {};
  269. $this->dispatcher->addListener('foo', $listener);
  270. $this->dispatcher->removeListener('foo', $listener);
  271. $this->assertFalse($this->dispatcher->hasListeners());
  272. }
  273. public function testGetListenersWhenAddedCallbackListenerIsRemoved()
  274. {
  275. $listener = function () {};
  276. $this->dispatcher->addListener('foo', $listener);
  277. $this->dispatcher->removeListener('foo', $listener);
  278. $this->assertSame(array(), $this->dispatcher->getListeners());
  279. }
  280. public function testHasListenersWithoutEventsReturnsFalseAfterHasListenersWithEventHasBeenCalled()
  281. {
  282. $this->assertFalse($this->dispatcher->hasListeners('foo'));
  283. $this->assertFalse($this->dispatcher->hasListeners());
  284. }
  285. }
  286. class CallableClass
  287. {
  288. public function __invoke()
  289. {
  290. }
  291. }
  292. class TestEventListener
  293. {
  294. public $preFooInvoked = false;
  295. public $postFooInvoked = false;
  296. /* Listener methods */
  297. public function preFoo(Event $e)
  298. {
  299. $this->preFooInvoked = true;
  300. }
  301. public function postFoo(Event $e)
  302. {
  303. $this->postFooInvoked = true;
  304. $e->stopPropagation();
  305. }
  306. }
  307. class TestWithDispatcher
  308. {
  309. public $name;
  310. public $dispatcher;
  311. public function foo(Event $e, $name, $dispatcher)
  312. {
  313. $this->name = $name;
  314. $this->dispatcher = $dispatcher;
  315. }
  316. }
  317. class TestEventSubscriber implements EventSubscriberInterface
  318. {
  319. public static function getSubscribedEvents()
  320. {
  321. return array('pre.foo' => 'preFoo', 'post.foo' => 'postFoo');
  322. }
  323. }
  324. class TestEventSubscriberWithPriorities implements EventSubscriberInterface
  325. {
  326. public static function getSubscribedEvents()
  327. {
  328. return array(
  329. 'pre.foo' => array('preFoo', 10),
  330. 'post.foo' => array('postFoo'),
  331. );
  332. }
  333. }
  334. class TestEventSubscriberWithMultipleListeners implements EventSubscriberInterface
  335. {
  336. public static function getSubscribedEvents()
  337. {
  338. return array('pre.foo' => array(
  339. array('preFoo1'),
  340. array('preFoo2', 10),
  341. ));
  342. }
  343. }