Mercurial > hg > isophonics-drupal-site
comparison core/tests/Drupal/Tests/Component/EventDispatcher/ContainerAwareEventDispatcherTest.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 1fec387a4317 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
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->getMock(ContainerInterface::class); | |
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->getMock(ContainerInterface::class); | |
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 } |