ContainerAwareEventDispatcherTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. // @codingStandardsIgnoreFile
  3. namespace Drupal\Tests\Component\EventDispatcher;
  4. use Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher;
  5. use Symfony\Component\DependencyInjection\Container;
  6. use Symfony\Component\DependencyInjection\ContainerBuilder;
  7. use Symfony\Component\DependencyInjection\ContainerInterface;
  8. use Symfony\Component\EventDispatcher\Tests\CallableClass;
  9. use Symfony\Component\EventDispatcher\Tests\ContainerAwareEventDispatcherTest as SymfonyContainerAwareEventDispatcherTest;
  10. use Symfony\Component\EventDispatcher\Tests\TestEventListener;
  11. /**
  12. * Unit tests for the ContainerAwareEventDispatcher.
  13. *
  14. * NOTE: 98% of this code is a literal copy of Symfony's emerging
  15. * CompiledEventDispatcherTest.
  16. *
  17. * This file does NOT follow Drupal coding standards, so as to simplify future
  18. * synchronizations.
  19. *
  20. * @see https://github.com/symfony/symfony/pull/12521
  21. *
  22. * @group EventDispatcher
  23. */
  24. class ContainerAwareEventDispatcherTest extends SymfonyContainerAwareEventDispatcherTest
  25. {
  26. protected function createEventDispatcher()
  27. {
  28. $container = new Container();
  29. return new ContainerAwareEventDispatcher($container);
  30. }
  31. public function testGetListenersWithCallables()
  32. {
  33. // When passing in callables exclusively as listeners into the event
  34. // dispatcher constructor, the event dispatcher must not attempt to
  35. // resolve any services.
  36. $container = $this->getMockBuilder(ContainerInterface::class)->getMock();
  37. $container->expects($this->never())->method($this->anything());
  38. $firstListener = new CallableClass();
  39. $secondListener = function () {};
  40. $thirdListener = array(new TestEventListener(), 'preFoo');
  41. $listeners = array(
  42. 'test_event' => array(
  43. 0 => array(
  44. array('callable' => $firstListener),
  45. array('callable' => $secondListener),
  46. array('callable' => $thirdListener),
  47. ),
  48. ),
  49. );
  50. $dispatcher = new ContainerAwareEventDispatcher($container, $listeners);
  51. $actualListeners = $dispatcher->getListeners();
  52. $expectedListeners = array(
  53. 'test_event' => array(
  54. $firstListener,
  55. $secondListener,
  56. $thirdListener,
  57. ),
  58. );
  59. $this->assertSame($expectedListeners, $actualListeners);
  60. }
  61. public function testDispatchWithCallables()
  62. {
  63. // When passing in callables exclusively as listeners into the event
  64. // dispatcher constructor, the event dispatcher must not attempt to
  65. // resolve any services.
  66. $container = $this->getMockBuilder(ContainerInterface::class)->getMock();
  67. $container->expects($this->never())->method($this->anything());
  68. $firstListener = new CallableClass();
  69. $secondListener = function () {};
  70. $thirdListener = array(new TestEventListener(), 'preFoo');
  71. $listeners = array(
  72. 'test_event' => array(
  73. 0 => array(
  74. array('callable' => $firstListener),
  75. array('callable' => $secondListener),
  76. array('callable' => $thirdListener),
  77. ),
  78. ),
  79. );
  80. $dispatcher = new ContainerAwareEventDispatcher($container, $listeners);
  81. $dispatcher->dispatch('test_event');
  82. $this->assertTrue($thirdListener[0]->preFooInvoked);
  83. }
  84. public function testGetListenersWithServices()
  85. {
  86. $container = new ContainerBuilder();
  87. $container->register('listener_service', 'Symfony\Component\EventDispatcher\Tests\TestEventListener');
  88. $listeners = array(
  89. 'test_event' => array(
  90. 0 => array(
  91. array('service' => array('listener_service', 'preFoo')),
  92. ),
  93. ),
  94. );
  95. $dispatcher = new ContainerAwareEventDispatcher($container, $listeners);
  96. $actualListeners = $dispatcher->getListeners();
  97. $listenerService = $container->get('listener_service');
  98. $expectedListeners = array(
  99. 'test_event' => array(
  100. array($listenerService, 'preFoo'),
  101. ),
  102. );
  103. $this->assertSame($expectedListeners, $actualListeners);
  104. }
  105. public function testDispatchWithServices()
  106. {
  107. $container = new ContainerBuilder();
  108. $container->register('listener_service', 'Symfony\Component\EventDispatcher\Tests\TestEventListener');
  109. $listeners = array(
  110. 'test_event' => array(
  111. 0 => array(
  112. array('service' => array('listener_service', 'preFoo')),
  113. ),
  114. ),
  115. );
  116. $dispatcher = new ContainerAwareEventDispatcher($container, $listeners);
  117. $dispatcher->dispatch('test_event');
  118. $listenerService = $container->get('listener_service');
  119. $this->assertTrue($listenerService->preFooInvoked);
  120. }
  121. public function testRemoveService()
  122. {
  123. $container = new ContainerBuilder();
  124. $container->register('listener_service', 'Symfony\Component\EventDispatcher\Tests\TestEventListener');
  125. $container->register('other_listener_service', 'Symfony\Component\EventDispatcher\Tests\TestEventListener');
  126. $listeners = array(
  127. 'test_event' => array(
  128. 0 => array(
  129. array('service' => array('listener_service', 'preFoo')),
  130. array('service' => array('other_listener_service', 'preFoo')),
  131. ),
  132. ),
  133. );
  134. $dispatcher = new ContainerAwareEventDispatcher($container, $listeners);
  135. $listenerService = $container->get('listener_service');
  136. $dispatcher->removeListener('test_event', array($listenerService, 'preFoo'));
  137. // Ensure that other service was not initialized during removal of the
  138. // listener service.
  139. $this->assertFalse($container->initialized('other_listener_service'));
  140. $dispatcher->dispatch('test_event');
  141. $this->assertFalse($listenerService->preFooInvoked);
  142. $otherService = $container->get('other_listener_service');
  143. $this->assertTrue($otherService->preFooInvoked);
  144. }
  145. public function testGetListenerPriorityWithServices()
  146. {
  147. $container = new ContainerBuilder();
  148. $container->register('listener_service', TestEventListener::class);
  149. $listeners = array(
  150. 'test_event' => array(
  151. 5 => array(
  152. array('service' => array('listener_service', 'preFoo')),
  153. ),
  154. ),
  155. );
  156. $dispatcher = new ContainerAwareEventDispatcher($container, $listeners);
  157. $listenerService = $container->get('listener_service');
  158. $actualPriority = $dispatcher->getListenerPriority('test_event', [$listenerService, 'preFoo']);
  159. $this->assertSame(5, $actualPriority);
  160. }
  161. /**
  162. * @expectedDeprecation The Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher class is deprecated since Symfony 3.3 and will be removed in 4.0. Use EventDispatcher with closure factories instead.
  163. * @group legacy
  164. */
  165. public function testAddAListenerService() {
  166. parent::testAddAListenerService();
  167. }
  168. /**
  169. * @expectedDeprecation The Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher class is deprecated since Symfony 3.3 and will be removed in 4.0. Use EventDispatcher with closure factories instead.
  170. * @group legacy
  171. */
  172. public function testPreventDuplicateListenerService() {
  173. parent::testPreventDuplicateListenerService();
  174. }
  175. /**
  176. * @expectedDeprecation The Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher class is deprecated since Symfony 3.3 and will be removed in 4.0. Use EventDispatcher with closure factories instead.
  177. * @group legacy
  178. */
  179. public function testAddASubscriberService() {
  180. parent::testAddASubscriberService();
  181. }
  182. /**
  183. * @expectedDeprecation The Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher class is deprecated since Symfony 3.3 and will be removed in 4.0. Use EventDispatcher with closure factories instead.
  184. * @group legacy
  185. */
  186. public function testHasListenersOnLazyLoad() {
  187. parent::testHasListenersOnLazyLoad();
  188. }
  189. /**
  190. * @expectedDeprecation The Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher class is deprecated since Symfony 3.3 and will be removed in 4.0. Use EventDispatcher with closure factories instead.
  191. * @group legacy
  192. */
  193. public function testGetListenersOnLazyLoad() {
  194. parent::testGetListenersOnLazyLoad();
  195. }
  196. /**
  197. * @expectedDeprecation The Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher class is deprecated since Symfony 3.3 and will be removed in 4.0. Use EventDispatcher with closure factories instead.
  198. * @group legacy
  199. */
  200. public function testRemoveAfterDispatch() {
  201. parent::testRemoveAfterDispatch();
  202. }
  203. /**
  204. * @expectedDeprecation The Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher class is deprecated since Symfony 3.3 and will be removed in 4.0. Use EventDispatcher with closure factories instead.
  205. * @group legacy
  206. */
  207. public function testRemoveBeforeDispatch() {
  208. parent::testRemoveBeforeDispatch();
  209. }
  210. }