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() |
|
|
|
|
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' |
|
|
|
|
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() |
|
|
|
|
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() |
|
|
|
|
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
|
|
|
|
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.