ContainerAwareEventDispatcherTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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->getMock('Symfony\Component\EventDispatcher\Tests\Service');
  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->getMock('Symfony\Component\EventDispatcher\Tests\SubscriberService');
  42. $service
  43. ->expects($this->once())
  44. ->method('onEvent')
  45. ->with($event)
  46. ;
  47. $container = new Container();
  48. $container->set('service.subscriber', $service);
  49. $dispatcher = new ContainerAwareEventDispatcher($container);
  50. $dispatcher->addSubscriberService('service.subscriber', 'Symfony\Component\EventDispatcher\Tests\SubscriberService');
  51. $dispatcher->dispatch('onEvent', $event);
  52. }
  53. public function testPreventDuplicateListenerService()
  54. {
  55. $event = new Event();
  56. $service = $this->getMock('Symfony\Component\EventDispatcher\Tests\Service');
  57. $service
  58. ->expects($this->once())
  59. ->method('onEvent')
  60. ->with($event)
  61. ;
  62. $container = new Container();
  63. $container->set('service.listener', $service);
  64. $dispatcher = new ContainerAwareEventDispatcher($container);
  65. $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'), 5);
  66. $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'), 10);
  67. $dispatcher->dispatch('onEvent', $event);
  68. }
  69. /**
  70. * @expectedException \InvalidArgumentException
  71. */
  72. public function testTriggerAListenerServiceOutOfScope()
  73. {
  74. $service = $this->getMock('Symfony\Component\EventDispatcher\Tests\Service');
  75. $scope = new Scope('scope');
  76. $container = new Container();
  77. $container->addScope($scope);
  78. $container->enterScope('scope');
  79. $container->set('service.listener', $service, 'scope');
  80. $dispatcher = new ContainerAwareEventDispatcher($container);
  81. $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
  82. $container->leaveScope('scope');
  83. $dispatcher->dispatch('onEvent');
  84. }
  85. public function testReEnteringAScope()
  86. {
  87. $event = new Event();
  88. $service1 = $this->getMock('Symfony\Component\EventDispatcher\Tests\Service');
  89. $service1
  90. ->expects($this->exactly(2))
  91. ->method('onEvent')
  92. ->with($event)
  93. ;
  94. $scope = new Scope('scope');
  95. $container = new Container();
  96. $container->addScope($scope);
  97. $container->enterScope('scope');
  98. $container->set('service.listener', $service1, 'scope');
  99. $dispatcher = new ContainerAwareEventDispatcher($container);
  100. $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
  101. $dispatcher->dispatch('onEvent', $event);
  102. $service2 = $this->getMock('Symfony\Component\EventDispatcher\Tests\Service');
  103. $service2
  104. ->expects($this->once())
  105. ->method('onEvent')
  106. ->with($event)
  107. ;
  108. $container->enterScope('scope');
  109. $container->set('service.listener', $service2, 'scope');
  110. $dispatcher->dispatch('onEvent', $event);
  111. $container->leaveScope('scope');
  112. $dispatcher->dispatch('onEvent');
  113. }
  114. public function testHasListenersOnLazyLoad()
  115. {
  116. $event = new Event();
  117. $service = $this->getMock('Symfony\Component\EventDispatcher\Tests\Service');
  118. $container = new Container();
  119. $container->set('service.listener', $service);
  120. $dispatcher = new ContainerAwareEventDispatcher($container);
  121. $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
  122. $event->setDispatcher($dispatcher);
  123. $event->setName('onEvent');
  124. $service
  125. ->expects($this->once())
  126. ->method('onEvent')
  127. ->with($event)
  128. ;
  129. $this->assertTrue($dispatcher->hasListeners());
  130. if ($dispatcher->hasListeners('onEvent')) {
  131. $dispatcher->dispatch('onEvent');
  132. }
  133. }
  134. public function testGetListenersOnLazyLoad()
  135. {
  136. $service = $this->getMock('Symfony\Component\EventDispatcher\Tests\Service');
  137. $container = new Container();
  138. $container->set('service.listener', $service);
  139. $dispatcher = new ContainerAwareEventDispatcher($container);
  140. $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
  141. $listeners = $dispatcher->getListeners();
  142. $this->assertTrue(isset($listeners['onEvent']));
  143. $this->assertCount(1, $dispatcher->getListeners('onEvent'));
  144. }
  145. public function testRemoveAfterDispatch()
  146. {
  147. $service = $this->getMock('Symfony\Component\EventDispatcher\Tests\Service');
  148. $container = new Container();
  149. $container->set('service.listener', $service);
  150. $dispatcher = new ContainerAwareEventDispatcher($container);
  151. $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
  152. $dispatcher->dispatch('onEvent', new Event());
  153. $dispatcher->removeListener('onEvent', array($container->get('service.listener'), 'onEvent'));
  154. $this->assertFalse($dispatcher->hasListeners('onEvent'));
  155. }
  156. public function testRemoveBeforeDispatch()
  157. {
  158. $service = $this->getMock('Symfony\Component\EventDispatcher\Tests\Service');
  159. $container = new Container();
  160. $container->set('service.listener', $service);
  161. $dispatcher = new ContainerAwareEventDispatcher($container);
  162. $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
  163. $dispatcher->removeListener('onEvent', array($container->get('service.listener'), 'onEvent'));
  164. $this->assertFalse($dispatcher->hasListeners('onEvent'));
  165. }
  166. }
  167. class Service
  168. {
  169. public function onEvent(Event $e)
  170. {
  171. }
  172. }
  173. class SubscriberService implements EventSubscriberInterface
  174. {
  175. public static function getSubscribedEvents()
  176. {
  177. return array(
  178. 'onEvent' => array('onEvent'),
  179. );
  180. }
  181. public function onEvent(Event $e)
  182. {
  183. }
  184. }