Completed
Push — master ( d5f107...6be6b5 )
by John
02:30
created

TelnetClientTest::testWriteException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 15
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 15
loc 15
rs 9.4285
cc 1
eloc 10
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
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
     *
23
     * @return void
24
     */
25
    public function testConnect($prompt = null, $promptError = null, $lineEnding = null)
26
    {
27
        $dsn = 'localhost:80';
28
        $socket = m::mock(Socket::class);
29
        $socketFactory = m::mock(SocketFactory::class)
30
            ->shouldReceive('createClient')
31
            ->with($dsn)
32
            ->andReturn($socket)
33
            ->once()
34
            ->getMock();
35
36
        $telnetClient = m::mock(
37
            TelnetClient::class,
38
            [$socketFactory, m::mock(PromptMatcher::class), m::mock(InterpretAsCommand::class)]
39
        )
40
            ->shouldReceive('setSocket')
41
            ->with($socket)
42
            ->once();
43
44
        if ($prompt !== null) {
45
            $telnetClient
46
                ->shouldReceive('setPrompt')
47
                ->with($prompt)
48
                ->once();
49
        } else {
50
            $telnetClient
51
                ->shouldReceive('setPrompt')
52
                ->never();
53
        }
54
55
        if ($promptError !== null) {
56
            $telnetClient
57
                ->shouldReceive('setPromptError')
58
                ->with($promptError)
59
                ->once();
60
        } else {
61
            $telnetClient
62
                ->shouldReceive('setPromptError')
63
                ->never();
64
        }
65
66
        if ($lineEnding !== null) {
67
            $telnetClient
68
                ->shouldReceive('setLineEnding')
69
                ->with($lineEnding)
70
                ->once();
71
        } else {
72
            $telnetClient
73
                ->shouldReceive('setLineEnding')
74
                ->never();
75
        }
76
77
        $telnetClient = $telnetClient->getMock()->makePartial();
78
        $telnetClient->connect($dsn, $prompt, $promptError, $lineEnding);
79
    }
80
81
    /**
82
     * @return array
83
     */
84
    public function dataProviderConnect()
85
    {
86
        return [
87
            ['PROMPT', 'PROMPTERROR', 'LINENDING'],
88
            [null, null, null]
89
        ];
90
    }
91
    /**
92
     * @dataProvider dataProviderExecute
93
     *
94
     * @param string $command
95
     * @param array $promptMatches
96
     * @param bool $isError
97
     * @param string $promptResponse
98
     * @param string $promptGlobal
99
     * @param string $promptLocal
100
     * @param string $promptError
101
     * @param string $lineEnding
102
     *
103
     * @return void
104
     */
105
    public function testExecute(
106
        $command,
107
        array $promptMatches,
108
        $isError,
109
        $promptResponse,
110
        $promptGlobal = null,
111
        $promptLocal = null,
112
        $promptError = null,
113
        $lineEnding = null
114
    ) {
115
        $lineEnding = $lineEnding ?: "\n";
116
        $socket = m::mock(Socket::class)
117
            ->shouldReceive('write')
118
            ->with($command.$lineEnding)
119
            ->once();
120
121
        // echo the command back when reading each character one-by-one from the socket
122
        $commandResponse = str_split($command.$lineEnding.$promptResponse.$lineEnding);
123
        $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...
124
            ->shouldReceive('read')
125
            ->with(1);
126
        call_user_func_array([$socket, 'andReturn'], $commandResponse);
127
        $socket = $socket
128
            ->times(count($commandResponse))
129
            ->shouldReceive('close')
130
            ->once()
131
            ->getMock();
132
133
        $socketFactory = m::mock(SocketFactory::class)
134
            ->shouldReceive('createClient')
135
            ->andReturn($socket)
136
            ->once()
137
            ->getMock();
138
139
        $promptMatcher = new PromptMatcher();
140
141
        $interpretAsCommand = m::mock(InterpretAsCommand::class)
142
            ->shouldReceive('interpret')
143
            ->times(count($commandResponse))
144
            ->andReturn(false)
145
            ->getMock();
146
147
        $telnetClient = new TelnetClient($socketFactory, $promptMatcher, $interpretAsCommand);
148
        $telnetClient->connect('127.0.0.1:23', $promptGlobal, $promptError, $lineEnding);
149
150
        $response = $telnetClient->execute($command, $promptLocal);
151
        $this->assertInstanceOf(TelnetResponseInterface::class, $response);
152
        $this->assertEquals($isError, $response->isError());
153
        $this->assertEquals($command, $response->getResponseText());
154
        $this->assertEquals($promptMatches, $response->getPromptMatches());
155
    }
156
157
    /**
158
     * @return array
159
     */
160
    public function dataProviderExecute()
161
    {
162
        return [
163
            ['party', ['$'], false, "\$"], // success
164
            ['party', ['OK'], false, 'OK', 'OK'], // success custom global prompt
165
            ['party', ['OK'], false, 'OK', null, 'OK'], // success custom local prompt
166
            ['party', ['ERROR'], true, 'ERROR'], // error
167
             // error custom prompt
168
            ['party', ['DAMN 123', 'DAMN', '123'], true, 'DAMN 123', null, null, '(DAMN) ([0-9]{3})'],
169
            ['party', ['$'], false, "\$", null, null, null, 'LOL'], // success custom line ending
170
            ['party', ['ERROR'], true, "ERROR", null, null, null, 'LOL'], // error custom line ending
171
        ];
172
    }
173
174
    public function testExecuteException()
175
    {
176
        $this->setExpectedException(TelnetExceptionInterface::class);
177
178
        $client = m::mock(TelnetClient::class)->makePartial();
179
        $client->execute('aCommand');
180
    }
181
182 View Code Duplication
    public function testWriteException()
183
    {
184
        $this->setExpectedException(TelnetExceptionInterface::class);
185
186
        $client = m::mock(TelnetClient::class)->makePartial();
187
188
        $socket = m::mock(Socket::class)
189
            ->shouldReceive('write')
190
            ->andThrow(\Exception::class)
191
            ->shouldReceive('close')
192
            ->getMock();
193
194
        $client->setSocket($socket);
195
        $client->execute('aCommand');
196
    }
197
198 View Code Duplication
    public function testReadException()
199
    {
200
        $this->setExpectedException(TelnetExceptionInterface::class);
201
202
        $client = m::mock(TelnetClient::class)->makePartial();
203
204
        $socket = m::mock(Socket::class)
205
            ->shouldReceive('write')
206
            ->shouldReceive('close')
207
            ->shouldReceive('read')
208
            ->andThrow(\Exception::class)
209
            ->getMock();
210
211
        $client->setSocket($socket);
212
        $client->execute('aCommand');
213
    }
214
215
    public function testFactory()
216
    {
217
        $client = TelnetClient::factory();
218
        $this->assertInstanceOf(TelnetClient::class, $client);
219
    }
220
}
221