Completed
Push — master ( 067ff2...474803 )
by Asmir
12s
created

testEventListenerCanBePrivate()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 35
Code Lines 22

Duplication

Lines 35
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 35
loc 35
rs 8.5806
cc 4
eloc 22
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
31
class EventSubscribersAndListenersPassTest extends TestCase
32
{
33
    /**
34
     * @param array $configs
35
     * @return ContainerBuilder
36
     */
37 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...
38
    {
39
        $loader = new JMSSerializerExtension();
40
        $container = new ContainerBuilder();
41
42
        $container->getCompilerPassConfig()->setOptimizationPasses(array(
43
            new ResolveParameterPlaceHoldersPass(),
44
            new ResolveDefinitionTemplatesPass(),
45
        ));
46
        $container->getCompilerPassConfig()->setRemovingPasses(array(new RemoveUnusedDefinitionsPass()));
47
48
        $container->setParameter('kernel.debug', true);
49
        $container->setParameter('kernel.cache_dir', sys_get_temp_dir() . '/serializer');
50
        $container->setParameter('kernel.bundles', array());
51
        $container->setParameter('kernel.bundles', array());
52
53
        $loader->load(['jms_serializer' => $configs], $container);
54
55
56
        // remove other listeners
57
        foreach (array_keys($container->findTaggedServiceIds('jms_serializer.event_listener')) as $id) {
58
            $container->removeDefinition($id);
59
        }
60
        // remove other subscribers
61
        foreach (array_keys($container->findTaggedServiceIds('jms_serializer.event_subscriber')) as $id) {
62
            $container->removeDefinition($id);
63
        }
64
65
        return $container;
66
    }
67
68
    /**
69
     * @expectedException RuntimeException
70
     * @expectedExceptionMessage The tag "jms_serializer.event_listener" of service "my_listener" requires an attribute named "event".
71
     */
72 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...
73
    {
74
        $container = $this->getContainer();
75
76
        $def = new Definition('Foo');
77
        $def->addTag('jms_serializer.event_listener', [
78
            'class' => 'Bar',
79
        ]);
80
81
        $container->setDefinition('my_listener', $def);
82
83
        $pass = new RegisterEventListenersAndSubscribersPass();
84
        $pass->process($container);
85
    }
86
87 View Code Duplication
    public function testEventListenerCanBePrivate()
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...
88
    {
89
        $container = $this->getContainer();
90
91
        $def = new Definition('Foo');
92
        $def->setPublic(false);
93
        $def->addTag('jms_serializer.event_listener', ['event' => 'serializer.pre_serialize']);
94
95
        $container->setDefinition('my_listener', $def);
96
97
        $pass = new RegisterEventListenersAndSubscribersPass();
98
        $pass->process($container);
99
100
        $dispatcher = $container->getDefinition('jms_serializer.event_dispatcher');
101
        $methodCalls = $dispatcher->getMethodCalls();
102
103
        $called = false;
104
        foreach ($methodCalls as $call) {
105
            if ($call[0] === 'setListeners') {
106
                $called = true;
107
                $this->assertEquals([
108
                    'serializer.pre_serialize' => [
109
                        [
110
                            ['my_listener', 'onserializerpreserialize'],
111
                            null,
112
                            null
113
                        ]
114
                    ]], $call[1][0]);
115
            }
116
        }
117
118
        if (!$called) {
119
            $this->fail("The method setListeners was not invoked on the jms_serializer.event_dispatcher");
120
        }
121
    }
122
123 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...
124
    {
125
        $container = $this->getContainer();
126
127
        $def = new Definition('Foo');
128
        $def->addTag('jms_serializer.event_listener', [
129
            'event' => 'serializer.pre_serialize',
130
            'class' => 'Bar',
131
        ]);
132
133
        $container->setDefinition('my_listener', $def);
134
135
        $pass = new RegisterEventListenersAndSubscribersPass();
136
        $pass->process($container);
137
138
        $dispatcher = $container->getDefinition('jms_serializer.event_dispatcher');
139
        $methodCalls = $dispatcher->getMethodCalls();
140
141
        $called = false;
142
        foreach ($methodCalls as $call) {
143
            if ($call[0] === 'setListeners') {
144
                $called = true;
145
                $this->assertEquals([
146
                    'serializer.pre_serialize' => [
147
                        [
148
                            ['my_listener', 'onserializerpreserialize'],
149
                            'bar',
150
                            null
151
                        ]
152
                    ]], $call[1][0]);
153
            }
154
        }
155
156
        if (!$called) {
157
            $this->fail("The method setListeners was not invoked on the jms_serializer.event_dispatcher");
158
        }
159
    }
160
161 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...
162
    {
163
        $container = $this->getContainer();
164
165
        $container->setParameter('bar', 'Bar');
166
167
        $def = new Definition('Foo');
168
        $def->addTag('jms_serializer.event_listener', [
169
            'event' => 'serializer.pre_serialize',
170
            'class' => '%bar%',
171
        ]);
172
173
        $container->setDefinition('my_listener', $def);
174
175
        $pass = new RegisterEventListenersAndSubscribersPass();
176
        $pass->process($container);
177
178
        $dispatcher = $container->getDefinition('jms_serializer.event_dispatcher');
179
        $methodCalls = $dispatcher->getMethodCalls();
180
181
        $called = false;
182
        foreach ($methodCalls as $call) {
183
            if ($call[0] === 'setListeners') {
184
                $called = true;
185
                $this->assertEquals([
186
                    'serializer.pre_serialize' => [
187
                        [
188
                            ['my_listener', 'onserializerpreserialize'],
189
                            'bar',
190
                            null
191
                        ]
192
                    ]], $call[1][0]);
193
            }
194
        }
195
196
        if (!$called) {
197
            $this->fail("The method setListeners was not invoked on the jms_serializer.event_dispatcher");
198
        }
199
    }
200
201
    public function testEventSubscriber()
202
    {
203
        $container = $this->getContainer();
204
205
        $def = new Definition('JMS\SerializerBundle\Tests\DependencyInjection\Fixture\EventSubscriber');
206
        $def->addTag('jms_serializer.event_subscriber');
207
208
        $container->setDefinition('my_listener', $def);
209
210
        $pass = new RegisterEventListenersAndSubscribersPass();
211
        $pass->process($container);
212
213
        $dispatcher = $container->getDefinition('jms_serializer.event_dispatcher');
214
        $methodCalls = $dispatcher->getMethodCalls();
215
216
        $called = false;
217
        foreach ($methodCalls as $call) {
218
            if ($call[0] === 'setListeners') {
219
                $called = true;
220
                $this->assertEquals([
221
                    'serializer.pre_serialize' => [
222
                        [
223
                            ['my_listener', 'onserializerpreserialize'],
224
                            'bar',
225
                            'json'
226
                        ]
227
                    ]], $call[1][0]);
228
            }
229
        }
230
231
        if (!$called) {
232
            $this->fail('The method setListeners was not invoked on the jms_serializer.event_dispatcher');
233
        }
234
    }
235
236
    /**
237
     * @expectedException RuntimeException
238
     * @expectedExceptionMessage The service "my_listener" (class: JMS\SerializerBundle\Tests\DependencyInjection\Fixture\VersionedObject) does not implement the EventSubscriberInterface.
239
     */
240 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...
241
    {
242
        $container = $this->getContainer();
243
244
        $def = new Definition('JMS\SerializerBundle\Tests\DependencyInjection\Fixture\VersionedObject');
245
        $def->addTag('jms_serializer.event_subscriber');
246
247
        $container->setDefinition('my_listener', $def);
248
249
        $pass = new RegisterEventListenersAndSubscribersPass();
250
        $pass->process($container);
251
    }
252
253 View Code Duplication
    public function testEventSubscriberCanBePrivate()
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...
254
    {
255
        $container = $this->getContainer();
256
257
        $def = new Definition(SimpleHandler::class);
258
        $def->setPublic(false);
259
        $def->addTag('jms_serializer.event_subscriber');
260
261
        $container->setDefinition('my_subscriber', $def);
262
263
        $pass = new RegisterEventListenersAndSubscribersPass();
264
        $pass->process($container);
265
266
        $dispatcher = $container->getDefinition('jms_serializer.event_dispatcher');
267
        $methodCalls = $dispatcher->getMethodCalls();
268
269
        $called = false;
270
        foreach ($methodCalls as $call) {
271
            if ($call[0] === 'setListeners') {
272
                $called = true;
273
                $this->assertEquals([
274
                    'the-event-name' => [
275
                        [
276
                            ['my_subscriber', 'onEventName'],
277
                            'some-class',
278
                            'json'
279
                        ]
280
                    ]], $call[1][0]);
281
            }
282
        }
283
284
        if (!$called) {
285
            $this->fail('The method setListeners was not invoked on the jms_serializer.event_dispatcher');
286
        }
287
    }
288
}
289
290