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\EventDispatcher\Tests\CallableClass;
10 use Symfony\Component\EventDispatcher\Tests\TestEventListener;
11 use Symfony\Component\EventDispatcher\Tests\ContainerAwareEventDispatcherTest as SymfonyContainerAwareEventDispatcherTest;
14 * Unit tests for the ContainerAwareEventDispatcher.
16 * NOTE: 98% of this code is a literal copy of Symfony's emerging
17 * CompiledEventDispatcherTest.
19 * This file does NOT follow Drupal coding standards, so as to simplify future
22 * @see https://github.com/symfony/symfony/pull/12521
24 * @group EventDispatcher
26 class ContainerAwareEventDispatcherTest extends SymfonyContainerAwareEventDispatcherTest
28 protected function createEventDispatcher()
30 $container = new Container();
32 return new ContainerAwareEventDispatcher($container);
35 public function testGetListenersWithCallables()
37 // When passing in callables exclusively as listeners into the event
38 // dispatcher constructor, the event dispatcher must not attempt to
39 // resolve any services.
40 $container = $this->getMock('Symfony\Component\DependencyInjection\IntrospectableContainerInterface');
41 $container->expects($this->never())->method($this->anything());
43 $firstListener = new CallableClass();
44 $secondListener = function () {};
45 $thirdListener = array(new TestEventListener(), 'preFoo');
47 'test_event' => array(
49 array('callable' => $firstListener),
50 array('callable' => $secondListener),
51 array('callable' => $thirdListener),
56 $dispatcher = new ContainerAwareEventDispatcher($container, $listeners);
57 $actualListeners = $dispatcher->getListeners();
59 $expectedListeners = array(
60 'test_event' => array(
67 $this->assertSame($expectedListeners, $actualListeners);
70 public function testDispatchWithCallables()
72 // When passing in callables exclusively as listeners into the event
73 // dispatcher constructor, the event dispatcher must not attempt to
74 // resolve any services.
75 $container = $this->getMock('Symfony\Component\DependencyInjection\IntrospectableContainerInterface');
76 $container->expects($this->never())->method($this->anything());
78 $firstListener = new CallableClass();
79 $secondListener = function () {};
80 $thirdListener = array(new TestEventListener(), 'preFoo');
82 'test_event' => array(
84 array('callable' => $firstListener),
85 array('callable' => $secondListener),
86 array('callable' => $thirdListener),
91 $dispatcher = new ContainerAwareEventDispatcher($container, $listeners);
92 $dispatcher->dispatch('test_event');
94 $this->assertTrue($thirdListener[0]->preFooInvoked);
97 public function testGetListenersWithServices()
99 $container = new ContainerBuilder();
100 $container->register('listener_service', 'Symfony\Component\EventDispatcher\Tests\TestEventListener');
103 'test_event' => array(
105 array('service' => array('listener_service', 'preFoo')),
110 $dispatcher = new ContainerAwareEventDispatcher($container, $listeners);
111 $actualListeners = $dispatcher->getListeners();
113 $listenerService = $container->get('listener_service');
114 $expectedListeners = array(
115 'test_event' => array(
116 array($listenerService, 'preFoo'),
120 $this->assertSame($expectedListeners, $actualListeners);
123 public function testDispatchWithServices()
125 $container = new ContainerBuilder();
126 $container->register('listener_service', 'Symfony\Component\EventDispatcher\Tests\TestEventListener');
129 'test_event' => array(
131 array('service' => array('listener_service', 'preFoo')),
136 $dispatcher = new ContainerAwareEventDispatcher($container, $listeners);
138 $dispatcher->dispatch('test_event');
140 $listenerService = $container->get('listener_service');
141 $this->assertTrue($listenerService->preFooInvoked);
144 public function testRemoveService()
146 $container = new ContainerBuilder();
147 $container->register('listener_service', 'Symfony\Component\EventDispatcher\Tests\TestEventListener');
148 $container->register('other_listener_service', 'Symfony\Component\EventDispatcher\Tests\TestEventListener');
151 'test_event' => array(
153 array('service' => array('listener_service', 'preFoo')),
154 array('service' => array('other_listener_service', 'preFoo')),
159 $dispatcher = new ContainerAwareEventDispatcher($container, $listeners);
161 $listenerService = $container->get('listener_service');
162 $dispatcher->removeListener('test_event', array($listenerService, 'preFoo'));
164 // Ensure that other service was not initialized during removal of the
166 $this->assertFalse($container->initialized('other_listener_service'));
168 $dispatcher->dispatch('test_event');
170 $this->assertFalse($listenerService->preFooInvoked);
171 $otherService = $container->get('other_listener_service');
172 $this->assertTrue($otherService->preFooInvoked);
175 public function testGetListenerPriorityWithServices()
177 $container = new ContainerBuilder();
178 $container->register('listener_service', TestEventListener::class);
181 'test_event' => array(
183 array('service' => array('listener_service', 'preFoo')),
188 $dispatcher = new ContainerAwareEventDispatcher($container, $listeners);
189 $listenerService = $container->get('listener_service');
190 $actualPriority = $dispatcher->getListenerPriority('test_event', [$listenerService, 'preFoo']);
192 $this->assertSame(5, $actualPriority);