DeploymentUtilsTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 163
Duplicated Lines 22.7 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 11
Bugs 1 Features 4
Metric Value
wmc 8
c 11
b 1
f 4
lcom 1
cbo 4
dl 37
loc 163
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testDeploySteps() 0 13 1
A methodsAndCountsProvider() 0 11 1
A testStartApplication() 13 13 1
B testIsInitialDeploy() 0 29 1
A testSetEnvironmentVariables() 11 11 1
A testAddRoute() 13 13 1
A getDeploymentDouble() 0 17 1
A getOutputDouble() 0 8 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
 * test suite to validate cloud foundry deployment utils.
4
 */
5
namespace Graviton\Deployment\Tests\Services\CloudFoundry;
6
7
use Graviton\Deployment\DeployScriptsTestCase;
8
use Graviton\Deployment\Services\CloudFoundry\DeploymentUtils;
9
use Symfony\Component\Process\Exception\ProcessFailedException;
10
11
/**
12
 * @author   List of contributors <https://github.com/libgraviton/deploy-scripts/graphs/contributors>
13
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
14
 * @link     http://swisscom.ch
15
 */
16
class DeploymentUtilsTest extends DeployScriptsTestCase
17
{
18
    /**
19
     * validates a deploy command.
20
     *
21
     * @dataProvider methodsAndCountsProvider
22
     *
23
     * @param string $command      Command to be run
24
     * @param array  $methodCounts amount of calls by method
25
     *
26
     * @return void
27
     */
28
    public function testDeploySteps($command, array $methodCounts)
29
    {
30
        $processDouble = $this->getMock('\Symfony\Component\Process\ProcessBuilder');
31
32
        DeploymentUtils::$command(
33
            $this->getDeploymentDouble($processDouble, $methodCounts),
34
            $this->getOutputDouble(),
35
            $this->getConfigurationSet(),
36
            'test-test',
37
            'graviton',
38
            'blue'
39
        );
40
    }
41
42
    /**
43
     * @return array
44
     */
45
    public function methodsAndCountsProvider()
46
    {
47
        return array(
48
            'login' => array('login', array('registerSteps' => 1, 'deploy' => 1)),
49
            'logout' => array('logout', array('registerSteps' => 1, 'deploy' => 1)),
50
            'cleanUp' => array('cleanUp', array('registerSteps' => 1, 'deploy' => 1)),
51
            'deploy' => array('deploy', array('registerSteps' => 1, 'deploy' => 1)),
52
            'determineDeploymentSlice' => array('determineDeploymentSlice', array('registerSteps' => 2, 'deploy' => 2)),
53
            'createServices' => array('createServices', array('registerSteps' => 1, 'deploy' => 1)),
54
        );
55
    }
56
57
    /**
58
     * validates the start application command
59
     *
60
     * @return void
61
     */
62 View Code Duplication
    public function testStartApplication()
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...
63
    {
64
        $processDouble = $this->getMock('\Symfony\Component\Process\ProcessBuilder');
65
66
        DeploymentUtils::startApplication(
67
            $this->getDeploymentDouble($processDouble, ['registerSteps' => 1, 'deploy' => 1]),
68
            $this->getOutputDouble(),
69
            $this->getConfigurationSet(),
70
            'test-test',
71
            'graviton',
72
            'blue'
0 ignored issues
show
Unused Code introduced by
The call to DeploymentUtils::startApplication() has too many arguments starting with 'blue'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
73
        );
74
    }
75
76
    /**
77
     * @return void
78
     */
79
    public function testIsInitialDeploy()
80
    {
81
        $processDouble = $this->getMockBuilder('\Symfony\Component\Process\Process')
82
            ->disableOriginalConstructor()
83
            ->getMock();
84
        $processBuilderDouble = $this->getMock('\Symfony\Component\Process\ProcessBuilder');
85
        $deploymentDouble = $this->getMockBuilder('\Graviton\Deployment\Deployment')
86
            ->setConstructorArgs(array($processBuilderDouble))
87
            ->setMethods(array('registerSteps', 'deploy'))
88
            ->getMock();
89
        $deploymentDouble
90
            ->expects($this->exactly(2))
91
            ->method('registerSteps')
92
            ->with($this->isType('array'))
93
            ->willReturn($deploymentDouble);
94
        $deploymentDouble
95
            ->expects($this->exactly(2))
96
            ->method('deploy')
97
            ->willThrowException(new ProcessFailedException($processDouble));
98
99
        DeploymentUtils::determineDeploymentSlice(
100
            $deploymentDouble,
101
            $this->getOutputDouble(),
102
            $this->getConfigurationSet(),
103
            'test-test'
104
        );
105
106
        $this->assertTrue(DeploymentUtils::isInitialDeploy());
107
    }
108
109
    /**
110
     * @return void
111
     */
112 View Code Duplication
    public function testSetEnvironmentVariables()
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...
113
    {
114
        $processDouble = $this->getMock('\Symfony\Component\Process\ProcessBuilder');
115
116
        DeploymentUtils::setEnvironmentVariables(
117
            $this->getDeploymentDouble($processDouble, array('registerSteps' => 1, 'deploy' => 1)),
118
            $this->getOutputDouble(),
119
            $this->getConfigurationSet(),
120
            'Test_test-unstable-blue'
121
        );
122
    }
123
124
    /**
125
     * @return void
126
     */
127 View Code Duplication
    public function testAddRoute()
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...
128
    {
129
        $processDouble = $this->getMock('\Symfony\Component\Process\ProcessBuilder');
130
131
        DeploymentUtils::addRoute(
132
            $this->getDeploymentDouble($processDouble, array('registerSteps' => 1, 'deploy' => 1)),
133
            $this->getOutputDouble(),
134
            $this->getConfigurationSet(),
135
            'Test_test-unstable',
136
            'blue',
137
            'Test_test-unstable'
138
        );
139
    }
140
141
    /**
142
     * Provides an instance of the Deployment class.
143
     *
144
     * @param \Symfony\Component\Process\ProcessBuilder $processDouble Stub to be able to test.
145
     * @param array                                     $methodCounts  What methods shall be called how often?
146
     *
147
     * @return \PHPUnit_Framework_MockObject_MockObject
148
     */
149
    private function getDeploymentDouble($processDouble, array $methodCounts)
150
    {
151
        $deployDouble = $this->getMockBuilder('\Graviton\Deployment\Deployment')
152
            ->setConstructorArgs(array($processDouble))
153
            ->setMethods(array('registerSteps', 'deploy'))
154
            ->getMock();
155
        $deployDouble
156
            ->expects($this->exactly($methodCounts['registerSteps']))
157
            ->method('registerSteps')
158
            ->with($this->isType('array'))
159
            ->willReturn($deployDouble);
160
        $deployDouble
161
            ->expects($this->exactly($methodCounts['deploy']))
162
            ->method('deploy');
163
164
        return $deployDouble;
165
    }
166
167
    /**
168
     * @return \PHPUnit_Framework_MockObject_MockObject
169
     */
170
    private function getOutputDouble()
171
    {
172
        $outputDouble = $this->getMockBuilder('\Symfony\Component\Console\Output\OutputInterface')
173
            ->setMethods(array('write', 'writeln'))
174
            ->getMockForAbstractClass();
175
176
        return $outputDouble;
177
    }
178
}
179