EventDispatcherTest.php 11 KB

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