testEventListener()   B
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 37
Code Lines 23

Duplication

Lines 37
Ratio 100 %

Importance

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