2 // @codingStandardsIgnoreFile
4 namespace Drupal\Tests\Component\EventDispatcher;
6 use Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher;
7 use Symfony\Component\DependencyInjection\Container;
8 use Symfony\Component\DependencyInjection\ContainerBuilder;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10 use Symfony\Component\EventDispatcher\Tests\CallableClass;
11 use Symfony\Component\EventDispatcher\Tests\ContainerAwareEventDispatcherTest as SymfonyContainerAwareEventDispatcherTest;
12 use Symfony\Component\EventDispatcher\Tests\TestEventListener;
15 * Unit tests for the ContainerAwareEventDispatcher.
17 * NOTE: 98% of this code is a literal copy of Symfony's emerging
18 * CompiledEventDispatcherTest.
20 * This file does NOT follow Drupal coding standards, so as to simplify future
23 * @see https://github.com/symfony/symfony/pull/12521
25 * @group EventDispatcher
27 class ContainerAwareEventDispatcherTest extends SymfonyContainerAwareEventDispatcherTest
29 protected function createEventDispatcher()
31 $container = new Container();
33 return new ContainerAwareEventDispatcher($container);
36 public function testGetListenersWithCallables()
38 // When passing in callables exclusively as listeners into the event
39 // dispatcher constructor, the event dispatcher must not attempt to
40 // resolve any services.
41 $container = $this->getMock(ContainerInterface::class);
42 $container->expects($this->never())->method($this->anything());
44 $firstListener = new CallableClass();
45 $secondListener = function () {};
46 $thirdListener = array(new TestEventListener(), 'preFoo');
48 'test_event' => array(
50 array('callable' => $firstListener),
51 array('callable' => $secondListener),
52 array('callable' => $thirdListener),
57 $dispatcher = new ContainerAwareEventDispatcher($container, $listeners);
58 $actualListeners = $dispatcher->getListeners();
60 $expectedListeners = array(
61 'test_event' => array(
68 $this->assertSame($expectedListeners, $actualListeners);
71 public function testDispatchWithCallables()
73 // When passing in callables exclusively as listeners into the event
74 // dispatcher constructor, the event dispatcher must not attempt to
75 // resolve any services.
76 $container = $this->getMock(ContainerInterface::class);
77 $container->expects($this->never())->method($this->anything());
79 $firstListener = new CallableClass();
80 $secondListener = function () {};
81 $thirdListener = array(new TestEventListener(), 'preFoo');
83 'test_event' => array(
85 array('callable' => $firstListener),
86 array('callable' => $secondListener),
87 array('callable' => $thirdListener),
92 $dispatcher = new ContainerAwareEventDispatcher($container, $listeners);
93 $dispatcher->dispatch('test_event');
95 $this->assertTrue($thirdListener[0]->preFooInvoked);
98 public function testGetListenersWithServices()
100 $container = new ContainerBuilder();
101 $container->register('listener_service', 'Symfony\Component\EventDispatcher\Tests\TestEventListener');
104 'test_event' => array(
106 array('service' => array('listener_service', 'preFoo')),
111 $dispatcher = new ContainerAwareEventDispatcher($container, $listeners);
112 $actualListeners = $dispatcher->getListeners();
114 $listenerService = $container->get('listener_service');
115 $expectedListeners = array(
116 'test_event' => array(
117 array($listenerService, 'preFoo'),
121 $this->assertSame($expectedListeners, $actualListeners);
124 public function testDispatchWithServices()
126 $container = new ContainerBuilder();
127 $container->register('listener_service', 'Symfony\Component\EventDispatcher\Tests\TestEventListener');
130 'test_event' => array(
132 array('service' => array('listener_service', 'preFoo')),
137 $dispatcher = new ContainerAwareEventDispatcher($container, $listeners);
139 $dispatcher->dispatch('test_event');
141 $listenerService = $container->get('listener_service');
142 $this->assertTrue($listenerService->preFooInvoked);
145 public function testRemoveService()
147 $container = new ContainerBuilder();
148 $container->register('listener_service', 'Symfony\Component\EventDispatcher\Tests\TestEventListener');
149 $container->register('other_listener_service', 'Symfony\Component\EventDispatcher\Tests\TestEventListener');
152 'test_event' => array(
154 array('service' => array('listener_service', 'preFoo')),
155 array('service' => array('other_listener_service', 'preFoo')),
160 $dispatcher = new ContainerAwareEventDispatcher($container, $listeners);
162 $listenerService = $container->get('listener_service');
163 $dispatcher->removeListener('test_event', array($listenerService, 'preFoo'));
165 // Ensure that other service was not initialized during removal of the
167 $this->assertFalse($container->initialized('other_listener_service'));
169 $dispatcher->dispatch('test_event');
171 $this->assertFalse($listenerService->preFooInvoked);
172 $otherService = $container->get('other_listener_service');
173 $this->assertTrue($otherService->preFooInvoked);
176 public function testGetListenerPriorityWithServices()
178 $container = new ContainerBuilder();
179 $container->register('listener_service', TestEventListener::class);
182 'test_event' => array(
184 array('service' => array('listener_service', 'preFoo')),
189 $dispatcher = new ContainerAwareEventDispatcher($container, $listeners);
190 $listenerService = $container->get('listener_service');
191 $actualPriority = $dispatcher->getListenerPriority('test_event', [$listenerService, 'preFoo']);
193 $this->assertSame(5, $actualPriority);