Simplify the function looking for output

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2020-10-27 09:11:19 +01:00
parent dd3d5829e7
commit c5d0c8ce12
No known key found for this signature in database
GPG Key ID: 7076EA9751AACDDA
1 changed files with 4 additions and 27 deletions

View File

@ -23,6 +23,8 @@
*
*/
use PHPUnit\Framework\Assert;
require __DIR__ . '/../../vendor/autoload.php';
trait CommandLine {
@ -95,25 +97,6 @@ trait CommandLine {
return $exceptions;
}
/**
* Finds all lines containing the given text
*
* @param string $input stdout or stderr output
* @param string $text text to search for
* @return array array of lines that matched
*/
public function findLines($input, $text) {
$results = [];
// the exception text usually appears after an "[Exception"] row
foreach (explode("\n", $input) as $line) {
if (strpos($line, $text) !== false) {
$results[] = $line;
}
}
return $results;
}
/**
* @Then /^the command was successful$/
*/
@ -158,19 +141,13 @@ trait CommandLine {
* @Then /^the command output contains the text "([^"]*)"$/
*/
public function theCommandOutputContainsTheText($text) {
$lines = $this->findLines($this->lastStdOut, $text);
if (empty($lines)) {
throw new \Exception('The command did not output the expected text on stdout "' . $text . '"');
}
Assert::assertContains($text, $this->lastStdOut, 'The command did not output the expected text on stdout');
}
/**
* @Then /^the command error output contains the text "([^"]*)"$/
*/
public function theCommandErrorOutputContainsTheText($text) {
$lines = $this->findLines($this->lastStdErr, $text);
if (empty($lines)) {
throw new \Exception('The command did not output the expected text on stderr "' . $text . '"');
}
Assert::assertContains($text, $this->lastStdErr, 'The command did not output the expected text on stderr');
}
}