AbstractEventDispatcherTest.php 13 KB

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