TwigExtensionPassTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 75
Duplicated Lines 16 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 6
lcom 1
cbo 6
dl 12
loc 75
rs 10
c 3
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getContainer() 12 12 1
A testStandardExtension() 0 13 1
A testLazyExtension() 0 21 3
A testLazyExtensionNotLoadedWhenOldSerializer() 0 20 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\TwigExtensionPass;
22
use JMS\SerializerBundle\DependencyInjection\JMSSerializerExtension;
23
use PHPUnit\Framework\TestCase;
24
use Symfony\Component\DependencyInjection\ContainerBuilder;
25
26
class TwigExtensionPassTest extends TestCase
27
{
28
    /**
29
     * @return ContainerBuilder
30
     */
31 View Code Duplication
    private function getContainer()
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...
32
    {
33
        $loader = new JMSSerializerExtension();
34
        $container = new ContainerBuilder();
35
36
        $container->setParameter('kernel.debug', true);
37
        $container->setParameter('kernel.cache_dir', sys_get_temp_dir() . '/serializer');
38
        $container->setParameter('kernel.bundles', array());
39
40
        $loader->load([[]], $container);
41
        return $container;
42
    }
43
44
    public function testStandardExtension()
45
    {
46
        $container = $this->getContainer();
47
48
        $pass = new TwigExtensionPass();
49
        $pass->process($container);
50
51
        $extension = $container->getDefinition('jms_serializer.twig_extension.serializer');
52
        $this->assertEquals('%jms_serializer.twig_extension.class%', (string)$extension->getClass());
53
        $this->assertCount(1, $extension->getArguments());
54
55
        $this->assertFalse($container->hasDefinition('jms_serializer.twig_extension.serializer_runtime_helper'));
56
    }
57
58
    public function testLazyExtension()
59
    {
60
        if (
61
            !class_exists('JMS\Serializer\Twig\SerializerRuntimeExtension')
62
            || !interface_exists('Twig_RuntimeLoaderInterface')
63
        ) {
64
            $this->markTestSkipped("Lazy extensions are supported only by serializer 1.7.0");
65
        }
66
        $container = $this->getContainer();
67
68
        $container->register('twig.runtime_loader');
69
70
        $pass = new TwigExtensionPass();
71
        $pass->process($container);
72
73
        $extension = $container->getDefinition('jms_serializer.twig_extension.serializer');
74
        $this->assertEquals('%jms_serializer.twig_runtime_extension.class%', (string)$extension->getClass());
75
        $this->assertCount(0, $extension->getArguments());
76
77
        $this->assertTrue($container->hasDefinition('jms_serializer.twig_extension.serializer_runtime_helper'));
78
    }
79
80
    public function testLazyExtensionNotLoadedWhenOldSerializer()
81
    {
82
        $container = $this->getContainer();
83
84
        $container->getParameterBag()->add(array(
85
            'jms_serializer.twig_runtime_extension.class' => 'foo',
86
            'jms_serializer.twig_runtime_extension_helper.class' => 'bar',
87
        ));
88
89
        $container->register('twig.runtime_loader');
90
91
        $pass = new TwigExtensionPass();
92
        $pass->process($container);
93
94
        $extension = $container->getDefinition('jms_serializer.twig_extension.serializer');
95
        $this->assertEquals('%jms_serializer.twig_extension.class%', (string)$extension->getClass());
96
        $this->assertCount(1, $extension->getArguments());
97
98
        $this->assertFalse($container->hasDefinition('jms_serializer.twig_extension.serializer_runtime_helper'));
99
    }
100
}
101
102