Completed
Push — master ( 474803...83f0bf )
by Asmir
11s
created

CustomHandlerPassTest::testHandlerCanBePrivate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 22
rs 9.2
cc 1
eloc 15
nc 1
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\Serializer\Exception\RuntimeException;
22
use JMS\SerializerBundle\DependencyInjection\Compiler\CustomHandlersPass;
23
use JMS\SerializerBundle\DependencyInjection\JMSSerializerExtension;
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\Reference;
31
32
class CustomHandlerPassTest 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
        // remove other subscribers
57
        foreach (array_keys($container->findTaggedServiceIds('jms_serializer.handler')) as $id) {
58
            $container->removeDefinition($id);
59
        }
60
        foreach (array_keys($container->findTaggedServiceIds('jms_serializer.subscribing_handler')) as $id) {
61
            $container->removeDefinition($id);
62
        }
63
64
        return $container;
65
    }
66
67
    public function testHandler()
68
    {
69
        $container = $this->getContainer();
70
71
        $def = new Definition('Foo');
72
        $def->addTag('jms_serializer.handler', [
73
            'type' => 'DateTime',
74
            'format' => 'json',
75
        ]);
76
        $container->setDefinition('my_service', $def);
77
78
        $pass = new CustomHandlersPass();
79
        $pass->process($container);
80
81
        $args = $container->getDefinition('jms_serializer.handler_registry')->getArguments();
82
83
        $this->assertSame([
84
            2 => ['DateTime' => ['json' => ['my_service', 'deserializeDateTimeFromjson']]],
85
            1 => ['DateTime' => ['json' => ['my_service', 'serializeDateTimeTojson']]]
86
        ], $args[1]);
87
    }
88
89
    public function testHandlerCanBePrivate()
90
    {
91
        $container = $this->getContainer();
92
93
        $def = new Definition('Foo');
94
        $def->setPublic(false);
95
        $def->addTag('jms_serializer.handler', [
96
            'type' => 'DateTime',
97
            'format' => 'json',
98
        ]);
99
        $container->setDefinition('my_service', $def);
100
101
        $pass = new CustomHandlersPass();
102
        $pass->process($container);
103
104
        $args = $container->getDefinition('jms_serializer.handler_registry')->getArguments();
105
106
        $this->assertEquals([
107
            2 => ['DateTime' => ['json' => [new Reference('my_service'), 'deserializeDateTimeFromjson']]],
108
            1 => ['DateTime' => ['json' => [new Reference('my_service'), 'serializeDateTimeTojson']]]
109
        ], $args[1]);
110
    }
111
112
    public function testHandlerDirection()
113
    {
114
        $container = $this->getContainer();
115
116
        $def = new Definition('Foo');
117
        $def->addTag('jms_serializer.handler', [
118
            'type' => 'DateTime',
119
            'format' => 'json',
120
            'direction' => 'SERIALIZATION',
121
        ]);
122
        $container->setDefinition('my_service', $def);
123
124
        $pass = new CustomHandlersPass();
125
        $pass->process($container);
126
127
        $args = $container->getDefinition('jms_serializer.handler_registry')->getArguments();
128
129
        $this->assertSame([
130
            1 => ['DateTime' => ['json' => ['my_service', 'serializeDateTimeTojson']]]
131
        ], $args[1]);
132
    }
133
134
    /**
135
     * @expectedException RuntimeException
136
     * @expectedExceptionMessage The direction "bar" of tag "jms_serializer.handler" of service "my_service" does not exist
137
     */
138
    public function testHandlerIncorrectDirection()
139
    {
140
        $container = $this->getContainer();
141
142
        $def = new Definition('Foo');
143
        $def->addTag('jms_serializer.handler', [
144
            'type' => 'DateTime',
145
            'format' => 'json',
146
            'direction' => 'bar',
147
        ]);
148
        $container->setDefinition('my_service', $def);
149
150
        $pass = new CustomHandlersPass();
151
        $pass->process($container);
152
    }
153
154
    /**
155
     * @expectedException RuntimeException
156
     * @expectedExceptionMessage Each tag named "jms_serializer.handler" of service "my_service" must have at least two attributes: "type" and "format"
157
     */
158 View Code Duplication
    public function testHandlerMustHaveTypeAndFormat()
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...
159
    {
160
        $container = $this->getContainer();
161
162
        $def = new Definition('Foo');
163
        $def->addTag('jms_serializer.handler');
164
        $container->setDefinition('my_service', $def);
165
166
        $pass = new CustomHandlersPass();
167
        $pass->process($container);
168
    }
169
170 View Code Duplication
    public function testSubscribingHandler()
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...
171
    {
172
        $container = $this->getContainer();
173
174
        $def = new Definition('JMS\SerializerBundle\Tests\DependencyInjection\Fixture\SubscribingHandler');
175
        $def->addTag('jms_serializer.subscribing_handler');
176
        $container->setDefinition('my_service', $def);
177
178
        $pass = new CustomHandlersPass();
179
        $pass->process($container);
180
181
        $args = $container->getDefinition('jms_serializer.handler_registry')->getArguments();
182
183
        $this->assertSame([
184
            1 => ['DateTime' => ['json' => ['my_service', 'onDateTime']]]
185
        ], $args[1]);
186
    }
187
188 View Code Duplication
    public function testSubscribingHandlerCanBePrivate()
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...
189
    {
190
        $container = $this->getContainer();
191
192
        $def = new Definition('JMS\SerializerBundle\Tests\DependencyInjection\Fixture\SubscribingHandler');
193
        $def->addTag('jms_serializer.subscribing_handler');
194
        $def->setPublic(false);
195
        $container->setDefinition('my_service', $def);
196
197
        $pass = new CustomHandlersPass();
198
        $pass->process($container);
199
200
        $args = $container->getDefinition('jms_serializer.handler_registry')->getArguments();
201
202
        $this->assertEquals([
203
            1 => ['DateTime' => ['json' => [new Reference('my_service'), 'onDateTime']]]
204
        ], $args[1]);
205
    }
206
207
    /**
208
     * @expectedException RuntimeException
209
     * @expectedExceptionMessage The service "my_service" must implement the SubscribingHandlerInterface
210
     */
211 View Code Duplication
    public function testSubscribingHandlerInterface()
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...
212
    {
213
        $container = $this->getContainer();
214
215
        $def = new Definition('JMS\SerializerBundle\Tests\DependencyInjection\Fixture\SimpleObject');
216
        $def->addTag('jms_serializer.subscribing_handler');
217
        $container->setDefinition('my_service', $def);
218
219
        $pass = new CustomHandlersPass();
220
        $pass->process($container);
221
    }
222
}
223