GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#594)
by Asmir
03:47
created

testEventSubscriber()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 34
rs 8.5806
cc 4
eloc 21
nc 6
nop 0
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 JMS\SerializerBundle\Tests\DependencyInjection\Fixture\SimpleHandler;
24
use PHPUnit\Framework\TestCase;
25
use Symfony\Component\DependencyInjection\Compiler\RemoveUnusedDefinitionsPass;
26
use Symfony\Component\DependencyInjection\Compiler\ResolveDefinitionTemplatesPass;
27
use Symfony\Component\DependencyInjection\Compiler\ResolveParameterPlaceHoldersPass;
28
use Symfony\Component\DependencyInjection\ContainerBuilder;
29
use Symfony\Component\DependencyInjection\Definition;
30
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
31
32
class EventSubscribersAndListenersPassTest extends TestCase
33
{
34
    /**
35
     * @param array $configs
36
     * @return ContainerBuilder
37
     */
38 View Code Duplication
    private function getContainer(array $configs = array())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
39
    {
40
        $loader = new JMSSerializerExtension();
41
        $container = new ContainerBuilder();
42
43
        $container->getCompilerPassConfig()->setOptimizationPasses(array(
44
            new ResolveParameterPlaceHoldersPass(),
45
            new ResolveDefinitionTemplatesPass(),
46
        ));
47
        $container->getCompilerPassConfig()->setRemovingPasses(array(new RemoveUnusedDefinitionsPass()));
48
49
        $container->setParameter('kernel.debug', true);
50
        $container->setParameter('kernel.cache_dir', sys_get_temp_dir() . '/serializer');
51
        $container->setParameter('kernel.bundles', array());
52
        $container->setParameter('kernel.bundles', array());
53
54
        $loader->load(['jms_serializer' => $configs], $container);
55
56
57
        // remove other listeners
58
        foreach (array_keys($container->findTaggedServiceIds('jms_serializer.event_listener')) as $id) {
59
            $container->removeDefinition($id);
60
        }
61
        // remove other subscribers
62
        foreach (array_keys($container->findTaggedServiceIds('jms_serializer.event_subscriber')) as $id) {
63
            $container->removeDefinition($id);
64
        }
65
66
        return $container;
67
    }
68
69
    /**
70
     * @expectedException RuntimeException
71
     * @expectedExceptionMessage The tag "jms_serializer.event_listener" of service "my_listener" requires an attribute named "event".
72
     */
73 View Code Duplication
    public function testEventListenerMustHaveEventDefined()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
74
    {
75
        $container = $this->getContainer();
76
77
        $def = new Definition('Foo');
78
        $def->addTag('jms_serializer.event_listener', [
79
            'class' => 'Bar',
80
        ]);
81
82
        $container->setDefinition('my_listener', $def);
83
84
        $pass = new RegisterEventListenersAndSubscribersPass();
85
        $pass->process($container);
86
    }
87
88
    public function testEventListenerCanBePrivateWithServiceClosureArgument()
89
    {
90
        if (!class_exists(ServiceClosureArgument::class)) {
91
            $this->markTestSkipped("ServiceClosureArgument not available");
92
        }
93
94
        $container = $this->getContainer();
95
96
        $def = new Definition('Foo');
97
        $def->setPublic(false);
98
        $def->addTag('jms_serializer.event_listener', ['event' => 'serializer.pre_serialize']);
99
100
        $container->setDefinition('my_listener', $def);
101
102
        $pass = new RegisterEventListenersAndSubscribersPass();
103
        $pass->process($container);
104
105
        $dispatcher = $container->getDefinition('jms_serializer.event_dispatcher');
106
        $methodCalls = $dispatcher->getMethodCalls();
107
108
        $called = false;
109 View Code Duplication
        foreach ($methodCalls as $call) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
110
            if ($call[0] === 'setListeners') {
111
                $called = true;
112
113
                $this->assertArrayHasKey('serializer.pre_serialize', $call[1][0]);
114
                $this->assertInstanceOf(ServiceClosureArgument::class, $call[1][0]['serializer.pre_serialize'][0][0][0]);
115
                $this->assertEquals('onserializerpreserialize', $call[1][0]['serializer.pre_serialize'][0][0][1]);
116
            }
117
        }
118
119
        if (!$called) {
120
            $this->fail("The method setListeners was not invoked on the jms_serializer.event_dispatcher");
121
        }
122
    }
123
124
    /**
125
     * @expectedException \RuntimeException
126
     * @expectedExceptionMessage The tag "jms_serializer.event_listener" of service "my_listener" requires the service to be public or to have symfony/event-dispatcher ^3.3|^4.0.
127
     */
128
    public function testEventListenerCantBePrivateWithoutServiceClosureArgument()
129
    {
130
        if (class_exists(ServiceClosureArgument::class)) {
131
            $this->markTestSkipped("ServiceClosureArgument is available");
132
        }
133
        $container = $this->getContainer();
134
135
        $def = new Definition('Foo');
136
        $def->setPublic(false);
137
        $def->addTag('jms_serializer.event_listener', ['event' => 'serializer.pre_serialize']);
138
139
        $container->setDefinition('my_listener', $def);
140
141
        $pass = new RegisterEventListenersAndSubscribersPass();
142
        $pass->process($container);
143
    }
144
145 View Code Duplication
    public function testEventListener()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
146
    {
147
        $container = $this->getContainer();
148
149
        $def = new Definition('Foo');
150
        $def->addTag('jms_serializer.event_listener', [
151
            'event' => 'serializer.pre_serialize',
152
            'class' => 'Bar',
153
        ]);
154
155
        $container->setDefinition('my_listener', $def);
156
157
        $pass = new RegisterEventListenersAndSubscribersPass();
158
        $pass->process($container);
159
160
        $dispatcher = $container->getDefinition('jms_serializer.event_dispatcher');
161
        $methodCalls = $dispatcher->getMethodCalls();
162
163
        $called = false;
164
        foreach ($methodCalls as $call) {
165
            if ($call[0] === 'setListeners') {
166
                $called = true;
167
                $this->assertEquals([
168
                    'serializer.pre_serialize' => [
169
                        [
170
                            ['my_listener', 'onserializerpreserialize'],
171
                            'bar',
172
                            null
173
                        ]
174
                    ]], $call[1][0]);
175
            }
176
        }
177
178
        if (!$called) {
179
            $this->fail("The method setListeners was not invoked on the jms_serializer.event_dispatcher");
180
        }
181
    }
182
183 View Code Duplication
    public function testEventListenerWithParams()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
184
    {
185
        $container = $this->getContainer();
186
187
        $container->setParameter('bar', 'Bar');
188
189
        $def = new Definition('Foo');
190
        $def->addTag('jms_serializer.event_listener', [
191
            'event' => 'serializer.pre_serialize',
192
            'class' => '%bar%',
193
        ]);
194
195
        $container->setDefinition('my_listener', $def);
196
197
        $pass = new RegisterEventListenersAndSubscribersPass();
198
        $pass->process($container);
199
200
        $dispatcher = $container->getDefinition('jms_serializer.event_dispatcher');
201
        $methodCalls = $dispatcher->getMethodCalls();
202
203
        $called = false;
204
        foreach ($methodCalls as $call) {
205
            if ($call[0] === 'setListeners') {
206
                $called = true;
207
                $this->assertEquals([
208
                    'serializer.pre_serialize' => [
209
                        [
210
                            ['my_listener', 'onserializerpreserialize'],
211
                            'bar',
212
                            null
213
                        ]
214
                    ]], $call[1][0]);
215
            }
216
        }
217
218
        if (!$called) {
219
            $this->fail("The method setListeners was not invoked on the jms_serializer.event_dispatcher");
220
        }
221
    }
222
223
    public function testEventSubscriber()
224
    {
225
        $container = $this->getContainer();
226
227
        $def = new Definition('JMS\SerializerBundle\Tests\DependencyInjection\Fixture\EventSubscriber');
228
        $def->addTag('jms_serializer.event_subscriber');
229
230
        $container->setDefinition('my_listener', $def);
231
232
        $pass = new RegisterEventListenersAndSubscribersPass();
233
        $pass->process($container);
234
235
        $dispatcher = $container->getDefinition('jms_serializer.event_dispatcher');
236
        $methodCalls = $dispatcher->getMethodCalls();
237
238
        $called = false;
239
        foreach ($methodCalls as $call) {
240
            if ($call[0] === 'setListeners') {
241
                $called = true;
242
                $this->assertEquals([
243
                    'serializer.pre_serialize' => [
244
                        [
245
                            ['my_listener', 'onserializerpreserialize'],
246
                            'bar',
247
                            'json'
248
                        ]
249
                    ]], $call[1][0]);
250
            }
251
        }
252
253
        if (!$called) {
254
            $this->fail('The method setListeners was not invoked on the jms_serializer.event_dispatcher');
255
        }
256
    }
257
258
    /**
259
     * @expectedException RuntimeException
260
     * @expectedExceptionMessage The service "my_subscriber" (class: JMS\SerializerBundle\Tests\DependencyInjection\Fixture\VersionedObject) does not implement the EventSubscriberInterface.
261
     */
262 View Code Duplication
    public function testEventSubscriberInterface()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
263
    {
264
        $container = $this->getContainer();
265
266
        $def = new Definition('JMS\SerializerBundle\Tests\DependencyInjection\Fixture\VersionedObject');
267
        $def->addTag('jms_serializer.event_subscriber');
268
269
        $container->setDefinition('my_subscriber', $def);
270
271
        $pass = new RegisterEventListenersAndSubscribersPass();
272
        $pass->process($container);
273
    }
274
275
    public function testEventSubscriberCanBePrivateWithServiceClosureArgument()
276
    {
277
        if (!class_exists(ServiceClosureArgument::class)) {
278
            $this->markTestSkipped("ServiceClosureArgument not available");
279
        }
280
281
        $container = $this->getContainer();
282
283
        $def = new Definition(SimpleHandler::class);
284
        $def->setPublic(false);
285
        $def->addTag('jms_serializer.event_subscriber');
286
287
        $container->setDefinition('my_subscriber', $def);
288
289
        $pass = new RegisterEventListenersAndSubscribersPass();
290
        $pass->process($container);
291
292
        $dispatcher = $container->getDefinition('jms_serializer.event_dispatcher');
293
        $methodCalls = $dispatcher->getMethodCalls();
294
295
        $called = false;
296 View Code Duplication
        foreach ($methodCalls as $call) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
297
            if ($call[0] === 'setListeners') {
298
                $called = true;
299
                $this->assertArrayHasKey('the-event-name', $call[1][0]);
300
                $this->assertInstanceOf(ServiceClosureArgument::class, $call[1][0]['the-event-name'][0][0][0]);
301
                $this->assertEquals('onEventName', $call[1][0]['the-event-name'][0][0][1]);
302
            }
303
        }
304
305
        if (!$called) {
306
            $this->fail("The method setListeners was not invoked on the jms_serializer.event_dispatcher");
307
        }
308
    }
309
310
    /**
311
     * @expectedException \RuntimeException
312
     * @expectedExceptionMessage The tag "jms_serializer.event_subscriber" of service "my_listener" requires the service to be public or to have symfony/event-dispatcher ^3.3|^4.0.
313
     */
314
    public function testEventSubscriberCantBePrivateWithoutServiceClosureArgument()
315
    {
316
        if (class_exists(ServiceClosureArgument::class)) {
317
            $this->markTestSkipped("ServiceClosureArgument is available");
318
        }
319
320
        $container = $this->getContainer();
321
322
        $def = new Definition(SimpleHandler::class);
323
        $def->setPublic(false);
324
        $def->addTag('jms_serializer.event_subscriber');
325
326
        $container->setDefinition('my_subscriber', $def);
327
328
        $pass = new RegisterEventListenersAndSubscribersPass();
329
        $pass->process($container);
330
331
        $dispatcher = $container->getDefinition('jms_serializer.event_dispatcher');
332
        $methodCalls = $dispatcher->getMethodCalls();
333
334
        $called = false;
335
        foreach ($methodCalls as $call) {
336
            if ($call[0] === 'setListeners') {
337
                $called = true;
338
                $this->assertEquals([
339
                    'the-event-name' => [
340
                        [
341
                            ['my_subscriber', 'onEventName'],
342
                            'some-class',
343
                            'json'
344
                        ]
345
                    ]], $call[1][0]);
346
            }
347
        }
348
349
        if (!$called) {
350
            $this->fail('The method setListeners was not invoked on the jms_serializer.event_dispatcher');
351
        }
352
    }
353
}
354
355