ContainerAwareEventDispatcherTest.php 7.6 KB

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