| 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 |  |  |     public function testContextDefaults() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $processor = new Processor(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $config = $processor->processConfiguration(new Configuration(true), []); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $this->assertArrayHasKey('default_context', $config); | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 76 |  | View Code Duplication |         foreach (['serialization', 'deserialization'] as $item) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             $this->assertArrayHasKey($item, $config['default_context']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             $defaultContext = $config['default_context'][$item]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             $this->assertTrue(is_array($defaultContext['attributes'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             $this->assertEmpty($defaultContext['attributes']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             $this->assertTrue(is_array($defaultContext['groups'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             $this->assertEmpty($defaultContext['groups']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             $this->assertArrayNotHasKey('version', $defaultContext); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             $this->assertArrayNotHasKey('serialize_null', $defaultContext); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     public function testContextValues() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $configArray = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             'serialization' => array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 'version' => 3, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                 'serialize_null' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 'attributes' => ['foo' => 'bar'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                 'groups' => ['Baz'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             'deserialization' => array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                 'version' => "5.5", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 'serialize_null' => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                 'attributes' => ['foo' => 'bar'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                 'groups' => ['Baz'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $processor = new Processor(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $config = $processor->processConfiguration(new Configuration(true), [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             'jms_serializer' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                 'default_context' => $configArray | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         $this->assertArrayHasKey('default_context', $config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         foreach (['serialization', 'deserialization'] as $configKey) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             $this->assertArrayHasKey($configKey, $config['default_context']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             $values = $config['default_context'][$configKey]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             $confArray = $configArray[$configKey]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             $this->assertSame($values['version'], $confArray['version']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             $this->assertSame($values['serialize_null'], $confArray['serialize_null']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             $this->assertSame($values['attributes'], $confArray['attributes']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             $this->assertSame($values['groups'], $confArray['groups']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     public function testConfigNormalization() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         $configArray = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             'default_context' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |                 'serialization' => 'the.serialization.factory.context', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |                 'deserialization' => 'the.deserialization.factory.context', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |             'property_naming' => 'property.mapping.service', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             'expression_evaluator' => 'expression_evaluator.service', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         $processor = new Processor(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         $config = $processor->processConfiguration(new Configuration(true), [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |             'jms_serializer' => $configArray | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         $this->assertArrayHasKey('default_context', $config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         $this->assertArrayHasKey('serialization', $config['default_context']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         $this->assertArrayHasKey('deserialization', $config['default_context']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         $this->assertArrayHasKey('id', $config['default_context']['serialization']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         $this->assertArrayHasKey('id', $config['default_context']['deserialization']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         $this->assertSame($configArray['default_context']['serialization'], $config['default_context']['serialization']['id']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         $this->assertSame($configArray['default_context']['deserialization'], $config['default_context']['deserialization']['id']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         $this->assertArrayHasKey('property_naming', $config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         $this->assertArrayHasKey('expression_evaluator', $config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         $this->assertArrayHasKey('id', $config['property_naming']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         $this->assertArrayHasKey('id', $config['expression_evaluator']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         $this->assertSame($configArray['property_naming'], $config['property_naming']['id']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         $this->assertSame($configArray['expression_evaluator'], $config['expression_evaluator']['id']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 163 |  |  |     public function testContextNullValues() | 
            
                                                        
            
                                    
            
            
                | 164 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 165 |  |  |         $configArray = array( | 
            
                                                        
            
                                    
            
            
                | 166 |  |  |             'serialization' => array( | 
            
                                                        
            
                                    
            
            
                | 167 |  |  |                 'version' => null, | 
            
                                                        
            
                                    
            
            
                | 168 |  |  |                 'serialize_null' => null, | 
            
                                                        
            
                                    
            
            
                | 169 |  |  |                 'attributes' => null, | 
            
                                                        
            
                                    
            
            
                | 170 |  |  |                 'groups' => null, | 
            
                                                        
            
                                    
            
            
                | 171 |  |  |             ), | 
            
                                                        
            
                                    
            
            
                | 172 |  |  |             'deserialization' => array( | 
            
                                                        
            
                                    
            
            
                | 173 |  |  |                 'version' => null, | 
            
                                                        
            
                                    
            
            
                | 174 |  |  |                 'serialize_null' => null, | 
            
                                                        
            
                                    
            
            
                | 175 |  |  |                 'attributes' => null, | 
            
                                                        
            
                                    
            
            
                | 176 |  |  |                 'groups' => null, | 
            
                                                        
            
                                    
            
            
                | 177 |  |  |             ) | 
            
                                                        
            
                                    
            
            
                | 178 |  |  |         ); | 
            
                                                        
            
                                    
            
            
                | 179 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 180 |  |  |         $processor = new Processor(); | 
            
                                                        
            
                                    
            
            
                | 181 |  |  |         $config = $processor->processConfiguration(new Configuration(true), [ | 
            
                                                        
            
                                    
            
            
                | 182 |  |  |             'jms_serializer' => [ | 
            
                                                        
            
                                    
            
            
                | 183 |  |  |                 'default_context' => $configArray | 
            
                                                        
            
                                    
            
            
                | 184 |  |  |             ] | 
            
                                                        
            
                                    
            
            
                | 185 |  |  |         ]); | 
            
                                                        
            
                                    
            
            
                | 186 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 187 |  |  |         $this->assertArrayHasKey('default_context', $config); | 
            
                                                        
            
                                                                    
                                                                                                        
            
            
                | 188 |  | View Code Duplication |         foreach (['serialization', 'deserialization'] as $configKey) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 189 |  |  |             $this->assertArrayHasKey($configKey, $config['default_context']); | 
            
                                                        
            
                                    
            
            
                | 190 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 191 |  |  |             $defaultContext = $config['default_context'][$configKey]; | 
            
                                                        
            
                                    
            
            
                | 192 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 193 |  |  |             $this->assertTrue(is_array($defaultContext['attributes'])); | 
            
                                                        
            
                                    
            
            
                | 194 |  |  |             $this->assertEmpty($defaultContext['attributes']); | 
            
                                                        
            
                                    
            
            
                | 195 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 196 |  |  |             $this->assertTrue(is_array($defaultContext['groups'])); | 
            
                                                        
            
                                    
            
            
                | 197 |  |  |             $this->assertEmpty($defaultContext['groups']); | 
            
                                                        
            
                                    
            
            
                | 198 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 199 |  |  |             $this->assertArrayNotHasKey('version', $defaultContext); | 
            
                                                        
            
                                    
            
            
                | 200 |  |  |             $this->assertArrayNotHasKey('serialize_null', $defaultContext); | 
            
                                                        
            
                                    
            
            
                | 201 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 202 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 203 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 204 |  |  |  | 
            
                        
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.