DoctrinePassTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 90
Duplicated Lines 13.33 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 1
cbo 6
dl 12
loc 90
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getContainer() 12 12 1
A testDoctrineDisabled() 0 15 1
A testOrm() 0 13 1
A testOdm() 0 16 1
A testOrmAndOdm() 0 22 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Compiler\DoctrinePass;
22
use JMS\SerializerBundle\DependencyInjection\JMSSerializerExtension;
23
use PHPUnit\Framework\TestCase;
24
use Symfony\Component\DependencyInjection\ContainerBuilder;
25
26
class DoctrinePassTest extends TestCase
27
{
28
    /**
29
     *
30
     * @param array $configs
31
     * @return ContainerBuilder
32
     */
33 View Code Duplication
    private function getContainer(array $configs = array())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
    {
35
        $loader = new JMSSerializerExtension();
36
        $container = new ContainerBuilder();
37
38
        $container->setParameter('kernel.debug', true);
39
        $container->setParameter('kernel.cache_dir', sys_get_temp_dir() . '/serializer');
40
        $container->setParameter('kernel.bundles', array());
41
42
        $loader->load(['jms_serializer' => $configs], $container);
43
        return $container;
44
    }
45
46
    public function testDoctrineDisabled()
47
    {
48
        $container = $this->getContainer(array(
49
            'metadata' => array('infer_types_from_doctrine_metadata' => false)
50
        ));
51
        $container->register('doctrine.orm.entity_manager');
52
53
        $pass = new DoctrinePass();
54
        $pass->process($container);
55
56
        $alias = $container->getAlias('jms_serializer.object_constructor');
57
        $this->assertTrue($alias->isPublic());
58
59
        $this->assertEquals('jms_serializer.unserialize_object_constructor', (string)$alias);
60
    }
61
62
    public function testOrm()
63
    {
64
        $container = $this->getContainer();
65
        $container->register('doctrine.orm.entity_manager');
66
67
        $pass = new DoctrinePass();
68
        $pass->process($container);
69
70
        $alias = $container->getAlias('jms_serializer.object_constructor');
71
        $this->assertTrue($alias->isPublic());
72
73
        $this->assertEquals('jms_serializer.doctrine_object_constructor', (string)$alias);
74
    }
75
76
    public function testOdm()
77
    {
78
        $container = $this->getContainer();
79
        $container->register('doctrine_phpcr.odm.document_manager');
80
81
        $pass = new DoctrinePass();
82
        $pass->process($container);
83
84
        $alias = $container->getAlias('jms_serializer.object_constructor');
85
        $this->assertTrue($alias->isPublic());
86
87
        $this->assertEquals('jms_serializer.doctrine_phpcr_object_constructor', (string)$alias);
88
89
        $def = $container->getDefinition('jms_serializer.doctrine_phpcr_object_constructor');
90
        $this->assertEquals('jms_serializer.unserialize_object_constructor', (string)$def->getArgument(1));
91
    }
92
93
    public function testOrmAndOdm()
94
    {
95
        $container = $this->getContainer();
96
97
        $container->register('doctrine_phpcr.odm.document_manager');
98
        $container->register('doctrine.orm.entity_manager');
99
100
        $pass = new DoctrinePass();
101
        $pass->process($container);
102
103
104
        $alias = $container->getAlias('jms_serializer.object_constructor');
105
        $this->assertTrue($alias->isPublic());
106
107
        $this->assertEquals('jms_serializer.doctrine_object_constructor', (string)$alias);
108
109
        $def = $container->getDefinition('jms_serializer.doctrine_object_constructor');
110
        $this->assertEquals('jms_serializer.doctrine_phpcr_object_constructor', (string)$def->getArgument(1));
111
112
        $def = $container->getDefinition('jms_serializer.doctrine_phpcr_object_constructor');
113
        $this->assertEquals('jms_serializer.unserialize_object_constructor', (string)$def->getArgument(1));
114
    }
115
}
116
117