Completed
Push — master ( a61272...cb5e47 )
by Asmir
03:28
created

Tests/DependencyInjection/ConfigurationTest.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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