Completed
Push — master ( bca297...c408c0 )
by Asmir
05:23
created

testWrongObjectConstructorFallbackStrategyTriggersException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
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
                        [
52
                            'namespace_prefix' => 'JMSSerializerBundleNs1',
53
                            'path' => '@JMSSerializerBundle',
54
                        ],
55
                        [
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 testContextDefaults()
88
    {
89
        $processor = new Processor();
90
        $config = $processor->processConfiguration(new Configuration(true), []);
91
92
        $this->assertArrayHasKey('default_context', $config);
93 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...
94
            $this->assertArrayHasKey($item, $config['default_context']);
95
96
            $defaultContext = $config['default_context'][$item];
97
98
            $this->assertTrue(is_array($defaultContext['attributes']));
99
            $this->assertEmpty($defaultContext['attributes']);
100
101
            $this->assertTrue(is_array($defaultContext['groups']));
102
            $this->assertEmpty($defaultContext['groups']);
103
104
            $this->assertArrayNotHasKey('version', $defaultContext);
105
            $this->assertArrayNotHasKey('serialize_null', $defaultContext);
106
        }
107
    }
108
109
    public function testContextValues()
110
    {
111
        $configArray = array(
112
            'serialization' => array(
113
                'version' => 3,
114
                'serialize_null' => true,
115
                'attributes' => ['foo' => 'bar'],
116
                'groups' => ['Baz'],
117
                'enable_max_depth_checks' => false,
118
            ),
119
            'deserialization' => array(
120
                'version' => "5.5",
121
                'serialize_null' => false,
122
                'attributes' => ['foo' => 'bar'],
123
                'groups' => ['Baz'],
124
                'enable_max_depth_checks' => true,
125
            )
126
        );
127
128
        $processor = new Processor();
129
        $config = $processor->processConfiguration(new Configuration(true), [
130
            'jms_serializer' => [
131
                'default_context' => $configArray
132
            ]
133
        ]);
134
135
        $this->assertArrayHasKey('default_context', $config);
136
        foreach (['serialization', 'deserialization'] as $configKey) {
137
            $this->assertArrayHasKey($configKey, $config['default_context']);
138
139
            $values = $config['default_context'][$configKey];
140
            $confArray = $configArray[$configKey];
141
142
            $this->assertSame($values['version'], $confArray['version']);
143
            $this->assertSame($values['serialize_null'], $confArray['serialize_null']);
144
            $this->assertSame($values['attributes'], $confArray['attributes']);
145
            $this->assertSame($values['groups'], $confArray['groups']);
146
            $this->assertSame($values['enable_max_depth_checks'], $confArray['enable_max_depth_checks']);
147
        }
148
    }
149
150
    public function testConfigNormalization()
151
    {
152
        $configArray = [
153
            'default_context' => [
154
                'serialization' => 'the.serialization.factory.context',
155
                'deserialization' => 'the.deserialization.factory.context',
156
            ],
157
            'property_naming' => 'property.mapping.service',
158
            'expression_evaluator' => 'expression_evaluator.service',
159
        ];
160
161
        $processor = new Processor();
162
        $config = $processor->processConfiguration(new Configuration(true), [
163
            'jms_serializer' => $configArray
164
        ]);
165
166
        $this->assertArrayHasKey('default_context', $config);
167
        $this->assertArrayHasKey('serialization', $config['default_context']);
168
        $this->assertArrayHasKey('deserialization', $config['default_context']);
169
        $this->assertArrayHasKey('id', $config['default_context']['serialization']);
170
        $this->assertArrayHasKey('id', $config['default_context']['deserialization']);
171
172
        $this->assertSame($configArray['default_context']['serialization'], $config['default_context']['serialization']['id']);
173
        $this->assertSame($configArray['default_context']['deserialization'], $config['default_context']['deserialization']['id']);
174
175
        $this->assertArrayHasKey('property_naming', $config);
176
        $this->assertArrayHasKey('expression_evaluator', $config);
177
        $this->assertArrayHasKey('id', $config['property_naming']);
178
        $this->assertArrayHasKey('id', $config['expression_evaluator']);
179
        $this->assertSame($configArray['property_naming'], $config['property_naming']['id']);
180
        $this->assertSame($configArray['expression_evaluator'], $config['expression_evaluator']['id']);
181
    }
182
183
    public function testContextNullValues()
184
    {
185
        $configArray = array(
186
            'serialization' => array(
187
                'version' => null,
188
                'serialize_null' => null,
189
                'attributes' => null,
190
                'groups' => null,
191
            ),
192
            'deserialization' => array(
193
                'version' => null,
194
                'serialize_null' => null,
195
                'attributes' => null,
196
                'groups' => null,
197
            )
198
        );
199
200
        $processor = new Processor();
201
        $config = $processor->processConfiguration(new Configuration(true), [
202
            'jms_serializer' => [
203
                'default_context' => $configArray
204
            ]
205
        ]);
206
207
        $this->assertArrayHasKey('default_context', $config);
208 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...
209
            $this->assertArrayHasKey($configKey, $config['default_context']);
210
211
            $defaultContext = $config['default_context'][$configKey];
212
213
            $this->assertTrue(is_array($defaultContext['attributes']));
214
            $this->assertEmpty($defaultContext['attributes']);
215
216
            $this->assertTrue(is_array($defaultContext['groups']));
217
            $this->assertEmpty($defaultContext['groups']);
218
219
            $this->assertArrayNotHasKey('version', $defaultContext);
220
            $this->assertArrayNotHasKey('serialize_null', $defaultContext);
221
        }
222
    }
223
}
224