/** * @dataProvider dataProviderIsMatch * * @param string $prompt * @param string $subject * @param string $lineEnding * @param bool $isMatch * @param array $matches * @param string $response * * @return void */ public function testIsMatch($prompt, $subject, $lineEnding, $isMatch, array $matches = null, $response = null) { $promptMatcher = new PromptMatcher(); $this->assertEquals($isMatch, $promptMatcher->isMatch($prompt, $subject, $lineEnding)); if (!$isMatch) { return; } $this->assertEquals($matches, $promptMatcher->getMatches()); $this->assertEquals($response, $promptMatcher->getResponseText()); }
/** * @param string $prompt * * @return \Graze\TelnetClient\TelnetResponseInterface * @throws TelnetExceptionInterface */ protected function getResponse($prompt = null) { $isError = false; $buffer = ''; do { // process one character at a time try { $character = $this->socket->read(1); } catch (Exception $e) { throw new TelnetException('failed reading from socket', 0, $e); } if (in_array($character, [$this->NULL, $this->DC1])) { break; } if ($this->interpretAsCommand->interpret($character, $this->socket)) { continue; } $buffer .= $character; // check for prompt if ($this->promptMatcher->isMatch($prompt ?: $this->prompt, $buffer, $this->lineEnding)) { break; } // check for error prompt if ($this->promptMatcher->isMatch($this->promptError, $buffer, $this->lineEnding)) { $isError = true; break; } } while (true); return new TelnetResponse($isError, $this->promptMatcher->getResponseText(), $this->promptMatcher->getMatches()); }