comparison core/tests/Drupal/Tests/Component/EventDispatcher/ContainerAwareEventDispatcherTest.php @ 0:c75dbcec494b

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