Skip to content

Commit

Permalink
CliTester: option -o is repeatable and accepts file name parameter (#329
Browse files Browse the repository at this point in the history
)
  • Loading branch information
milo authored and dg committed Feb 5, 2021
1 parent 33e0a86 commit 7946609
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 27 deletions.
78 changes: 57 additions & 21 deletions src/Runner/CliTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class CliTester
/** @var bool */
private $debugMode = true;

/** @var string|null */
private $stdoutFormat;


public function run(): ?int
{
Expand All @@ -41,7 +44,7 @@ public function run(): ?int
$this->debugMode = (bool) $this->options['--debug'];
if (isset($this->options['--colors'])) {
Environment::$useColors = (bool) $this->options['--colors'];
} elseif (in_array($this->options['-o'], ['tap', 'junit'], true)) {
} elseif (in_array($this->stdoutFormat, ['tap', 'junit'], true)) {
Environment::$useColors = false;
}

Expand All @@ -67,7 +70,7 @@ public function run(): ?int
$coverageFile = $this->prepareCodeCoverage($runner);
}

if ($this->options['-o'] !== null) {
if ($this->stdoutFormat !== null) {
ob_clean();
}
ob_end_flush();
Expand All @@ -89,6 +92,8 @@ public function run(): ?int

private function loadOptions(): CommandLine
{
$outputFiles = [];

echo <<<'XX'
_____ ___ ___ _____ ___ ___
|_ _/ __)( __/_ _/ __)| _ )
Expand All @@ -110,7 +115,8 @@ private function loadOptions(): CommandLine
-s Show information about skipped tests.
--stop-on-fail Stop execution upon the first failure.
-j <num> Run <num> jobs in parallel (default: 8).
-o <console|tap|junit|none> Specify output format.
-o <console|tap|junit|none> Specify one or more output formats with optional file name.
(e.g. -o junit:output.xml)
-w | --watch <path> Watch directory.
-i | --info Show tests environment info and exit.
--setup <path> Script for runner setup.
Expand All @@ -130,6 +136,22 @@ private function loadOptions(): CommandLine
'--debug' => [],
'--cider' => [],
'--coverage-src' => [CommandLine::REALPATH => true, CommandLine::REPEATABLE => true],
'-o' => [CommandLine::REPEATABLE => true, CommandLine::NORMALIZER => function ($arg) use (&$outputFiles) {
[$format, $file] = explode(':', $arg, 2) + [1 => null];

if (isset($outputFiles[$file])) {
throw new \Exception(
$file === null
? 'Option -o <format> without file name parameter can be used only once.'
: "Cannot specify output by -o into file '$file' more then once."
);
} elseif ($file === null) {
$this->stdoutFormat = $format;
}
$outputFiles[$file] = true;

return [$format, $file];
}],
]);

if (isset($_SERVER['argv'])) {
Expand Down Expand Up @@ -167,7 +189,7 @@ private function createPhpInterpreter(): void
echo "Note: No php.ini is used.\n";
}

if (in_array($this->options['-o'], ['tap', 'junit'], true)) {
if (in_array($this->stdoutFormat, ['tap', 'junit'], true)) {
array_push($args, '-d', 'html_errors=off');
}

Expand All @@ -194,22 +216,36 @@ private function createRunner(): Runner
$runner->setTempDirectory($this->options['--temp']);
}

switch ($this->options['-o']) {
case 'none':
break;
case 'tap':
$runner->outputHandlers[] = new Output\TapPrinter;
break;
case 'junit':
$runner->outputHandlers[] = new Output\JUnitPrinter;
break;
default:
$runner->outputHandlers[] = new Output\ConsolePrinter(
$runner,
(bool) $this->options['-s'],
'php://output',
(bool) $this->options['--cider']
);
if ($this->stdoutFormat === null) {
$runner->outputHandlers[] = new Output\ConsolePrinter(
$runner,
(bool) $this->options['-s'],
'php://output',
(bool) $this->options['--cider']
);
}

foreach ($this->options['-o'] as $output) {
[$format, $file] = $output;
switch ($format) {
case 'console':
$runner->outputHandlers[] = new Output\ConsolePrinter($runner, (bool) $this->options['-s'], $file, (bool) $this->options['--cider']);
break;

case 'tap':
$runner->outputHandlers[] = new Output\TapPrinter($file);
break;

case 'junit':
$runner->outputHandlers[] = new Output\JUnitPrinter($file);
break;

case 'none':
break;

default:
throw new \LogicException("Undefined output printer '$format'.'");
}
}

if ($this->options['--log']) {
Expand Down Expand Up @@ -256,7 +292,7 @@ private function prepareCodeCoverage(Runner $runner): string

private function finishCodeCoverage(string $file): void
{
if (!in_array($this->options['-o'], ['none', 'tap', 'junit'], true)) {
if (!in_array($this->stdoutFormat, ['none', 'tap', 'junit'], true)) {
echo 'Generating code coverage report... ';
}
if (filesize($file) === 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/Runner/Output/ConsolePrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ class ConsolePrinter implements Tester\Runner\OutputHandler
public function __construct(
Runner $runner,
bool $displaySkipped = false,
string $file = 'php://output',
string $file = null,
bool $ciderMode = false
) {
$this->runner = $runner;
$this->displaySkipped = $displaySkipped;
$this->file = fopen($file, 'w');
$this->file = fopen($file ?: 'php://output', 'w');
$this->symbols = [
Test::PASSED => $ciderMode ? Dumper::color('green', '🍎') : '.',
Test::SKIPPED => 's',
Expand Down
4 changes: 2 additions & 2 deletions src/Runner/Output/JUnitPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class JUnitPrinter implements Tester\Runner\OutputHandler
private $results;


public function __construct(string $file = 'php://output')
public function __construct(string $file = null)
{
$this->file = fopen($file, 'w');
$this->file = fopen($file ?: 'php://output', 'w');
}


Expand Down
4 changes: 2 additions & 2 deletions src/Runner/Output/TapPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ class TapPrinter implements Tester\Runner\OutputHandler
private $results;


public function __construct(string $file = 'php://output')
public function __construct(string $file = null)
{
$this->file = fopen($file, 'w');
$this->file = fopen($file ?: 'php://output', 'w');
}


Expand Down

0 comments on commit 7946609

Please sign in to comment.