Completed
Push — master ( 85ee03...d3810b )
by Asmir
04:47
created

ConfigurationTest::testDefaultDateFormat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
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\SerializerBundle\DependencyInjection\Configuration;
22
use JMS\SerializerBundle\JMSSerializerBundle;
23
use Symfony\Component\Config\Definition\Processor;
24
use Symfony\Component\DependencyInjection\ContainerBuilder;
25
26
class ConfigurationTest extends \PHPUnit_Framework_TestCase
27
{
28
    private function getContainer(array $configs = array())
29
    {
30
        $container = new ContainerBuilder();
31
32
        $container->setParameter('kernel.debug', true);
33
        $container->setParameter('kernel.cache_dir', sys_get_temp_dir() . '/serializer');
34
        $container->setParameter('kernel.bundles', array('JMSSerializerBundle' => 'JMS\SerializerBundle\JMSSerializerBundle'));
35
36
        $bundle = new JMSSerializerBundle();
37
38
        $extension = $bundle->getContainerExtension();
39
        $extension->load($configs, $container);
40
41
        return $container;
42
    }
43
44
    public function testConfig()
45
    {
46
        $ref = new JMSSerializerBundle();
47
        $container = $this->getContainer([
48
            [
49
                'metadata' => [
50
                    'directories' => [
51
                        'foo' => [
52
                            'namespace_prefix' => 'JMSSerializerBundleNs1',
53
                            'path' => '@JMSSerializerBundle',
54
                        ],
55
                        'bar' => [
56
                            'namespace_prefix' => 'JMSSerializerBundleNs2',
57
                            'path' => '@JMSSerializerBundle/Resources/config',
58
                        ],
59
                    ]
60
                ]
61
            ],
62
        ]);
63
64
        $directories = $container->getDefinition('jms_serializer.metadata.file_locator')->getArgument(0);
65
66
        $this->assertEquals($ref->getPath(), $directories['JMSSerializerBundleNs1']);
67
        $this->assertEquals($ref->getPath() . '/Resources/config', $directories['JMSSerializerBundleNs2']);
68
    }
69
70
    /**
71
     * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
72
     */
73
    public function testWrongObjectConstructorFallbackStrategyTriggersException()
74
    {
75
        $processor = new Processor();
76
        $processor->processConfiguration(new Configuration(true), [
77
            'jms_serializer' => [
78
                'object_constructors' => [
79
                    'doctrine' => [
80
                        'fallback_strategy' => "foo",
81
                    ],
82
                ]
83
            ]
84
        ]);
85
    }
86
87
    public function testConfigComposed()
88
    {
89
        $ref = new JMSSerializerBundle();
90
        $container = $this->getContainer([
91
            [
92
                'metadata' => [
93
                    'directories' => [
94
                        'foo' => [
95
                            'namespace_prefix' => 'JMSSerializerBundleNs1',
96
                            'path' => '@JMSSerializerBundle',
97
                        ],
98
                    ]
99
                ]
100
            ],
101
            [
102
                'metadata' => [
103
                    'directories' => [
104
                        [
105
                            'name' => 'foo',
106
                            'namespace_prefix' => 'JMSSerializerBundleNs2',
107
                            'path' => '@JMSSerializerBundle/Resources/config',
108
                        ],
109
                    ]
110
                ]
111
            ],
112
        ]);
113
114
        $directories = $container->getDefinition('jms_serializer.metadata.file_locator')->getArgument(0);
115
116
        $this->assertArrayNotHasKey('JMSSerializerBundleNs1', $directories);
117
        $this->assertEquals($ref->getPath().'/Resources/config', $directories['JMSSerializerBundleNs2']);
118
    }
119
120
    public function testContextDefaults()
121
    {
122
        $processor = new Processor();
123
        $config = $processor->processConfiguration(new Configuration(true), []);
124
125
        $this->assertArrayHasKey('default_context', $config);
126 View Code Duplication
        foreach (['serialization', 'deserialization'] as $item) {
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...
127
            $this->assertArrayHasKey($item, $config['default_context']);
128
129
            $defaultContext = $config['default_context'][$item];
130
131
            $this->assertTrue(is_array($defaultContext['attributes']));
132
            $this->assertEmpty($defaultContext['attributes']);
133
134
            $this->assertTrue(is_array($defaultContext['groups']));
135
            $this->assertEmpty($defaultContext['groups']);
136
137
            $this->assertArrayNotHasKey('version', $defaultContext);
138
            $this->assertArrayNotHasKey('serialize_null', $defaultContext);
139
        }
140
    }
141
142
    public function testContextValues()
143
    {
144
        $configArray = array(
145
            'serialization' => array(
146
                'version' => 3,
147
                'serialize_null' => true,
148
                'attributes' => ['foo' => 'bar'],
149
                'groups' => ['Baz'],
150
                'enable_max_depth_checks' => false,
151
            ),
152
            'deserialization' => array(
153
                'version' => "5.5",
154
                'serialize_null' => false,
155
                'attributes' => ['foo' => 'bar'],
156
                'groups' => ['Baz'],
157
                'enable_max_depth_checks' => true,
158
            )
159
        );
160
161
        $processor = new Processor();
162
        $config = $processor->processConfiguration(new Configuration(true), [
163
            'jms_serializer' => [
164
                'default_context' => $configArray
165
            ]
166
        ]);
167
168
        $this->assertArrayHasKey('default_context', $config);
169
        foreach (['serialization', 'deserialization'] as $configKey) {
170
            $this->assertArrayHasKey($configKey, $config['default_context']);
171
172
            $values = $config['default_context'][$configKey];
173
            $confArray = $configArray[$configKey];
174
175
            $this->assertSame($values['version'], $confArray['version']);
176
            $this->assertSame($values['serialize_null'], $confArray['serialize_null']);
177
            $this->assertSame($values['attributes'], $confArray['attributes']);
178
            $this->assertSame($values['groups'], $confArray['groups']);
179
            $this->assertSame($values['enable_max_depth_checks'], $confArray['enable_max_depth_checks']);
180
        }
181
    }
182
183
    public function testConfigNormalization()
184
    {
185
        $configArray = [
186
            'default_context' => [
187
                'serialization' => 'the.serialization.factory.context',
188
                'deserialization' => 'the.deserialization.factory.context',
189
            ],
190
            'property_naming' => 'property.mapping.service',
191
            'expression_evaluator' => 'expression_evaluator.service',
192
        ];
193
194
        $processor = new Processor();
195
        $config = $processor->processConfiguration(new Configuration(true), [
196
            'jms_serializer' => $configArray
197
        ]);
198
199
        $this->assertArrayHasKey('default_context', $config);
200
        $this->assertArrayHasKey('serialization', $config['default_context']);
201
        $this->assertArrayHasKey('deserialization', $config['default_context']);
202
        $this->assertArrayHasKey('id', $config['default_context']['serialization']);
203
        $this->assertArrayHasKey('id', $config['default_context']['deserialization']);
204
205
        $this->assertSame($configArray['default_context']['serialization'], $config['default_context']['serialization']['id']);
206
        $this->assertSame($configArray['default_context']['deserialization'], $config['default_context']['deserialization']['id']);
207
208
        $this->assertArrayHasKey('property_naming', $config);
209
        $this->assertArrayHasKey('expression_evaluator', $config);
210
        $this->assertArrayHasKey('id', $config['property_naming']);
211
        $this->assertArrayHasKey('id', $config['expression_evaluator']);
212
        $this->assertSame($configArray['property_naming'], $config['property_naming']['id']);
213
        $this->assertSame($configArray['expression_evaluator'], $config['expression_evaluator']['id']);
214
    }
215
216
    public function testContextNullValues()
217
    {
218
        $configArray = array(
219
            'serialization' => array(
220
                'version' => null,
221
                'serialize_null' => null,
222
                'attributes' => null,
223
                'groups' => null,
224
            ),
225
            'deserialization' => array(
226
                'version' => null,
227
                'serialize_null' => null,
228
                'attributes' => null,
229
                'groups' => null,
230
            )
231
        );
232
233
        $processor = new Processor();
234
        $config = $processor->processConfiguration(new Configuration(true), [
235
            'jms_serializer' => [
236
                'default_context' => $configArray
237
            ]
238
        ]);
239
240
        $this->assertArrayHasKey('default_context', $config);
241 View Code Duplication
        foreach (['serialization', 'deserialization'] as $configKey) {
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...
242
            $this->assertArrayHasKey($configKey, $config['default_context']);
243
244
            $defaultContext = $config['default_context'][$configKey];
245
246
            $this->assertTrue(is_array($defaultContext['attributes']));
247
            $this->assertEmpty($defaultContext['attributes']);
248
249
            $this->assertTrue(is_array($defaultContext['groups']));
250
            $this->assertEmpty($defaultContext['groups']);
251
252
            $this->assertArrayNotHasKey('version', $defaultContext);
253
            $this->assertArrayNotHasKey('serialize_null', $defaultContext);
254
        }
255
    }
256
257
    public function testDefaultDateFormat()
258
    {
259
        $processor = new Processor();
260
        $config = $processor->processConfiguration(new Configuration(true), []);
261
262
        $this->assertEquals(\DateTime::ATOM, $config['handlers']['datetime']['default_format']);
263
    }
264
}
265