1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graze\TelnetClient\Test\Unit; |
4
|
|
|
|
5
|
|
|
use \Mockery as m; |
6
|
|
|
use \Socket\Raw\Socket; |
7
|
|
|
use \Socket\Raw\Factory as SocketFactory; |
8
|
|
|
use \Graze\TelnetClient\PromptMatcher; |
9
|
|
|
use \Graze\TelnetClient\InterpretAsCommand; |
10
|
|
|
use \Graze\TelnetClient\TelnetClient; |
11
|
|
|
use \Graze\TelnetClient\TelnetResponseInterface; |
12
|
|
|
use \Graze\TelnetClient\Exception\TelnetExceptionInterface; |
13
|
|
|
|
14
|
|
|
class TelnetClientTest extends \PHPUnit_Framework_TestCase |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @dataProvider dataProviderConnect |
18
|
|
|
* |
19
|
|
|
* @param string $prompt |
20
|
|
|
* @param string $promptError |
21
|
|
|
* @param string $lineEnding |
22
|
|
|
* @param float|null $timeout |
23
|
|
|
* |
24
|
|
|
* @return void |
25
|
|
|
*/ |
26
|
|
|
public function testConnect($prompt = null, $promptError = null, $lineEnding = null, $timeout = null) |
27
|
|
|
{ |
28
|
|
|
$dsn = 'localhost:80'; |
29
|
|
|
$socket = m::mock(Socket::class); |
30
|
|
|
$socketFactory = m::mock(SocketFactory::class) |
31
|
|
|
->shouldReceive('createClient') |
32
|
|
|
->with($dsn, $timeout) |
33
|
|
|
->andReturn($socket) |
34
|
|
|
->once() |
35
|
|
|
->getMock(); |
36
|
|
|
|
37
|
|
|
$telnetClient = m::mock( |
38
|
|
|
TelnetClient::class, |
39
|
|
|
[$socketFactory, m::mock(PromptMatcher::class), m::mock(InterpretAsCommand::class)] |
40
|
|
|
) |
41
|
|
|
->shouldReceive('setSocket') |
42
|
|
|
->with($socket) |
43
|
|
|
->once(); |
44
|
|
|
|
45
|
|
|
if ($prompt !== null) { |
46
|
|
|
$telnetClient |
47
|
|
|
->shouldReceive('setPrompt') |
48
|
|
|
->with($prompt) |
49
|
|
|
->once(); |
50
|
|
|
} else { |
51
|
|
|
$telnetClient |
52
|
|
|
->shouldReceive('setPrompt') |
53
|
|
|
->never(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if ($promptError !== null) { |
57
|
|
|
$telnetClient |
58
|
|
|
->shouldReceive('setPromptError') |
59
|
|
|
->with($promptError) |
60
|
|
|
->once(); |
61
|
|
|
} else { |
62
|
|
|
$telnetClient |
63
|
|
|
->shouldReceive('setPromptError') |
64
|
|
|
->never(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if ($lineEnding !== null) { |
68
|
|
|
$telnetClient |
69
|
|
|
->shouldReceive('setLineEnding') |
70
|
|
|
->with($lineEnding) |
71
|
|
|
->once(); |
72
|
|
|
} else { |
73
|
|
|
$telnetClient |
74
|
|
|
->shouldReceive('setLineEnding') |
75
|
|
|
->never(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$telnetClient = $telnetClient->getMock()->makePartial(); |
79
|
|
|
$telnetClient->connect($dsn, $prompt, $promptError, $lineEnding, $timeout); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
|
|
public function dataProviderConnect() |
86
|
|
|
{ |
87
|
|
|
return [ |
88
|
|
|
['PROMPT', 'PROMPTERROR', 'LINENDING'], |
89
|
|
|
['PROMPT', 'PROMPTERROR', 'LINENDING', 5], |
90
|
|
|
[null, null, null] |
91
|
|
|
]; |
92
|
|
|
} |
93
|
|
|
/** |
94
|
|
|
* @dataProvider dataProviderExecute |
95
|
|
|
* |
96
|
|
|
* @param string $command |
97
|
|
|
* @param array $promptMatches |
98
|
|
|
* @param bool $isError |
99
|
|
|
* @param string $promptResponse |
100
|
|
|
* @param string $promptGlobal |
101
|
|
|
* @param string $promptLocal |
102
|
|
|
* @param string $promptError |
103
|
|
|
* @param string $lineEnding |
104
|
|
|
* |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
|
View Code Duplication |
public function testExecute( |
|
|
|
|
108
|
|
|
$command, |
109
|
|
|
array $promptMatches, |
110
|
|
|
$isError, |
111
|
|
|
$promptResponse, |
112
|
|
|
$promptGlobal = null, |
113
|
|
|
$promptLocal = null, |
114
|
|
|
$promptError = null, |
115
|
|
|
$lineEnding = null |
116
|
|
|
) { |
117
|
|
|
$lineEnding = $lineEnding ?: "\n"; |
118
|
|
|
$socket = m::mock(Socket::class) |
119
|
|
|
->shouldReceive('write') |
120
|
|
|
->with($command.$lineEnding) |
121
|
|
|
->once(); |
122
|
|
|
|
123
|
|
|
// echo the command back when reading each character one-by-one from the socket |
124
|
|
|
$commandResponse = str_split($command.$lineEnding.$promptResponse.$lineEnding); |
125
|
|
|
$socket = $socket |
126
|
|
|
->shouldReceive('read') |
127
|
|
|
->with(1); |
128
|
|
|
call_user_func_array([$socket, 'andReturn'], $commandResponse); |
129
|
|
|
$socket = $socket |
130
|
|
|
->times(count($commandResponse)) |
131
|
|
|
->shouldReceive('close') |
132
|
|
|
->once() |
133
|
|
|
->getMock(); |
134
|
|
|
|
135
|
|
|
$socketFactory = m::mock(SocketFactory::class) |
136
|
|
|
->shouldReceive('createClient') |
137
|
|
|
->andReturn($socket) |
138
|
|
|
->once() |
139
|
|
|
->getMock(); |
140
|
|
|
|
141
|
|
|
$promptMatcher = new PromptMatcher(); |
142
|
|
|
|
143
|
|
|
$interpretAsCommand = m::mock(InterpretAsCommand::class) |
144
|
|
|
->shouldReceive('interpret') |
145
|
|
|
->times(count($commandResponse)) |
146
|
|
|
->andReturn(false) |
147
|
|
|
->getMock(); |
148
|
|
|
|
149
|
|
|
$telnetClient = new TelnetClient($socketFactory, $promptMatcher, $interpretAsCommand); |
150
|
|
|
$telnetClient->connect('127.0.0.1:23', $promptGlobal, $promptError, $lineEnding); |
151
|
|
|
|
152
|
|
|
$response = $telnetClient->execute($command, $promptLocal); |
153
|
|
|
$this->assertInstanceOf(TelnetResponseInterface::class, $response); |
154
|
|
|
$this->assertEquals($isError, $response->isError()); |
155
|
|
|
$this->assertEquals($command, $response->getResponseText()); |
156
|
|
|
$this->assertEquals($promptMatches, $response->getPromptMatches()); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @dataProvider dataProviderExecute |
161
|
|
|
* |
162
|
|
|
* @param string $command |
163
|
|
|
* @param array $promptMatches |
164
|
|
|
* @param bool $isError |
165
|
|
|
* @param string $promptResponse |
166
|
|
|
* @param string $promptGlobal |
167
|
|
|
* @param string $promptLocal |
168
|
|
|
* @param string $promptError |
169
|
|
|
* @param string $lineEnding |
170
|
|
|
* |
171
|
|
|
* @return void |
172
|
|
|
*/ |
173
|
|
View Code Duplication |
public function testExecutePromptError( |
|
|
|
|
174
|
|
|
$command, |
175
|
|
|
array $promptMatches, |
176
|
|
|
$isError, |
177
|
|
|
$promptResponse, |
178
|
|
|
$promptGlobal = null, |
179
|
|
|
$promptLocal = null, |
180
|
|
|
$promptError = null, |
181
|
|
|
$lineEnding = null |
182
|
|
|
) { |
183
|
|
|
$lineEnding = $lineEnding ?: "\n"; |
184
|
|
|
$socket = m::mock(Socket::class) |
185
|
|
|
->shouldReceive('write') |
186
|
|
|
->with($command.$lineEnding) |
187
|
|
|
->once(); |
188
|
|
|
|
189
|
|
|
// echo the command back when reading each character one-by-one from the socket |
190
|
|
|
$commandResponse = str_split($command.$lineEnding.$promptResponse.$lineEnding); |
191
|
|
|
$socket = $socket |
|
|
|
|
192
|
|
|
->shouldReceive('read') |
193
|
|
|
->with(1); |
194
|
|
|
call_user_func_array([$socket, 'andReturn'], $commandResponse); |
195
|
|
|
$socket = $socket |
196
|
|
|
->times(count($commandResponse)) |
197
|
|
|
->shouldReceive('close') |
198
|
|
|
->once() |
199
|
|
|
->getMock(); |
200
|
|
|
|
201
|
|
|
$socketFactory = m::mock(SocketFactory::class) |
202
|
|
|
->shouldReceive('createClient') |
203
|
|
|
->andReturn($socket) |
204
|
|
|
->once() |
205
|
|
|
->getMock(); |
206
|
|
|
|
207
|
|
|
$promptMatcher = new PromptMatcher(); |
208
|
|
|
|
209
|
|
|
$interpretAsCommand = m::mock(InterpretAsCommand::class) |
210
|
|
|
->shouldReceive('interpret') |
211
|
|
|
->times(count($commandResponse)) |
212
|
|
|
->andReturn(false) |
213
|
|
|
->getMock(); |
214
|
|
|
|
215
|
|
|
$telnetClient = new TelnetClient($socketFactory, $promptMatcher, $interpretAsCommand); |
216
|
|
|
$telnetClient->connect('127.0.0.1:23', $promptGlobal, null, $lineEnding); |
217
|
|
|
|
218
|
|
|
$response = $telnetClient->execute($command, $promptLocal, $promptError); |
219
|
|
|
$this->assertInstanceOf(TelnetResponseInterface::class, $response); |
220
|
|
|
$this->assertEquals($isError, $response->isError()); |
221
|
|
|
$this->assertEquals($command, $response->getResponseText()); |
222
|
|
|
$this->assertEquals($promptMatches, $response->getPromptMatches()); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @return array |
227
|
|
|
*/ |
228
|
|
|
public function dataProviderExecute() |
229
|
|
|
{ |
230
|
|
|
return [ |
231
|
|
|
['party', ['$'], false, "\$"], // success |
232
|
|
|
['party', ['OK'], false, 'OK', 'OK'], // success custom global prompt |
233
|
|
|
['party', ['OK'], false, 'OK', null, 'OK'], // success custom local prompt |
234
|
|
|
['party', ['ERROR'], true, 'ERROR'], // error |
235
|
|
|
// error custom prompt |
236
|
|
|
['party', ['DAMN 123', 'DAMN', '123'], true, 'DAMN 123', null, null, '(DAMN) ([0-9]{3})'], |
237
|
|
|
['party', ['$'], false, "\$", null, null, null, 'LOL'], // success custom line ending |
238
|
|
|
['party', ['ERROR'], true, "ERROR", null, null, null, 'LOL'], // error custom line ending |
239
|
|
|
]; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
public function testExecuteException() |
243
|
|
|
{ |
244
|
|
|
$this->setExpectedException(TelnetExceptionInterface::class); |
|
|
|
|
245
|
|
|
|
246
|
|
|
$client = m::mock(TelnetClient::class)->makePartial(); |
247
|
|
|
$client->execute('aCommand'); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
View Code Duplication |
public function testWriteException() |
|
|
|
|
251
|
|
|
{ |
252
|
|
|
$this->setExpectedException(TelnetExceptionInterface::class); |
|
|
|
|
253
|
|
|
|
254
|
|
|
$client = m::mock(TelnetClient::class)->makePartial(); |
255
|
|
|
|
256
|
|
|
$socket = m::mock(Socket::class) |
257
|
|
|
->shouldReceive('write') |
258
|
|
|
->andThrow(\Exception::class) |
259
|
|
|
->shouldReceive('close') |
260
|
|
|
->getMock(); |
261
|
|
|
|
262
|
|
|
$client->setSocket($socket); |
263
|
|
|
$client->execute('aCommand'); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
View Code Duplication |
public function testReadException() |
|
|
|
|
267
|
|
|
{ |
268
|
|
|
$this->setExpectedException(TelnetExceptionInterface::class); |
|
|
|
|
269
|
|
|
|
270
|
|
|
$client = m::mock(TelnetClient::class)->makePartial(); |
271
|
|
|
|
272
|
|
|
$socket = m::mock(Socket::class) |
273
|
|
|
->shouldReceive('write') |
274
|
|
|
->shouldReceive('close') |
275
|
|
|
->shouldReceive('read') |
276
|
|
|
->andThrow(\Exception::class) |
277
|
|
|
->getMock(); |
278
|
|
|
|
279
|
|
|
$client->setSocket($socket); |
280
|
|
|
$client->execute('aCommand'); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
public function testFactory() |
284
|
|
|
{ |
285
|
|
|
$client = TelnetClient::factory(); |
286
|
|
|
$this->assertInstanceOf(TelnetClient::class, $client); |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
|
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.