Passed
Pull Request — master (#23)
by
unknown
01:37
created

TelnetClientTest::testExecutePromptError()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 51

Duplication

Lines 51
Ratio 100 %

Importance

Changes 0
Metric Value
dl 51
loc 51
rs 9.069
c 0
b 0
f 0
cc 2
nc 2
nop 8

How to fix   Long Method    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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
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...
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);
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...
245
246
        $client = m::mock(TelnetClient::class)->makePartial();
247
        $client->execute('aCommand');
248
    }
249
250 View Code Duplication
    public function testWriteException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
251
    {
252
        $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...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
267
    {
268
        $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...
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