Skip to content

Commit

Permalink
Add draft functionality to buffer TestDox output after reordering
Browse files Browse the repository at this point in the history
  • Loading branch information
epdenouden authored and sebastianbergmann committed Nov 26, 2018
1 parent d634df7 commit 705e3aa
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 5 deletions.
6 changes: 2 additions & 4 deletions src/Runner/TestSuiteSorter.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,10 @@ private function calculateTestExecutionOrder(Test $suite): array
foreach ($suite->tests() as $test) {
if (!($test instanceof TestSuite)) {
$tests[] = $this->getNormalizedTestName($test);
} else {
$tests = \array_merge($tests, $this->calculateTestExecutionOrder($test));
}
}

foreach ($suite as $_suite) {
$tests = \array_merge($tests, $this->calculateTestExecutionOrder($_suite));
}
}

return $tests;
Expand Down
7 changes: 7 additions & 0 deletions src/TextUI/TestRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use PHPUnit\Util\Log\JUnit;
use PHPUnit\Util\Log\TeamCity;
use PHPUnit\Util\Printer;
use PHPUnit\Util\TestDox\CliTestDoxPrinter;
use PHPUnit\Util\TestDox\HtmlResultPrinter;
use PHPUnit\Util\TestDox\TextResultPrinter;
use PHPUnit\Util\TestDox\XmlResultPrinter;
Expand Down Expand Up @@ -203,6 +204,7 @@ public function doRun(Test $suite, array $arguments = [], bool $exit = true): Te
$sorter = new TestSuiteSorter($cache);

$sorter->reorderTestsInSuite($suite, $arguments['executionOrder'], $arguments['resolveDependencies'], $arguments['executionOrderDefects']);
$originalExecutionOrder = $sorter->getOriginalExecutionOrder();

unset($sorter);
}
Expand Down Expand Up @@ -309,6 +311,11 @@ public function doRun(Test $suite, array $arguments = [], bool $exit = true): Te
$arguments['columns'],
$arguments['reverseList']
);

if (isset($originalExecutionOrder) && ($this->printer instanceof CliTestDoxPrinter)) {
/* @var CliTestDoxPrinter */
$this->printer->setOriginalExecutionOrder($originalExecutionOrder);
}
}
}

Expand Down
75 changes: 74 additions & 1 deletion src/Util/TestDox/CliTestDoxPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,44 @@ class CliTestDoxPrinter extends ResultPrinter
*/
private $prettifier;

/**
* @var int The number of test results received from the TestRunner
*/
private $testCount = 0;

/**
* @var int The number of test results already sent to the output
*/
private $testFlushCount = 0;

/**
* @var array Buffer for write()
*/
private $outputBuffer = [];

/**
* @var bool
*/
private $bufferExecutionOrder = false;

/**
* @var array array<string>
*/
private $originalExecutionOrder = [];

public function __construct($out = null, bool $verbose = false, $colors = self::COLOR_DEFAULT, bool $debug = false, $numberOfColumns = 80, bool $reverse = false)
{
parent::__construct($out, $verbose, $colors, $debug, $numberOfColumns, $reverse);

$this->prettifier = new NamePrettifier;
}

public function setOriginalExecutionOrder(array $order): void
{
$this->originalExecutionOrder = $order;
$this->bufferExecutionOrder = !empty($order);
}

public function startTest(Test $test): void
{
if (!$test instanceof TestCase && !$test instanceof PhptTestCase && !$test instanceof TestSuite) {
Expand Down Expand Up @@ -96,7 +127,20 @@ public function endTest(Test $test, float $time): void

$this->currentTestResult->setRuntime($time);

$this->write($this->currentTestResult->toString($this->previousTestResult, $this->verbose));
$testName = '';
if ($test instanceof PhptTestCase) {
$testName = $test->getName();
$this->testCount++;
} elseif ($test instanceof TestCase) {
$testName = $test->getName(true);

if (\strpos($testName, '::') === false) {
$testName = \get_class($test) . '::' . $testName;
}
$this->testCount++;
}

$this->writeOriginalExecutionOrder($testName, $this->currentTestResult->toString($this->previousTestResult, $this->verbose));

$this->previousTestResult = $this->currentTestResult;

Expand Down Expand Up @@ -156,6 +200,35 @@ public function addSkippedTest(Test $test, \Throwable $t, float $time): void
);
}

public function writeOriginalExecutionOrder(string $testname, string $msg): void
{
if (!$this->bufferExecutionOrder) {
$this->write($msg);

return;
}

if ($testname == $this->originalExecutionOrder[$this->testFlushCount]) {
$this->write($msg);
$this->testFlushCount++;

while (isset($this->outputBuffer[$this->originalExecutionOrder[$this->testFlushCount]])) {
// $this->write("** flushing {$this->originalExecutionOrder[$this->testFlushCount]}\n");
foreach($this->outputBuffer[$this->originalExecutionOrder[$this->testFlushCount++]] as $line) {
$this->write($line);
}
}
} else {
// parent::write("** buffering $testname\n");
$this->outputBuffer[$testname][] = $msg;
}
}

public function write(string $msg): void
{
parent::write($msg);
}

public function writeProgress(string $progress): void
{
}
Expand Down

0 comments on commit 705e3aa

Please sign in to comment.