@@ 109-159 (lines=51) @@ | ||
106 | * |
|
107 | * @return void |
|
108 | */ |
|
109 | public function testExecute( |
|
110 | $command, |
|
111 | array $promptMatches, |
|
112 | $isError, |
|
113 | $promptResponse, |
|
114 | $promptGlobal = null, |
|
115 | $promptLocal = null, |
|
116 | $promptError = null, |
|
117 | $lineEnding = null |
|
118 | ) { |
|
119 | $lineEnding = $lineEnding ?: "\n"; |
|
120 | $socket = m::mock(Socket::class) |
|
121 | ->shouldReceive('write') |
|
122 | ->with($command.$lineEnding) |
|
123 | ->once(); |
|
124 | ||
125 | // echo the command back when reading each byte one-by-one from the socket |
|
126 | $commandResponse = str_split($command.$lineEnding.$promptResponse.$lineEnding); |
|
127 | $socket = $socket |
|
128 | ->shouldReceive('read') |
|
129 | ->with(1); |
|
130 | call_user_func_array([$socket, 'andReturn'], $commandResponse); |
|
131 | $socket = $socket |
|
132 | ->times(count($commandResponse)) |
|
133 | ->shouldReceive('close') |
|
134 | ->once() |
|
135 | ->getMock(); |
|
136 | ||
137 | /** @var SocketFactory $socketFactory */ |
|
138 | $socketFactory = m::mock(SocketFactory::class) |
|
139 | ->shouldReceive('createClient') |
|
140 | ->andReturn($socket) |
|
141 | ->once() |
|
142 | ->getMock(); |
|
143 | ||
144 | $promptMatcher = new PromptMatcher(); |
|
145 | ||
146 | /** @var InterpretAsCommand $interpretAsCommand */ |
|
147 | $interpretAsCommand = m::mock(InterpretAsCommand::class) |
|
148 | ->shouldReceive('interpret') |
|
149 | ->times(count($commandResponse)) |
|
150 | ->andReturn(false) |
|
151 | ->getMock(); |
|
152 | ||
153 | $telnetClient = new TelnetClient($socketFactory, $promptMatcher, $interpretAsCommand); |
|
154 | $telnetClient->connect('127.0.0.1:23', $promptGlobal, $promptError, $lineEnding); |
|
155 | ||
156 | $response = $telnetClient->execute($command, $promptLocal); |
|
157 | $this->assertInstanceOf(TelnetResponseInterface::class, $response); |
|
158 | $this->assertEquals($isError, $response->isError()); |
|
159 | $this->assertEquals($command, $response->getResponseText()); |
|
160 | $this->assertEquals($promptMatches, $response->getPromptMatches()); |
|
161 | } |
|
162 | ||
@@ 177-227 (lines=51) @@ | ||
174 | * |
|
175 | * @return void |
|
176 | */ |
|
177 | public function testExecutePromptError( |
|
178 | $command, |
|
179 | array $promptMatches, |
|
180 | $isError, |
|
181 | $promptResponse, |
|
182 | $promptGlobal = null, |
|
183 | $promptLocal = null, |
|
184 | $promptError = null, |
|
185 | $lineEnding = null |
|
186 | ) { |
|
187 | $lineEnding = $lineEnding ?: "\n"; |
|
188 | $socket = m::mock(Socket::class) |
|
189 | ->shouldReceive('write') |
|
190 | ->with($command.$lineEnding) |
|
191 | ->once(); |
|
192 | ||
193 | // echo the command back when reading each byte one-by-one from the socket |
|
194 | $commandResponse = str_split($command.$lineEnding.$promptResponse.$lineEnding); |
|
195 | $socket = $socket |
|
196 | ->shouldReceive('read') |
|
197 | ->with(1); |
|
198 | call_user_func_array([$socket, 'andReturn'], $commandResponse); |
|
199 | $socket = $socket |
|
200 | ->times(count($commandResponse)) |
|
201 | ->shouldReceive('close') |
|
202 | ->once() |
|
203 | ->getMock(); |
|
204 | ||
205 | /** @var SocketFactory $socketFactory */ |
|
206 | $socketFactory = m::mock(SocketFactory::class) |
|
207 | ->shouldReceive('createClient') |
|
208 | ->andReturn($socket) |
|
209 | ->once() |
|
210 | ->getMock(); |
|
211 | ||
212 | $promptMatcher = new PromptMatcher(); |
|
213 | ||
214 | /** @var InterpretAsCommand $interpretAsCommand */ |
|
215 | $interpretAsCommand = m::mock(InterpretAsCommand::class) |
|
216 | ->shouldReceive('interpret') |
|
217 | ->times(count($commandResponse)) |
|
218 | ->andReturn(false) |
|
219 | ->getMock(); |
|
220 | ||
221 | $telnetClient = new TelnetClient($socketFactory, $promptMatcher, $interpretAsCommand); |
|
222 | $telnetClient->connect('127.0.0.1:23', $promptGlobal, null, $lineEnding); |
|
223 | ||
224 | $response = $telnetClient->execute($command, $promptLocal, $promptError); |
|
225 | $this->assertInstanceOf(TelnetResponseInterface::class, $response); |
|
226 | $this->assertEquals($isError, $response->isError()); |
|
227 | $this->assertEquals($command, $response->getResponseText()); |
|
228 | $this->assertEquals($promptMatches, $response->getPromptMatches()); |
|
229 | } |
|
230 |