1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graze\TelnetClient\Test\Unit; |
4
|
|
|
|
5
|
|
|
use \Mockery as m; |
6
|
|
|
use \Socket\Raw\Socket; |
7
|
|
|
use \Graze\TelnetClient\InterpretAsCommand; |
8
|
|
|
use \Graze\TelnetClient\Exception\UndefinedCommandException; |
9
|
|
|
use \Graze\TelnetClient\Exception\TelnetExceptionInterface; |
10
|
|
|
|
11
|
|
|
class InterpretAsCommandTest extends \PHPUnit_Framework_TestCase |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @dataProvider dataProviderInterpret |
15
|
|
|
* |
16
|
|
|
* @param bool $isIac |
17
|
|
|
* @param string $character |
18
|
|
|
* @param string $command |
19
|
|
|
* @param string $option |
20
|
|
|
* @param string $response |
21
|
|
|
* |
22
|
|
|
* @return void |
23
|
|
|
*/ |
24
|
|
|
public function testInterpret($isIac, $character, $command = null, $option = null, $response = null) |
25
|
|
|
{ |
26
|
|
|
$socket = m::mock(Socket::class); |
27
|
|
|
|
28
|
|
|
if ($isIac) { |
29
|
|
|
$socket |
30
|
|
|
->shouldReceive('read') |
31
|
|
|
->andReturn($command, $option) |
32
|
|
|
->twice() |
33
|
|
|
->shouldReceive('write') |
34
|
|
|
->with($response) |
35
|
|
|
->once() |
36
|
|
|
->getMock(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$interpretAsCommand = new InterpretAsCommand(); |
40
|
|
|
$this->assertEquals($isIac, $interpretAsCommand->interpret($character, $socket)); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
|
|
public function dataProviderInterpret() |
47
|
|
|
{ |
48
|
|
|
// can't put this in setUp() as dataProviders are called first |
49
|
|
|
$WILL = chr(251); |
50
|
|
|
$WONT = chr(252); |
51
|
|
|
$DO = chr(253); |
52
|
|
|
$DONT = chr(254); |
53
|
|
|
$IAC = chr(255); |
54
|
|
|
|
55
|
|
|
return [ |
56
|
|
|
[true, $IAC, $DO, '1', $IAC.$WONT.'1'], |
57
|
|
|
[true, $IAC, $DONT, '3', $IAC.$WONT.'3'], |
58
|
|
|
[true, $IAC, $WILL, '5', $IAC.$DONT.'5'], |
59
|
|
|
[true, $IAC, $WONT, '6', $IAC.$DONT.'6'], |
60
|
|
|
[false, 'A'] |
61
|
|
|
]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testUndefinedCommandException() |
65
|
|
|
{ |
66
|
|
|
$socket = m::mock(Socket::class) |
67
|
|
|
->shouldReceive('read') |
68
|
|
|
->with(1) |
69
|
|
|
->andReturn('A', 'B') |
70
|
|
|
->twice() |
71
|
|
|
->getMock(); |
72
|
|
|
$interpretAsCommand = new InterpretAsCommand(); |
73
|
|
|
|
74
|
|
|
$this->setExpectedException(UndefinedCommandException::class); |
75
|
|
|
$interpretAsCommand->interpret(chr(255), $socket); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testNegotiationException() |
79
|
|
|
{ |
80
|
|
|
$this->setExpectedException(TelnetExceptionInterface::class); |
81
|
|
|
|
82
|
|
|
$socket = m::mock(Socket::class) |
83
|
|
|
->shouldReceive('read') |
84
|
|
|
->andThrow(\Exception::class) |
85
|
|
|
->getMock(); |
86
|
|
|
$iac = new InterpretAsCommand(); |
87
|
|
|
$iac->interpret(chr(255), $socket); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|