ContainerAwareEventDispatcherTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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\EventDispatcher\ContainerAwareEventDispatcher;
  13. use Symfony\Component\EventDispatcher\Event;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. /**
  16. * @group legacy
  17. */
  18. class ContainerAwareEventDispatcherTest extends AbstractEventDispatcherTest
  19. {
  20. protected function createEventDispatcher()
  21. {
  22. $container = new Container();
  23. return new ContainerAwareEventDispatcher($container);
  24. }
  25. public function testAddAListenerService()
  26. {
  27. $event = new Event();
  28. $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
  29. $service
  30. ->expects($this->once())
  31. ->method('onEvent')
  32. ->with($event)
  33. ;
  34. $container = new Container();
  35. $container->set('service.listener', $service);
  36. $dispatcher = new ContainerAwareEventDispatcher($container);
  37. $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
  38. $dispatcher->dispatch('onEvent', $event);
  39. }
  40. public function testAddASubscriberService()
  41. {
  42. $event = new Event();
  43. $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\SubscriberService')->getMock();
  44. $service
  45. ->expects($this->once())
  46. ->method('onEvent')
  47. ->with($event)
  48. ;
  49. $service
  50. ->expects($this->once())
  51. ->method('onEventWithPriority')
  52. ->with($event)
  53. ;
  54. $service
  55. ->expects($this->once())
  56. ->method('onEventNested')
  57. ->with($event)
  58. ;
  59. $container = new Container();
  60. $container->set('service.subscriber', $service);
  61. $dispatcher = new ContainerAwareEventDispatcher($container);
  62. $dispatcher->addSubscriberService('service.subscriber', 'Symfony\Component\EventDispatcher\Tests\SubscriberService');
  63. $dispatcher->dispatch('onEvent', $event);
  64. $dispatcher->dispatch('onEventWithPriority', $event);
  65. $dispatcher->dispatch('onEventNested', $event);
  66. }
  67. public function testPreventDuplicateListenerService()
  68. {
  69. $event = new Event();
  70. $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
  71. $service
  72. ->expects($this->once())
  73. ->method('onEvent')
  74. ->with($event)
  75. ;
  76. $container = new Container();
  77. $container->set('service.listener', $service);
  78. $dispatcher = new ContainerAwareEventDispatcher($container);
  79. $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'), 5);
  80. $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'), 10);
  81. $dispatcher->dispatch('onEvent', $event);
  82. }
  83. public function testHasListenersOnLazyLoad()
  84. {
  85. $event = new Event();
  86. $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
  87. $container = new Container();
  88. $container->set('service.listener', $service);
  89. $dispatcher = new ContainerAwareEventDispatcher($container);
  90. $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
  91. $service
  92. ->expects($this->once())
  93. ->method('onEvent')
  94. ->with($event)
  95. ;
  96. $this->assertTrue($dispatcher->hasListeners());
  97. if ($dispatcher->hasListeners('onEvent')) {
  98. $dispatcher->dispatch('onEvent');
  99. }
  100. }
  101. public function testGetListenersOnLazyLoad()
  102. {
  103. $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
  104. $container = new Container();
  105. $container->set('service.listener', $service);
  106. $dispatcher = new ContainerAwareEventDispatcher($container);
  107. $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
  108. $listeners = $dispatcher->getListeners();
  109. $this->assertTrue(isset($listeners['onEvent']));
  110. $this->assertCount(1, $dispatcher->getListeners('onEvent'));
  111. }
  112. public function testRemoveAfterDispatch()
  113. {
  114. $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
  115. $container = new Container();
  116. $container->set('service.listener', $service);
  117. $dispatcher = new ContainerAwareEventDispatcher($container);
  118. $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
  119. $dispatcher->dispatch('onEvent', new Event());
  120. $dispatcher->removeListener('onEvent', array($container->get('service.listener'), 'onEvent'));
  121. $this->assertFalse($dispatcher->hasListeners('onEvent'));
  122. }
  123. public function testRemoveBeforeDispatch()
  124. {
  125. $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
  126. $container = new Container();
  127. $container->set('service.listener', $service);
  128. $dispatcher = new ContainerAwareEventDispatcher($container);
  129. $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
  130. $dispatcher->removeListener('onEvent', array($container->get('service.listener'), 'onEvent'));
  131. $this->assertFalse($dispatcher->hasListeners('onEvent'));
  132. }
  133. }
  134. class Service
  135. {
  136. public function onEvent(Event $e)
  137. {
  138. }
  139. }
  140. class SubscriberService implements EventSubscriberInterface
  141. {
  142. public static function getSubscribedEvents()
  143. {
  144. return array(
  145. 'onEvent' => 'onEvent',
  146. 'onEventWithPriority' => array('onEventWithPriority', 10),
  147. 'onEventNested' => array(array('onEventNested')),
  148. );
  149. }
  150. public function onEvent(Event $e)
  151. {
  152. }
  153. public function onEventWithPriority(Event $e)
  154. {
  155. }
  156. public function onEventNested(Event $e)
  157. {
  158. }
  159. }