Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace CLI testdox printer with rpkamp/fancy-testdox-printer #2920

Merged
merged 6 commits into from
Dec 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/TextUI/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
use PHPUnit\Util\Getopt;
use PHPUnit\Util\Log\TeamCity;
use PHPUnit\Util\Printer;
use PHPUnit\Util\TestDox\TextResultPrinter;
use PHPUnit\Util\TestDox\CliTestDoxPrinter;
use PHPUnit\Util\TextTestListRenderer;
use PHPUnit\Util\XmlTestListRenderer;
use ReflectionClass;
Expand Down Expand Up @@ -548,7 +548,7 @@ protected function handleArguments(array $argv): void
break;

case '--testdox':
$this->arguments['printer'] = TextResultPrinter::class;
$this->arguments['printer'] = CliTestDoxPrinter::class;

break;

Expand Down
211 changes: 211 additions & 0 deletions src/Util/TestDox/CliTestDoxPrinter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
<?php
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Util\TestDox;

use PHP_Timer;
use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\Test;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\TestResult;
use PHPUnit\Framework\Warning;
use PHPUnit\Runner\PhptTestCase;
use PHPUnit\TextUI\ResultPrinter;
use PHPUnit\Util\TestDox\TestResult as TestDoxTestResult;

/**
* This printer is for CLI output only. For the classes that output to file, html and xml,
* please refer to the PHPUnit\Util\TestDox namespace
*/
class CliTestDoxPrinter extends ResultPrinter
{
/**
* @var TestDoxTestResult
*/
private $currentTestResult;

/**
* @var TestDoxTestResult
*/
private $previousTestResult;

/**
* @var TestDoxTestResult[]
*/
private $nonSuccessfulTestResults = [];

/**
* @var NamePrettifier
*/
private $prettifier;

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

$this->prettifier = new NamePrettifier();
}

public function startTest(Test $test): void
{
if (!$test instanceof TestCase && !$test instanceof PhptTestCase) {
return;
}

$class = \get_class($test);
if ($test instanceof TestCase) {
$annotations = $test->getAnnotations();

if (isset($annotations['class']['testdox'][0])) {
$className = $annotations['class']['testdox'][0];
} else {
$className = $this->prettifier->prettifyTestClass($class);
}

if (isset($annotations['method']['testdox'][0])) {
$testMethod = $annotations['method']['testdox'][0];
} else {
$testMethod = $this->prettifier->prettifyTestMethod($test->getName(false));
}
$testMethod .= substr($test->getDataSetAsString(false), 5);
} elseif ($test instanceof PhptTestCase) {
$className = $class;
$testMethod = $test->getName();
}

$this->currentTestResult = new TestDoxTestResult(
function (string $color, string $buffer) {
return $this->formatWithColor($color, $buffer);
},
$className,
$testMethod
);

parent::startTest($test);
}

public function endTest(Test $test, $time): void
{
if (!$test instanceof TestCase && !$test instanceof PhptTestCase) {
return;
}

parent::endTest($test, $time);

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

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

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

if (!$this->currentTestResult->isTestSuccessful()) {
$this->nonSuccessfulTestResults[] = $this->currentTestResult;
}
}

public function addError(Test $test, \Throwable $t, $time): void
{
$this->currentTestResult->fail(
$this->formatWithColor('fg-yellow', '✘'),
(string) $t
);
}

public function addWarning(Test $test, Warning $e, $time): void
{
$this->currentTestResult->fail(
$this->formatWithColor('fg-yellow', '✘'),
(string) $e
);
}

public function addFailure(Test $test, AssertionFailedError $e, $time): void
{
$this->currentTestResult->fail(
$this->formatWithColor('fg-red', '✘'),
(string) $e
);
}

public function addIncompleteTest(Test $test, \Throwable $t, $time): void
{
$this->currentTestResult->fail(
$this->formatWithColor('fg-yellow', '∅'),
(string) $t,
true
);
}

public function addRiskyTest(Test $test, \Throwable $t, $time): void
{
$this->currentTestResult->fail(
$this->formatWithColor('fg-yellow', '☢'),
(string) $t,
true
);
}

public function addSkippedTest(Test $test, \Throwable $e, $time): void
{
$this->currentTestResult->fail(
$this->formatWithColor('fg-yellow', '→'),
(string) $e,
true
);
}

public function writeProgress($progress): void
{
// NOOP, block normal behavior of \PHPUnit\TextUI\ResultPrinter
}

public function flush(): void
{
}

public function printResult(TestResult $result): void
{
$this->printHeader();

$this->printNonSuccessfulTestsSummary($result->count());

$this->printFooter($result);
}

protected function printHeader(): void
{
$this->write("\n" . PHP_Timer::resourceUsage() . "\n\n");
}

public function printNonSuccessfulTestsSummary(int $numberOfExecutedTests): void
{
$numberOfNonSuccessfulTests = \count($this->nonSuccessfulTestResults);
if ($numberOfNonSuccessfulTests === 0) {
return;
}

if (($numberOfNonSuccessfulTests / $numberOfExecutedTests) >= 0.7) {
return;
}

$this->write("Summary of non-successful tests:\n\n");

$previousTestResult = null;
foreach ($this->nonSuccessfulTestResults as $testResult) {
$this->write($testResult->toString($previousTestResult, $this->verbose));
$previousTestResult = $testResult;
}
}
}
20 changes: 3 additions & 17 deletions src/Util/TestDox/ResultPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,30 +304,16 @@ public function endTest(Test $test, $time): void
return;
}

if (!isset($this->tests[$this->currentTestMethodPrettified])) {
if ($this->testStatus === BaseTestRunner::STATUS_PASSED) {
$this->tests[$this->currentTestMethodPrettified]['success'] = 1;
$this->tests[$this->currentTestMethodPrettified]['failure'] = 0;
} else {
$this->tests[$this->currentTestMethodPrettified]['success'] = 0;
$this->tests[$this->currentTestMethodPrettified]['failure'] = 1;
}
} else {
if ($this->testStatus === BaseTestRunner::STATUS_PASSED) {
$this->tests[$this->currentTestMethodPrettified]['success']++;
} else {
$this->tests[$this->currentTestMethodPrettified]['failure']++;
}
}
$this->tests[] = [$this->currentTestMethodPrettified, $this->testStatus];

$this->currentTestClassPrettified = null;
$this->currentTestMethodPrettified = null;
}

protected function doEndClass(): void
{
foreach ($this->tests as $name => $data) {
$this->onTest($name, $data['failure'] === 0);
foreach ($this->tests as $test) {
$this->onTest($test[0], $test[1] === BaseTestRunner::STATUS_PASSED);
}

$this->endClass($this->testClass);
Expand Down
Loading