These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /* |
||
4 | * Copyright 2011 Johannes M. Schmitt <[email protected]> |
||
5 | * |
||
6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
||
7 | * you may not use this file except in compliance with the License. |
||
8 | * You may obtain a copy of the License at |
||
9 | * |
||
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
||
11 | * |
||
12 | * Unless required by applicable law or agreed to in writing, software |
||
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
||
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
15 | * See the License for the specific language governing permissions and |
||
16 | * limitations under the License. |
||
17 | */ |
||
18 | |||
19 | namespace JMS\SerializerBundle\Tests\DependencyInjection; |
||
20 | |||
21 | use JMS\SerializerBundle\DependencyInjection\Compiler\RegisterEventListenersAndSubscribersPass; |
||
22 | use JMS\SerializerBundle\DependencyInjection\JMSSerializerExtension; |
||
23 | use Symfony\Component\DependencyInjection\Compiler\RemoveUnusedDefinitionsPass; |
||
24 | use Symfony\Component\DependencyInjection\Compiler\ResolveDefinitionTemplatesPass; |
||
25 | use Symfony\Component\DependencyInjection\Compiler\ResolveParameterPlaceHoldersPass; |
||
26 | use Symfony\Component\DependencyInjection\ContainerBuilder; |
||
27 | use Symfony\Component\DependencyInjection\Definition; |
||
28 | |||
29 | class EventSubscribersAndListenersPassTest extends \PHPUnit_Framework_TestCase |
||
30 | { |
||
31 | /** |
||
32 | * @param array $configs |
||
33 | * @return ContainerBuilder |
||
34 | */ |
||
35 | View Code Duplication | private function getContainer(array $configs = array()) |
|
0 ignored issues
–
show
|
|||
36 | { |
||
37 | $loader = new JMSSerializerExtension(); |
||
38 | $container = new ContainerBuilder(); |
||
39 | |||
40 | $container->getCompilerPassConfig()->setOptimizationPasses(array( |
||
41 | new ResolveParameterPlaceHoldersPass(), |
||
42 | new ResolveDefinitionTemplatesPass(), |
||
43 | )); |
||
44 | $container->getCompilerPassConfig()->setRemovingPasses(array(new RemoveUnusedDefinitionsPass())); |
||
45 | |||
46 | $container->setParameter('kernel.debug', true); |
||
47 | $container->setParameter('kernel.cache_dir', sys_get_temp_dir() . '/serializer'); |
||
48 | $container->setParameter('kernel.bundles', array()); |
||
49 | $container->setParameter('kernel.bundles', array()); |
||
50 | |||
51 | $loader->load(['jms_serializer' => $configs], $container); |
||
52 | |||
53 | |||
54 | // remove other listeners |
||
55 | foreach (array_keys($container->findTaggedServiceIds('jms_serializer.event_listener')) as $id) { |
||
56 | $container->removeDefinition($id); |
||
57 | } |
||
58 | // remove other subscribers |
||
59 | foreach (array_keys($container->findTaggedServiceIds('jms_serializer.event_subscriber')) as $id) { |
||
60 | $container->removeDefinition($id); |
||
61 | } |
||
62 | |||
63 | return $container; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @expectedException RuntimeException |
||
68 | */ |
||
69 | View Code Duplication | public function testEventListenerMustHaveEventDefined() |
|
70 | { |
||
71 | $container = $this->getContainer(); |
||
72 | |||
73 | $def = new Definition('Foo'); |
||
74 | $def->addTag('jms_serializer.event_listener', [ |
||
75 | 'class' => 'Bar', |
||
76 | ]); |
||
77 | |||
78 | $container->setDefinition('my_listener', $def); |
||
79 | |||
80 | $pass = new RegisterEventListenersAndSubscribersPass(); |
||
81 | $pass->process($container); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @expectedException RuntimeException |
||
86 | */ |
||
87 | View Code Duplication | public function testEventListenerMustBePublic() |
|
88 | { |
||
89 | $container = $this->getContainer(); |
||
90 | |||
91 | $def = new Definition('Foo'); |
||
92 | $def->setPublic(false); |
||
93 | $def->addTag('jms_serializer.event_listener'); |
||
94 | |||
95 | $container->setDefinition('my_listener', $def); |
||
96 | |||
97 | $pass = new RegisterEventListenersAndSubscribersPass(); |
||
98 | $pass->process($container); |
||
99 | } |
||
100 | |||
101 | View Code Duplication | public function testEventListener() |
|
102 | { |
||
103 | $container = $this->getContainer(); |
||
104 | |||
105 | $def = new Definition('Foo'); |
||
106 | $def->addTag('jms_serializer.event_listener', [ |
||
107 | 'event' => 'serializer.pre_serialize', |
||
108 | 'class' => 'Bar', |
||
109 | ]); |
||
110 | |||
111 | $container->setDefinition('my_listener', $def); |
||
112 | |||
113 | $pass = new RegisterEventListenersAndSubscribersPass(); |
||
114 | $pass->process($container); |
||
115 | |||
116 | $dispatcher = $container->getDefinition('jms_serializer.event_dispatcher'); |
||
117 | $methodCalls = $dispatcher->getMethodCalls(); |
||
118 | |||
119 | $called = false; |
||
120 | foreach ($methodCalls as $call) { |
||
121 | if ($call[0] === 'setListeners') { |
||
122 | $called = true; |
||
123 | $this->assertEquals([ |
||
124 | 'serializer.pre_serialize' => [ |
||
125 | [ |
||
126 | ['my_listener', 'onserializerpreserialize'], |
||
127 | 'bar', |
||
128 | null |
||
129 | ] |
||
130 | ]], $call[1][0]); |
||
131 | } |
||
132 | } |
||
133 | |||
134 | if (!$called) { |
||
135 | $this->fail("The method setListeners was not invoked on the jms_serializer.event_dispatcher"); |
||
136 | } |
||
137 | } |
||
138 | |||
139 | View Code Duplication | public function testEventListenerWithParams() |
|
140 | { |
||
141 | $container = $this->getContainer(); |
||
142 | |||
143 | $container->setParameter('bar', 'Bar'); |
||
144 | |||
145 | $def = new Definition('Foo'); |
||
146 | $def->addTag('jms_serializer.event_listener', [ |
||
147 | 'event' => 'serializer.pre_serialize', |
||
148 | 'class' => '%bar%', |
||
149 | ]); |
||
150 | |||
151 | $container->setDefinition('my_listener', $def); |
||
152 | |||
153 | $pass = new RegisterEventListenersAndSubscribersPass(); |
||
154 | $pass->process($container); |
||
155 | |||
156 | $dispatcher = $container->getDefinition('jms_serializer.event_dispatcher'); |
||
157 | $methodCalls = $dispatcher->getMethodCalls(); |
||
158 | |||
159 | $called = false; |
||
160 | foreach ($methodCalls as $call) { |
||
161 | if ($call[0] === 'setListeners') { |
||
162 | $called = true; |
||
163 | $this->assertEquals([ |
||
164 | 'serializer.pre_serialize' => [ |
||
165 | [ |
||
166 | ['my_listener', 'onserializerpreserialize'], |
||
167 | 'bar', |
||
168 | null |
||
169 | ] |
||
170 | ]], $call[1][0]); |
||
171 | } |
||
172 | } |
||
173 | |||
174 | if (!$called) { |
||
175 | $this->fail("The method setListeners was not invoked on the jms_serializer.event_dispatcher"); |
||
176 | } |
||
177 | } |
||
178 | |||
179 | public function testEventSubscriber() |
||
180 | { |
||
181 | $container = $this->getContainer(); |
||
182 | |||
183 | $def = new Definition('JMS\SerializerBundle\Tests\DependencyInjection\Fixture\EventSubscriber'); |
||
184 | $def->addTag('jms_serializer.event_subscriber'); |
||
185 | |||
186 | $container->setDefinition('my_listener', $def); |
||
187 | |||
188 | $pass = new RegisterEventListenersAndSubscribersPass(); |
||
189 | $pass->process($container); |
||
190 | |||
191 | $dispatcher = $container->getDefinition('jms_serializer.event_dispatcher'); |
||
192 | $methodCalls = $dispatcher->getMethodCalls(); |
||
193 | |||
194 | $called = false; |
||
195 | foreach ($methodCalls as $call) { |
||
196 | if ($call[0] === 'setListeners') { |
||
197 | $called = true; |
||
198 | $this->assertEquals([ |
||
199 | 'serializer.pre_serialize' => [ |
||
200 | [ |
||
201 | ['my_listener', 'onserializerpreserialize'], |
||
202 | 'bar', |
||
203 | 'json' |
||
204 | ] |
||
205 | ]], $call[1][0]); |
||
206 | } |
||
207 | } |
||
208 | |||
209 | if (!$called) { |
||
210 | $this->fail("The method setListeners was not invoked on the jms_serializer.event_dispatcher"); |
||
211 | } |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * @expectedException RuntimeException |
||
216 | */ |
||
217 | View Code Duplication | public function testEventSubscriberInterface() |
|
218 | { |
||
219 | $container = $this->getContainer(); |
||
220 | |||
221 | $def = new Definition('JMS\SerializerBundle\Tests\DependencyInjection\Fixture\VersionedObject'); |
||
222 | $def->addTag('jms_serializer.event_subscriber'); |
||
223 | |||
224 | $container->setDefinition('my_listener', $def); |
||
225 | |||
226 | $pass = new RegisterEventListenersAndSubscribersPass(); |
||
227 | $pass->process($container); |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * @expectedException RuntimeException |
||
232 | */ |
||
233 | View Code Duplication | public function testEventSubscriberMustBePublic() |
|
234 | { |
||
235 | $container = $this->getContainer(); |
||
236 | |||
237 | $def = new Definition('JMS\SerializerBundle\Tests\DependencyInjection\Fixture\VersionedObject'); |
||
238 | $def->setPublic(false); |
||
239 | $def->addTag('jms_serializer.event_subscriber'); |
||
240 | |||
241 | $container->setDefinition('my_listener', $def); |
||
242 | |||
243 | $pass = new RegisterEventListenersAndSubscribersPass(); |
||
244 | $pass->process($container); |
||
245 | } |
||
246 | } |
||
247 | |||
248 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.