Completed
Push — master ( 41a3d7...798a98 )
by John
04:27
created

TelnetClientTest::dataProviderConnect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
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
13
class TelnetClientTest extends \PHPUnit_Framework_TestCase
14
{
15
    /**
16
     * @dataProvider dataProviderConnect
17
     *
18
     * @param string $prompt
19
     * @param string $promptError
20
     * @param string $lineEnding
21
     *
22
     * @return void
23
     */
24
    public function testConnect($prompt = null, $promptError = null, $lineEnding = null)
25
    {
26
        $dsn = 'localhost:80';
27
        $socket = m::mock(Socket::class);
28
        $socketFactory = m::mock(SocketFactory::class)
29
            ->shouldReceive('createClient')
30
            ->with($dsn)
31
            ->andReturn($socket)
32
            ->once()
33
            ->getMock();
34
35
        $telnetClient = m::mock(
36
            TelnetClient::class,
37
            [$socketFactory, m::mock(PromptMatcher::class), m::mock(InterpretAsCommand::class)]
38
        )
39
            ->shouldReceive('setSocket')
40
            ->with($socket)
41
            ->once();
42
43
        if ($prompt !== null) {
44
            $telnetClient
45
                ->shouldReceive('setPrompt')
46
                ->with($prompt)
47
                ->once();
48
        } else {
49
            $telnetClient
50
                ->shouldReceive('setPrompt')
51
                ->never();
52
        }
53
54
        if ($promptError !== null) {
55
            $telnetClient
56
                ->shouldReceive('setPromptError')
57
                ->with($promptError)
58
                ->once();
59
        } else {
60
            $telnetClient
61
                ->shouldReceive('setPromptError')
62
                ->never();
63
        }
64
65
        if ($lineEnding !== null) {
66
            $telnetClient
67
                ->shouldReceive('setLineEnding')
68
                ->with($lineEnding)
69
                ->once();
70
        } else {
71
            $telnetClient
72
                ->shouldReceive('setLineEnding')
73
                ->never();
74
        }
75
76
        $telnetClient = $telnetClient->getMock()->makePartial();
77
        $telnetClient->connect($dsn, $prompt, $promptError, $lineEnding);
78
    }
79
80
    /**
81
     * @return array
82
     */
83
    public function dataProviderConnect()
84
    {
85
        return [
86
            ['PROMPT', 'PROMPTERROR', 'LINENDING'],
87
            [null, null, null]
88
        ];
89
    }
90
    /**
91
     * @dataProvider dataProviderExecute
92
     *
93
     * @param string $command
94
     * @param array $promptMatches
95
     * @param bool $isError
96
     * @param string $promptResponse
97
     * @param string $promptGlobal
98
     * @param string $promptLocal
99
     * @param string $promptError
100
     * @param string $lineEnding
101
     *
102
     * @return void
103
     */
104
    public function testExecute(
105
        $command,
106
        array $promptMatches,
107
        $isError,
108
        $promptResponse,
109
        $promptGlobal = null,
110
        $promptLocal = null,
111
        $promptError = null,
112
        $lineEnding = null
113
    ) {
114
        $lineEnding = $lineEnding ?: "\n";
115
        $socket = m::mock(Socket::class)
116
            ->shouldReceive('write')
117
            ->with($command.$lineEnding)
118
            ->once();
119
120
        // echo the command back when reading each character one-by-one from the socket
121
        $commandResponse = str_split($command.$lineEnding.$promptResponse.$lineEnding);
122
        $socket = $socket
0 ignored issues
show
Bug introduced by
The method shouldReceive() does not seem to exist on object<Mockery\Expectation>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
123
            ->shouldReceive('read')
124
            ->with(1);
125
        call_user_func_array([$socket, 'andReturn'], $commandResponse);
126
        $socket = $socket
127
            ->times(count($commandResponse))
128
            ->shouldReceive('close')
129
            ->once()
130
            ->getMock();
131
132
        $socketFactory = m::mock(SocketFactory::class)
133
            ->shouldReceive('createClient')
134
            ->andReturn($socket)
135
            ->once()
136
            ->getMock();
137
138
        $promptMatcher = new PromptMatcher();
139
140
        $interpretAsCommand = m::mock(InterpretAsCommand::class)
141
            ->shouldReceive('interpret')
142
            ->times(count($commandResponse))
143
            ->andReturn(false)
144
            ->getMock();
145
146
        $telnetClient = new TelnetClient($socketFactory, $promptMatcher, $interpretAsCommand);
147
        $telnetClient->connect('127.0.0.1:23', $promptGlobal, $promptError, $lineEnding);
148
149
        $response = $telnetClient->execute($command, $promptLocal);
150
        $this->assertInstanceOf(TelnetResponseInterface::class, $response);
151
        $this->assertEquals($isError, $response->isError());
152
        $this->assertEquals($command, $response->getResponseText());
153
        $this->assertEquals($promptMatches, $response->getPromptMatches());
154
    }
155
156
    /**
157
     * @return array
158
     */
159
    public function dataProviderExecute()
160
    {
161
        return [
162
            ['party', ['$'], false, "\$"], // success
163
            ['party', ['OK'], false, 'OK', 'OK'], // success custom global prompt
164
            ['party', ['OK'], false, 'OK', null, 'OK'], // success custom local prompt
165
            ['party', ['ERROR'], true, 'ERROR'], // error
166
             // error custom prompt
167
            ['party', ['DAMN 123', 'DAMN', '123'], true, 'DAMN 123', null, null, '(DAMN) ([0-9]{3})'],
168
            ['party', ['$'], false, "\$", null, null, null, 'LOL'], // success custom line ending
169
            ['party', ['ERROR'], true, "ERROR", null, null, null, 'LOL'], // error custom line ending
170
        ];
171
    }
172
}
173