Няма описание

ContainerAwareEventDispatcherTest.php 6.2KB

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