AbstractEventDispatcherTest.php 13 KB

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