Completed
Push — master ( e207d9...c74d35 )
by Eric
09:33
created

ResourceCompilerPassTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 94
Duplicated Lines 61.7 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 3
dl 58
loc 94
rs 10

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
 * This file is part of the Ivory Form Extra package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\FormExtraBundle\Tests\DependencyInjection\Compiler;
13
14
use Ivory\FormExtraBundle\DependencyInjection\Compiler\ResourceCompilerPass;
15
16
/**
17
 * Resource compiler pass test.
18
 *
19
 * @author GeLo <[email protected]>
20
 */
21
class ResourceCompilerPassTest extends \PHPUnit_Framework_TestCase
22
{
23
    /** @var \Ivory\FormExtraBundle\DependencyInjection\Compiler\ResourceCompilerPass */
24
    private $compilerPass;
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    protected function setUp()
30
    {
31
        $this->compilerPass = new ResourceCompilerPass();
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    protected function tearDown()
38
    {
39
        unset($this->compilerPass);
40
    }
41
42
    public function testTwigResource()
43
    {
44
        $containerBuilder = $this->createContainerBuilderMock();
45
        $containerBuilder
46
            ->expects($this->exactly(2))
47
            ->method('hasParameter')
48
            ->will($this->returnValueMap(array(
49
                array('templating.helper.form.resources', false),
50
                array($parameter = 'twig.form.resources', true),
51
            )));
52
53
        $containerBuilder
54
            ->expects($this->once())
55
            ->method('getParameter')
56
            ->with($this->identicalTo($parameter))
57
            ->will($this->returnValue(array($template = 'layout.html.twig')));
58
59
        $containerBuilder
60
            ->expects($this->once())
61
            ->method('setParameter')
62
            ->with(
63
                $this->identicalTo($parameter),
64
                $this->identicalTo(array(
65
                    'IvoryFormExtraBundle:Form:javascript.html.twig',
66
                    'IvoryFormExtraBundle:Form:stylesheet.html.twig',
67
                    $template,
68
                ))
69
            );
70
71
        $this->compilerPass->process($containerBuilder);
72
    }
73
74
    public function testPhpResource()
75
    {
76
        $containerBuilder = $this->createContainerBuilderMock();
77
        $containerBuilder
78
            ->expects($this->exactly(2))
79
            ->method('hasParameter')
80
            ->will($this->returnValueMap(array(
81
                array($parameter = 'templating.helper.form.resources', true),
82
                array('twig.form.resources', false),
83
            )));
84
85
        $containerBuilder
86
            ->expects($this->once())
87
            ->method('getParameter')
88
            ->with($this->identicalTo($parameter))
89
            ->will($this->returnValue(array($template = 'layout.html.php')));
90
91
        $containerBuilder
92
            ->expects($this->once())
93
            ->method('setParameter')
94
            ->with(
95
                $this->identicalTo($parameter),
96
                $this->identicalTo(array('IvoryFormExtraBundle:Form', $template))
97
            );
98
99
        $this->compilerPass->process($containerBuilder);
100
    }
101
102
    /**
103
     * Creates a container builder mock.
104
     *
105
     * @return \Symfony\Component\DependencyInjection\ContainerBuilder|\PHPUnit_Framework_MockObject_MockObject
106
     */
107
    private function createContainerBuilderMock()
108
    {
109
        return $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')
110
            ->disableOriginalConstructor()
111
            ->setMethods(array('hasParameter', 'getParameter', 'setParameter'))
112
            ->getMock();
113
    }
114
}
115