InterpretAsCommandTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 5
dl 0
loc 81
rs 10
c 0
b 0
f 0

4 Methods

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

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
78
        $interpretAsCommand->interpret(chr(255), $socket);
79
    }
80
81
    public function testNegotiationException()
82
    {
83
        $this->setExpectedException(TelnetExceptionInterface::class);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
84
85
        /** @var Socket $socket */
86
        $socket = m::mock(Socket::class)
87
            ->shouldReceive('read')
88
            ->andThrow(Exception::class)
89
            ->getMock();
90
        $iac = new InterpretAsCommand();
91
        $iac->interpret(chr(255), $socket);
92
    }
93
}
94