EventDispatcherTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. public function testInitialState()
  29. {
  30. $this->assertEquals(array(), $this->dispatcher->getListeners());
  31. $this->assertFalse($this->dispatcher->hasListeners(self::preFoo));
  32. $this->assertFalse($this->dispatcher->hasListeners(self::postFoo));
  33. }
  34. public function testAddListener()
  35. {
  36. $this->dispatcher->addListener('pre.foo', array($this->listener, 'preFoo'));
  37. $this->dispatcher->addListener('post.foo', array($this->listener, 'postFoo'));
  38. $this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
  39. $this->assertTrue($this->dispatcher->hasListeners(self::postFoo));
  40. $this->assertEquals(1, count($this->dispatcher->getListeners(self::preFoo)));
  41. $this->assertEquals(1, count($this->dispatcher->getListeners(self::postFoo)));
  42. $this->assertEquals(2, count($this->dispatcher->getListeners()));
  43. }
  44. public function testGetListenersSortsByPriority()
  45. {
  46. $listener1 = new TestEventListener();
  47. $listener2 = new TestEventListener();
  48. $listener3 = new TestEventListener();
  49. $listener1->name = '1';
  50. $listener2->name = '2';
  51. $listener3->name = '3';
  52. $this->dispatcher->addListener('pre.foo', array($listener1, 'preFoo'), -10);
  53. $this->dispatcher->addListener('pre.foo', array($listener2, 'preFoo'), 10);
  54. $this->dispatcher->addListener('pre.foo', array($listener3, 'preFoo'));
  55. $expected = array(
  56. array($listener2, 'preFoo'),
  57. array($listener3, 'preFoo'),
  58. array($listener1, 'preFoo'),
  59. );
  60. $this->assertSame($expected, $this->dispatcher->getListeners('pre.foo'));
  61. }
  62. public function testGetAllListenersSortsByPriority()
  63. {
  64. $listener1 = new TestEventListener();
  65. $listener2 = new TestEventListener();
  66. $listener3 = new TestEventListener();
  67. $listener4 = new TestEventListener();
  68. $listener5 = new TestEventListener();
  69. $listener6 = new TestEventListener();
  70. $this->dispatcher->addListener('pre.foo', $listener1, -10);
  71. $this->dispatcher->addListener('pre.foo', $listener2);
  72. $this->dispatcher->addListener('pre.foo', $listener3, 10);
  73. $this->dispatcher->addListener('post.foo', $listener4, -10);
  74. $this->dispatcher->addListener('post.foo', $listener5);
  75. $this->dispatcher->addListener('post.foo', $listener6, 10);
  76. $expected = array(
  77. 'pre.foo' => array($listener3, $listener2, $listener1),
  78. 'post.foo' => array($listener6, $listener5, $listener4),
  79. );
  80. $this->assertSame($expected, $this->dispatcher->getListeners());
  81. }
  82. public function testDispatch()
  83. {
  84. $this->dispatcher->addListener('pre.foo', array($this->listener, 'preFoo'));
  85. $this->dispatcher->addListener('post.foo', array($this->listener, 'postFoo'));
  86. $this->dispatcher->dispatch(self::preFoo);
  87. $this->assertTrue($this->listener->preFooInvoked);
  88. $this->assertFalse($this->listener->postFooInvoked);
  89. }
  90. public function testDispatchForClosure()
  91. {
  92. $invoked = 0;
  93. $listener = function () use (&$invoked) {
  94. $invoked++;
  95. };
  96. $this->dispatcher->addListener('pre.foo', $listener);
  97. $this->dispatcher->addListener('post.foo', $listener);
  98. $this->dispatcher->dispatch(self::preFoo);
  99. $this->assertEquals(1, $invoked);
  100. }
  101. public function testStopEventPropagation()
  102. {
  103. $otherListener = new TestEventListener();
  104. // postFoo() stops the propagation, so only one listener should
  105. // be executed
  106. // Manually set priority to enforce $this->listener to be called first
  107. $this->dispatcher->addListener('post.foo', array($this->listener, 'postFoo'), 10);
  108. $this->dispatcher->addListener('post.foo', array($otherListener, 'preFoo'));
  109. $this->dispatcher->dispatch(self::postFoo);
  110. $this->assertTrue($this->listener->postFooInvoked);
  111. $this->assertFalse($otherListener->postFooInvoked);
  112. }
  113. public function testDispatchByPriority()
  114. {
  115. $invoked = array();
  116. $listener1 = function () use (&$invoked) {
  117. $invoked[] = '1';
  118. };
  119. $listener2 = function () use (&$invoked) {
  120. $invoked[] = '2';
  121. };
  122. $listener3 = function () use (&$invoked) {
  123. $invoked[] = '3';
  124. };
  125. $this->dispatcher->addListener('pre.foo', $listener1, -10);
  126. $this->dispatcher->addListener('pre.foo', $listener2);
  127. $this->dispatcher->addListener('pre.foo', $listener3, 10);
  128. $this->dispatcher->dispatch(self::preFoo);
  129. $this->assertEquals(array('3', '2', '1'), $invoked);
  130. }
  131. public function testRemoveListener()
  132. {
  133. $this->dispatcher->addListener('pre.bar', $this->listener);
  134. $this->assertTrue($this->dispatcher->hasListeners(self::preBar));
  135. $this->dispatcher->removeListener('pre.bar', $this->listener);
  136. $this->assertFalse($this->dispatcher->hasListeners(self::preBar));
  137. $this->dispatcher->removeListener('notExists', $this->listener);
  138. }
  139. public function testAddSubscriber()
  140. {
  141. $eventSubscriber = new TestEventSubscriber();
  142. $this->dispatcher->addSubscriber($eventSubscriber);
  143. $this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
  144. $this->assertTrue($this->dispatcher->hasListeners(self::postFoo));
  145. }
  146. public function testAddSubscriberWithPriorities()
  147. {
  148. $eventSubscriber = new TestEventSubscriber();
  149. $this->dispatcher->addSubscriber($eventSubscriber);
  150. $eventSubscriber = new TestEventSubscriberWithPriorities();
  151. $this->dispatcher->addSubscriber($eventSubscriber);
  152. $listeners = $this->dispatcher->getListeners('pre.foo');
  153. $this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
  154. $this->assertEquals(2, count($listeners));
  155. $this->assertInstanceOf('Symfony\Tests\Component\EventDispatcher\TestEventSubscriberWithPriorities', $listeners[0][0]);
  156. }
  157. public function testRemoveSubscriber()
  158. {
  159. $eventSubscriber = new TestEventSubscriber();
  160. $this->dispatcher->addSubscriber($eventSubscriber);
  161. $this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
  162. $this->assertTrue($this->dispatcher->hasListeners(self::postFoo));
  163. $this->dispatcher->removeSubscriber($eventSubscriber);
  164. $this->assertFalse($this->dispatcher->hasListeners(self::preFoo));
  165. $this->assertFalse($this->dispatcher->hasListeners(self::postFoo));
  166. }
  167. }
  168. class TestEventListener
  169. {
  170. public $preFooInvoked = false;
  171. public $postFooInvoked = false;
  172. /* Listener methods */
  173. public function preFoo(Event $e)
  174. {
  175. $this->preFooInvoked = true;
  176. }
  177. public function postFoo(Event $e)
  178. {
  179. $this->postFooInvoked = true;
  180. $e->stopPropagation();
  181. }
  182. }
  183. class TestEventSubscriber implements EventSubscriberInterface
  184. {
  185. public static function getSubscribedEvents()
  186. {
  187. return array('pre.foo' => 'preFoo', 'post.foo' => 'postFoo');
  188. }
  189. }
  190. class TestEventSubscriberWithPriorities implements EventSubscriberInterface
  191. {
  192. public static function getSubscribedEvents()
  193. {
  194. return array('pre.foo' => array('preFoo', 10));
  195. }
  196. }