EventDispatcherTest.php 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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\Tests\Component\EventDispatcher;
  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->assertEquals(1, count($this->dispatcher->getListeners(self::preFoo)));
  46. $this->assertEquals(1, count($this->dispatcher->getListeners(self::postFoo)));
  47. $this->assertEquals(2, count($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. }
  95. public function testDispatchForClosure()
  96. {
  97. $invoked = 0;
  98. $listener = function () use (&$invoked) {
  99. $invoked++;
  100. };
  101. $this->dispatcher->addListener('pre.foo', $listener);
  102. $this->dispatcher->addListener('post.foo', $listener);
  103. $this->dispatcher->dispatch(self::preFoo);
  104. $this->assertEquals(1, $invoked);
  105. }
  106. public function testStopEventPropagation()
  107. {
  108. $otherListener = new TestEventListener();
  109. // postFoo() stops the propagation, so only one listener should
  110. // be executed
  111. // Manually set priority to enforce $this->listener to be called first
  112. $this->dispatcher->addListener('post.foo', array($this->listener, 'postFoo'), 10);
  113. $this->dispatcher->addListener('post.foo', array($otherListener, 'preFoo'));
  114. $this->dispatcher->dispatch(self::postFoo);
  115. $this->assertTrue($this->listener->postFooInvoked);
  116. $this->assertFalse($otherListener->postFooInvoked);
  117. }
  118. public function testDispatchByPriority()
  119. {
  120. $invoked = array();
  121. $listener1 = function () use (&$invoked) {
  122. $invoked[] = '1';
  123. };
  124. $listener2 = function () use (&$invoked) {
  125. $invoked[] = '2';
  126. };
  127. $listener3 = function () use (&$invoked) {
  128. $invoked[] = '3';
  129. };
  130. $this->dispatcher->addListener('pre.foo', $listener1, -10);
  131. $this->dispatcher->addListener('pre.foo', $listener2);
  132. $this->dispatcher->addListener('pre.foo', $listener3, 10);
  133. $this->dispatcher->dispatch(self::preFoo);
  134. $this->assertEquals(array('3', '2', '1'), $invoked);
  135. }
  136. public function testRemoveListener()
  137. {
  138. $this->dispatcher->addListener('pre.bar', $this->listener);
  139. $this->assertTrue($this->dispatcher->hasListeners(self::preBar));
  140. $this->dispatcher->removeListener('pre.bar', $this->listener);
  141. $this->assertFalse($this->dispatcher->hasListeners(self::preBar));
  142. $this->dispatcher->removeListener('notExists', $this->listener);
  143. }
  144. public function testAddSubscriber()
  145. {
  146. $eventSubscriber = new TestEventSubscriber();
  147. $this->dispatcher->addSubscriber($eventSubscriber);
  148. $this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
  149. $this->assertTrue($this->dispatcher->hasListeners(self::postFoo));
  150. }
  151. public function testAddSubscriberWithPriorities()
  152. {
  153. $eventSubscriber = new TestEventSubscriber();
  154. $this->dispatcher->addSubscriber($eventSubscriber);
  155. $eventSubscriber = new TestEventSubscriberWithPriorities();
  156. $this->dispatcher->addSubscriber($eventSubscriber);
  157. $listeners = $this->dispatcher->getListeners('pre.foo');
  158. $this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
  159. $this->assertEquals(2, count($listeners));
  160. $this->assertInstanceOf('Symfony\Tests\Component\EventDispatcher\TestEventSubscriberWithPriorities', $listeners[0][0]);
  161. }
  162. public function testRemoveSubscriber()
  163. {
  164. $eventSubscriber = new TestEventSubscriber();
  165. $this->dispatcher->addSubscriber($eventSubscriber);
  166. $this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
  167. $this->assertTrue($this->dispatcher->hasListeners(self::postFoo));
  168. $this->dispatcher->removeSubscriber($eventSubscriber);
  169. $this->assertFalse($this->dispatcher->hasListeners(self::preFoo));
  170. $this->assertFalse($this->dispatcher->hasListeners(self::postFoo));
  171. }
  172. public function testRemoveSubscriberWithPriorities()
  173. {
  174. $eventSubscriber = new TestEventSubscriberWithPriorities();
  175. $this->dispatcher->addSubscriber($eventSubscriber);
  176. $this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
  177. $this->dispatcher->removeSubscriber($eventSubscriber);
  178. $this->assertFalse($this->dispatcher->hasListeners(self::preFoo));
  179. }
  180. }
  181. class TestEventListener
  182. {
  183. public $preFooInvoked = false;
  184. public $postFooInvoked = false;
  185. /* Listener methods */
  186. public function preFoo(Event $e)
  187. {
  188. $this->preFooInvoked = true;
  189. }
  190. public function postFoo(Event $e)
  191. {
  192. $this->postFooInvoked = true;
  193. $e->stopPropagation();
  194. }
  195. }
  196. class TestEventSubscriber implements EventSubscriberInterface
  197. {
  198. public static function getSubscribedEvents()
  199. {
  200. return array('pre.foo' => 'preFoo', 'post.foo' => 'postFoo');
  201. }
  202. }
  203. class TestEventSubscriberWithPriorities implements EventSubscriberInterface
  204. {
  205. public static function getSubscribedEvents()
  206. {
  207. return array('pre.foo' => array('preFoo', 10));
  208. }
  209. }