1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Validate deploy class |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Graviton\Deployment; |
7
|
|
|
|
8
|
|
|
use Symfony\Component\Process\ProcessBuilder; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @author List of contributors <https://github.com/libgraviton/deploy-scripts/graphs/contributors> |
12
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
13
|
|
|
* @link http://swisscom.ch |
14
|
|
|
*/ |
15
|
|
|
class DeploymentTest extends \PHPUnit_Framework_TestCase |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* testDeployWithOneStep |
19
|
|
|
* |
20
|
|
|
* @return void |
21
|
|
|
*/ |
22
|
|
|
public function testDeployWithOneStep() |
23
|
|
|
{ |
24
|
|
|
$command = array('helloWorldCmd'); |
25
|
|
|
|
26
|
|
|
$step = $this->getMock('Graviton\Deployment\Steps\StepInterface'); |
27
|
|
|
$step |
28
|
|
|
->expects($this->once()) |
29
|
|
|
->method('getCommand') |
30
|
|
|
->willReturn($command); |
31
|
|
|
|
32
|
|
|
$process = $this->getProcessDouble(); |
33
|
|
|
$process |
34
|
|
|
->expects($this->once()) |
35
|
|
|
->method('mustRun'); |
36
|
|
|
|
37
|
|
|
$processBuilder = $this->getMock('\Symfony\Component\Process\ProcessBuilder'); |
38
|
|
|
$processBuilder |
39
|
|
|
->expects($this->once()) |
40
|
|
|
->method('setArguments') |
41
|
|
|
->with($this->equalTo($command)) |
42
|
|
|
->willReturn($processBuilder); |
43
|
|
|
$processBuilder |
44
|
|
|
->expects($this->once()) |
45
|
|
|
->method('getProcess') |
46
|
|
|
->willReturn($process); |
47
|
|
|
|
48
|
|
|
$deployment = new Deployment($processBuilder); |
49
|
|
|
$deployment |
50
|
|
|
->registerSteps([$step]) |
51
|
|
|
->deploy(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* testDeployWithManySteps |
56
|
|
|
* |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
|
|
public function testDeployWithManySteps() |
60
|
|
|
{ |
61
|
|
|
$command = array('helloWorldCmd'); |
62
|
|
|
|
63
|
|
|
$step = $this->getMock('Graviton\Deployment\Steps\StepInterface'); |
64
|
|
|
$step |
65
|
|
|
->expects($this->exactly(2)) |
66
|
|
|
->method('getCommand') |
67
|
|
|
->willReturn($command); |
68
|
|
|
|
69
|
|
|
$process = $this->getProcessDouble(); |
70
|
|
|
$process |
71
|
|
|
->expects($this->exactly(2)) |
72
|
|
|
->method('mustRun'); |
73
|
|
|
|
74
|
|
|
$processBuilder = $this->getMock('\Symfony\Component\Process\ProcessBuilder'); |
75
|
|
|
$processBuilder |
76
|
|
|
->expects($this->exactly(2)) |
77
|
|
|
->method('setArguments') |
78
|
|
|
->with($this->equalTo($command)) |
79
|
|
|
->willReturn($processBuilder); |
80
|
|
|
$processBuilder |
81
|
|
|
->expects($this->exactly(2)) |
82
|
|
|
->method('getProcess') |
83
|
|
|
->willReturn($process); |
84
|
|
|
|
85
|
|
|
$deployment = $this->getDeploymentObject($processBuilder, [$step, $step]); |
86
|
|
|
$deployment->deploy(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Validates registerSteps |
91
|
|
|
* |
92
|
|
|
* @return void |
93
|
|
|
*/ |
94
|
|
|
public function testNoStepsRegistered() |
95
|
|
|
{ |
96
|
|
|
$processBuilder = new \Symfony\Component\Process\ProcessBuilder; |
97
|
|
|
|
98
|
|
|
$deployment = new Deployment($processBuilder); |
99
|
|
|
$deployment->registerSteps(array()); |
100
|
|
|
|
101
|
|
|
$output = $deployment->deploy(); |
102
|
|
|
|
103
|
|
|
$this->assertSame('No steps registered! Aborting.', $output); |
104
|
|
|
$this->assertAttributeCount(0, 'steps', $deployment); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return void |
109
|
|
|
*/ |
110
|
|
|
public function testResetSteps() |
111
|
|
|
{ |
112
|
|
|
$deployment = $this->getDeploymentObject( |
113
|
|
|
$this->getMock('\Symfony\Component\Process\ProcessBuilder'), |
114
|
|
|
[$this->getMock('Graviton\Deployment\Steps\StepInterface')] |
115
|
|
|
); |
116
|
|
|
|
117
|
|
|
$this->assertAttributeCount(1, 'steps', $deployment); |
118
|
|
|
$this->assertInstanceOf( |
119
|
|
|
'Graviton\Deployment\Deployment', |
120
|
|
|
$deployment->resetSteps() |
121
|
|
|
); |
122
|
|
|
$this->assertAttributeCount(0, 'steps', $deployment); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return void |
127
|
|
|
*/ |
128
|
|
|
public function testRegisterInvalidTest() |
129
|
|
|
{ |
130
|
|
|
$deployment = new Deployment($this->getMock('\Symfony\Component\Process\ProcessBuilder')); |
131
|
|
|
|
132
|
|
|
$this->setExpectedException('\InvalidArgumentException'); |
133
|
|
|
$deployment->registerSteps(['invalid step type']); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Provides an instance of the \Symfony\Component\Process\Process |
138
|
|
|
* |
139
|
|
|
* @param array $methods Set of methods to be stubbed. |
140
|
|
|
* |
141
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject |
142
|
|
|
*/ |
143
|
|
|
protected function getProcessDouble(array $methods = array()) |
144
|
|
|
{ |
145
|
|
|
$process = $this->getMockBuilder('\Symfony\Component\Process\Process') |
146
|
|
|
->disableOriginalConstructor() |
147
|
|
|
->setMethods($methods) |
148
|
|
|
->getMock(); |
149
|
|
|
|
150
|
|
|
return $process; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param ProcessBuilder $processBuilder Test double of the SF2 ProcessBuilder |
155
|
|
|
* @param array $steps List of steps to be registered. |
156
|
|
|
* |
157
|
|
|
* @return Deployment |
158
|
|
|
*/ |
159
|
|
|
private function getDeploymentObject(ProcessBuilder $processBuilder, array $steps = array()) |
160
|
|
|
{ |
161
|
|
|
$deployment = new Deployment($processBuilder); |
162
|
|
|
$deployment->registerSteps($steps); |
163
|
|
|
|
164
|
|
|
return $deployment; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|