ContainerAwareEventDispatcherTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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\DependencyInjection\Container;
  12. use Symfony\Component\DependencyInjection\Scope;
  13. use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher;
  14. use Symfony\Component\EventDispatcher\Event;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. class ContainerAwareEventDispatcherTest extends AbstractEventDispatcherTest
  17. {
  18. protected function createEventDispatcher()
  19. {
  20. $container = new Container();
  21. return new ContainerAwareEventDispatcher($container);
  22. }
  23. public function testAddAListenerService()
  24. {
  25. $event = new Event();
  26. $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
  27. $service
  28. ->expects($this->once())
  29. ->method('onEvent')
  30. ->with($event)
  31. ;
  32. $container = new Container();
  33. $container->set('service.listener', $service);
  34. $dispatcher = new ContainerAwareEventDispatcher($container);
  35. $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
  36. $dispatcher->dispatch('onEvent', $event);
  37. }
  38. public function testAddASubscriberService()
  39. {
  40. $event = new Event();
  41. $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\SubscriberService')->getMock();
  42. $service
  43. ->expects($this->once())
  44. ->method('onEvent')
  45. ->with($event)
  46. ;
  47. $service
  48. ->expects($this->once())
  49. ->method('onEventWithPriority')
  50. ->with($event)
  51. ;
  52. $service
  53. ->expects($this->once())
  54. ->method('onEventNested')
  55. ->with($event)
  56. ;
  57. $container = new Container();
  58. $container->set('service.subscriber', $service);
  59. $dispatcher = new ContainerAwareEventDispatcher($container);
  60. $dispatcher->addSubscriberService('service.subscriber', 'Symfony\Component\EventDispatcher\Tests\SubscriberService');
  61. $dispatcher->dispatch('onEvent', $event);
  62. $dispatcher->dispatch('onEventWithPriority', $event);
  63. $dispatcher->dispatch('onEventNested', $event);
  64. }
  65. public function testPreventDuplicateListenerService()
  66. {
  67. $event = new Event();
  68. $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
  69. $service
  70. ->expects($this->once())
  71. ->method('onEvent')
  72. ->with($event)
  73. ;
  74. $container = new Container();
  75. $container->set('service.listener', $service);
  76. $dispatcher = new ContainerAwareEventDispatcher($container);
  77. $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'), 5);
  78. $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'), 10);
  79. $dispatcher->dispatch('onEvent', $event);
  80. }
  81. /**
  82. * @expectedException \InvalidArgumentException
  83. * @group legacy
  84. */
  85. public function testTriggerAListenerServiceOutOfScope()
  86. {
  87. $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
  88. $scope = new Scope('scope');
  89. $container = new Container();
  90. $container->addScope($scope);
  91. $container->enterScope('scope');
  92. $container->set('service.listener', $service, 'scope');
  93. $dispatcher = new ContainerAwareEventDispatcher($container);
  94. $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
  95. $container->leaveScope('scope');
  96. $dispatcher->dispatch('onEvent');
  97. }
  98. /**
  99. * @group legacy
  100. */
  101. public function testReEnteringAScope()
  102. {
  103. $event = new Event();
  104. $service1 = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
  105. $service1
  106. ->expects($this->exactly(2))
  107. ->method('onEvent')
  108. ->with($event)
  109. ;
  110. $scope = new Scope('scope');
  111. $container = new Container();
  112. $container->addScope($scope);
  113. $container->enterScope('scope');
  114. $container->set('service.listener', $service1, 'scope');
  115. $dispatcher = new ContainerAwareEventDispatcher($container);
  116. $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
  117. $dispatcher->dispatch('onEvent', $event);
  118. $service2 = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
  119. $service2
  120. ->expects($this->once())
  121. ->method('onEvent')
  122. ->with($event)
  123. ;
  124. $container->enterScope('scope');
  125. $container->set('service.listener', $service2, 'scope');
  126. $dispatcher->dispatch('onEvent', $event);
  127. $container->leaveScope('scope');
  128. $dispatcher->dispatch('onEvent');
  129. }
  130. public function testHasListenersOnLazyLoad()
  131. {
  132. $event = new Event();
  133. $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
  134. $container = new Container();
  135. $container->set('service.listener', $service);
  136. $dispatcher = new ContainerAwareEventDispatcher($container);
  137. $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
  138. $event->setDispatcher($dispatcher);
  139. $event->setName('onEvent');
  140. $service
  141. ->expects($this->once())
  142. ->method('onEvent')
  143. ->with($event)
  144. ;
  145. $this->assertTrue($dispatcher->hasListeners());
  146. if ($dispatcher->hasListeners('onEvent')) {
  147. $dispatcher->dispatch('onEvent');
  148. }
  149. }
  150. public function testGetListenersOnLazyLoad()
  151. {
  152. $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
  153. $container = new Container();
  154. $container->set('service.listener', $service);
  155. $dispatcher = new ContainerAwareEventDispatcher($container);
  156. $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
  157. $listeners = $dispatcher->getListeners();
  158. $this->assertArrayHasKey('onEvent', $listeners);
  159. $this->assertCount(1, $dispatcher->getListeners('onEvent'));
  160. }
  161. public function testRemoveAfterDispatch()
  162. {
  163. $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
  164. $container = new Container();
  165. $container->set('service.listener', $service);
  166. $dispatcher = new ContainerAwareEventDispatcher($container);
  167. $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
  168. $dispatcher->dispatch('onEvent', new Event());
  169. $dispatcher->removeListener('onEvent', array($container->get('service.listener'), 'onEvent'));
  170. $this->assertFalse($dispatcher->hasListeners('onEvent'));
  171. }
  172. public function testRemoveBeforeDispatch()
  173. {
  174. $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
  175. $container = new Container();
  176. $container->set('service.listener', $service);
  177. $dispatcher = new ContainerAwareEventDispatcher($container);
  178. $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
  179. $dispatcher->removeListener('onEvent', array($container->get('service.listener'), 'onEvent'));
  180. $this->assertFalse($dispatcher->hasListeners('onEvent'));
  181. }
  182. }
  183. class Service
  184. {
  185. public function onEvent(Event $e)
  186. {
  187. }
  188. }
  189. class SubscriberService implements EventSubscriberInterface
  190. {
  191. public static function getSubscribedEvents()
  192. {
  193. return array(
  194. 'onEvent' => 'onEvent',
  195. 'onEventWithPriority' => array('onEventWithPriority', 10),
  196. 'onEventNested' => array(array('onEventNested')),
  197. );
  198. }
  199. public function onEvent(Event $e)
  200. {
  201. }
  202. public function onEventWithPriority(Event $e)
  203. {
  204. }
  205. public function onEventNested(Event $e)
  206. {
  207. }
  208. }