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