|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Gerrie package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Andreas Grunwald <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Gerrie\Tests\Component\Connection; |
|
12
|
|
|
|
|
13
|
|
|
use Gerrie\Component\Connection\SSH; |
|
14
|
|
|
|
|
15
|
|
|
class SSHTest extends \PHPUnit_Framework_TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @return SSH |
|
20
|
|
|
*/ |
|
21
|
|
|
public function getSSHInstance($executable) |
|
22
|
|
|
{ |
|
23
|
|
|
$config = [ |
|
24
|
|
|
'KeyFile' => '', |
|
25
|
|
|
'Port' => 0 |
|
26
|
|
|
]; |
|
27
|
|
|
|
|
28
|
|
|
$sshInstance = new SSH($executable, $config); |
|
29
|
|
|
|
|
30
|
|
|
return $sshInstance; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function testGetterAndSetterExecutable() |
|
34
|
|
|
{ |
|
35
|
|
|
$instance = $this->getSSHInstance(''); |
|
36
|
|
|
$executable = '/usr/local/bin/ssh'; |
|
37
|
|
|
|
|
38
|
|
|
$this->assertEmpty($instance->getExecutable()); |
|
39
|
|
|
|
|
40
|
|
|
$instance->setExecutable($executable); |
|
41
|
|
|
$this->assertEquals($executable, $instance->getExecutable()); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function testGetterAndSetterKeyFile() |
|
45
|
|
|
{ |
|
46
|
|
|
$instance = $this->getSSHInstance(''); |
|
47
|
|
|
$sshKeyFile = '/Users/max/.ssh/id_rsa_unittest'; |
|
48
|
|
|
|
|
49
|
|
|
$this->assertEmpty($instance->getKeyFile()); |
|
50
|
|
|
|
|
51
|
|
|
$instance->setKeyFile($sshKeyFile); |
|
52
|
|
|
$this->assertEquals($sshKeyFile, $instance->getKeyFile()); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function testGetterAndSetterWithValidPort() |
|
56
|
|
|
{ |
|
57
|
|
|
$instance = $this->getSSHInstance(''); |
|
58
|
|
|
$port = 12345; |
|
59
|
|
|
|
|
60
|
|
|
$this->assertEquals(0, $instance->getPort()); |
|
61
|
|
|
|
|
62
|
|
|
$instance->setPort($port); |
|
63
|
|
|
$this->assertEquals($port, $instance->getPort()); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function testGetterAndSetterWithInvalidPort() |
|
67
|
|
|
{ |
|
68
|
|
|
$instance = $this->getSSHInstance(''); |
|
69
|
|
|
$port = 'FooBar'; |
|
70
|
|
|
|
|
71
|
|
|
$this->assertEquals(0, $instance->getPort()); |
|
72
|
|
|
|
|
73
|
|
|
$instance->setPort($port); |
|
74
|
|
|
$this->assertEquals(0, $instance->getPort()); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function testAddCommandParts() |
|
78
|
|
|
{ |
|
79
|
|
|
$instance = $this->getSSHInstance(''); |
|
80
|
|
|
$commandParts = [ |
|
81
|
|
|
'ls-project', |
|
82
|
|
|
'example.com' |
|
83
|
|
|
]; |
|
84
|
|
|
|
|
85
|
|
|
$this->assertEquals([], $instance->getCommandParts()); |
|
86
|
|
|
|
|
87
|
|
|
$instance->addCommandPart($commandParts[0]); |
|
88
|
|
|
$instance->addCommandPart($commandParts[1]); |
|
89
|
|
|
|
|
90
|
|
|
$this->assertEquals($commandParts, $instance->getCommandParts()); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function testAddArguments() |
|
94
|
|
|
{ |
|
95
|
|
|
$instance = $this->getSSHInstance(''); |
|
96
|
|
|
$arguments = [ |
|
97
|
|
|
['argument', 'value', '='], |
|
98
|
|
|
['--branch', '"\'fo', ' '] |
|
99
|
|
|
]; |
|
100
|
|
|
$argumentsResult = [ |
|
101
|
|
|
0 => "argument='value'", |
|
102
|
|
|
1 => "--branch '\"'\''fo'" |
|
103
|
|
|
]; |
|
104
|
|
|
|
|
105
|
|
|
$this->assertEquals([], $instance->getArguments()); |
|
106
|
|
|
|
|
107
|
|
|
$instance->addArgument($arguments[0][0], $arguments[0][1], $arguments[0][2]); |
|
108
|
|
|
$instance->addArgument($arguments[1][0], $arguments[1][1], $arguments[1][2]); |
|
109
|
|
|
|
|
110
|
|
|
$this->assertEquals($argumentsResult, $instance->getArguments()); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function testResetArgumentsAndCommandParts() |
|
114
|
|
|
{ |
|
115
|
|
|
$instance = $this->getSSHInstance(''); |
|
116
|
|
|
$commandParts = [ |
|
117
|
|
|
'ls-project', |
|
118
|
|
|
'example.com' |
|
119
|
|
|
]; |
|
120
|
|
|
$arguments = [ |
|
121
|
|
|
['argument', 'value', '='], |
|
122
|
|
|
['--branch', '"\'fo', ' '] |
|
123
|
|
|
]; |
|
124
|
|
|
|
|
125
|
|
|
$this->assertEquals([], $instance->getCommandParts()); |
|
126
|
|
|
$this->assertEquals([], $instance->getArguments()); |
|
127
|
|
|
|
|
128
|
|
|
$instance->addCommandPart($commandParts[0]); |
|
129
|
|
|
$instance->addCommandPart($commandParts[1]); |
|
130
|
|
|
|
|
131
|
|
|
$instance->addArgument($arguments[0][0], $arguments[0][1], $arguments[0][2]); |
|
132
|
|
|
$instance->addArgument($arguments[1][0], $arguments[1][1], $arguments[1][2]); |
|
133
|
|
|
|
|
134
|
|
|
$this->assertCount(2, $instance->getCommandParts()); |
|
135
|
|
|
$this->assertCount(2, $instance->getArguments()); |
|
136
|
|
|
|
|
137
|
|
|
$instance->reset(); |
|
138
|
|
|
|
|
139
|
|
|
$this->assertCount(0, $instance->getCommandParts()); |
|
140
|
|
|
$this->assertCount(0, $instance->getArguments()); |
|
141
|
|
|
$this->assertInternalType('array', $instance->getCommandParts()); |
|
142
|
|
|
$this->assertInternalType('array', $instance->getArguments()); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function testGetEmptyCommand() |
|
146
|
|
|
{ |
|
147
|
|
|
$instance = $this->getSSHInstance(''); |
|
148
|
|
|
|
|
149
|
|
|
$this->assertEquals('', $instance->getCommand()); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
public function testGetCommand() |
|
153
|
|
|
{ |
|
154
|
|
|
$instance = $this->getSSHInstance('/usr/bin/ssh'); |
|
155
|
|
|
|
|
156
|
|
|
$commandParts = [ |
|
157
|
|
|
'ls-project' |
|
158
|
|
|
]; |
|
159
|
|
|
$arguments = [ |
|
160
|
|
|
['argument', 'value', '='], |
|
161
|
|
|
['--branch', '"\'fo', ' '] |
|
162
|
|
|
]; |
|
163
|
|
|
|
|
164
|
|
|
$instance->addCommandPart($commandParts[0]); |
|
165
|
|
|
|
|
166
|
|
|
$instance->addArgument($arguments[0][0], $arguments[0][1], $arguments[0][2]); |
|
167
|
|
|
$instance->addArgument($arguments[1][0], $arguments[1][1], $arguments[1][2]); |
|
168
|
|
|
|
|
169
|
|
|
$command = "/usr/bin/ssh ls-project argument='value' --branch '\"'\''fo' 2>&1"; |
|
170
|
|
|
$this->assertEquals($command, $instance->getCommand()); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
public function testGetCommandWithKeyFile() |
|
174
|
|
|
{ |
|
175
|
|
|
$instance = $this->getSSHInstance('/usr/bin/ssh'); |
|
176
|
|
|
$instance->setKeyFile('/Users/max/.ssh/id_rsa'); |
|
177
|
|
|
|
|
178
|
|
|
$commandParts = [ |
|
179
|
|
|
'ls-project' |
|
180
|
|
|
]; |
|
181
|
|
|
$arguments = [ |
|
182
|
|
|
['argument', 'value', '='], |
|
183
|
|
|
['--branch', '"\'fo', ' '] |
|
184
|
|
|
]; |
|
185
|
|
|
|
|
186
|
|
|
$instance->addCommandPart($commandParts[0]); |
|
187
|
|
|
|
|
188
|
|
|
$instance->addArgument($arguments[0][0], $arguments[0][1], $arguments[0][2]); |
|
189
|
|
|
$instance->addArgument($arguments[1][0], $arguments[1][1], $arguments[1][2]); |
|
190
|
|
|
|
|
191
|
|
|
$command = "/usr/bin/ssh -i /Users/max/.ssh/id_rsa ls-project argument='value' --branch '\"'\''fo' 2>&1"; |
|
192
|
|
|
$this->assertEquals($command, $instance->getCommand()); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
public function testGetCommandWithPort() |
|
196
|
|
|
{ |
|
197
|
|
|
$instance = $this->getSSHInstance('/usr/bin/ssh'); |
|
198
|
|
|
$instance->setPort(29418); |
|
199
|
|
|
|
|
200
|
|
|
$commandParts = [ |
|
201
|
|
|
'ls-project' |
|
202
|
|
|
]; |
|
203
|
|
|
$arguments = [ |
|
204
|
|
|
['argument', 'value', '='], |
|
205
|
|
|
['--branch', '"\'fo', ' '] |
|
206
|
|
|
]; |
|
207
|
|
|
|
|
208
|
|
|
$instance->addCommandPart($commandParts[0]); |
|
209
|
|
|
|
|
210
|
|
|
$instance->addArgument($arguments[0][0], $arguments[0][1], $arguments[0][2]); |
|
211
|
|
|
$instance->addArgument($arguments[1][0], $arguments[1][1], $arguments[1][2]); |
|
212
|
|
|
|
|
213
|
|
|
$command = "/usr/bin/ssh -p 29418 ls-project argument='value' --branch '\"'\''fo' 2>&1"; |
|
214
|
|
|
$this->assertEquals($command, $instance->getCommand()); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
public function testExecuteCommand() |
|
218
|
|
|
{ |
|
219
|
|
|
$output = 'Gerrie Unit test'; |
|
220
|
|
|
|
|
221
|
|
|
$instance = $this->getSSHInstance('echo'); |
|
222
|
|
|
$instance->addArgument('', '', $output); |
|
223
|
|
|
|
|
224
|
|
|
$result = $instance->execute(false); |
|
225
|
|
|
|
|
226
|
|
|
$this->assertEquals([$output], $result); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
public function testExecuteCommandWithImplode() |
|
230
|
|
|
{ |
|
231
|
|
|
$output = 'Gerrie Unit test - By Gerrie'; |
|
232
|
|
|
|
|
233
|
|
|
$instance = $this->getSSHInstance('echo'); |
|
234
|
|
|
$instance->addArgument('', '', $output); |
|
235
|
|
|
|
|
236
|
|
|
$result = $instance->execute(true); |
|
237
|
|
|
|
|
238
|
|
|
$this->assertEquals($output, $result); |
|
239
|
|
|
} |
|
240
|
|
|
} |