Code Duplication    Length = 51-51 lines in 2 locations

tests/unit/TelnetClientTest.php 2 locations

@@ 107-157 (lines=51) @@
104
     *
105
     * @return void
106
     */
107
    public function testExecute(
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
@@ 173-223 (lines=51) @@
170
     *
171
     * @return void
172
     */
173
    public function testExecutePromptError(
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
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