Output improvements.

This commit is contained in:
luke
2021-07-30 12:23:24 +01:00
parent e8595c56b3
commit 6a84b825e6
3 changed files with 192 additions and 209 deletions

View File

@ -57,7 +57,7 @@
"scripts": {
"lint": "php-cs-fixer fix -v",
"test:lint": "php-cs-fixer fix -v --dry-run",
"test:types": "phpstan analyse --ansi --memory-limit=0",
"test:types": "phpstan analyse --ansi --memory-limit=-1",
"test:unit": "php bin/pest --colors=always --exclude-group=integration",
"test:integration": "php bin/pest --colors=always --group=integration",
"update:snapshots": "REBUILD_SNAPSHOTS=true php bin/pest --colors=always",

View File

@ -38,14 +38,6 @@ final class TeamCity extends DefaultResultPrinter
/** @var \PHPUnit\Util\Log\TeamCity */
private $phpunitTeamCity;
/**
* A stack of error messages and test failures to be displayed
* once the test suite has finished running.
*
* @var array<callable>
*/
protected $outputStack = [];
/**
* @param resource|string|null $out
*/
@ -73,10 +65,55 @@ final class TeamCity extends DefaultResultPrinter
public function printResult(TestResult $result): void
{
$this->printHeader($result);
$this->printFooter($result);
}
protected function printFooter(TestResult $result): void
{
$this->writeNewLine();
$this->writeProgress('Tests: ');
$results = [
'failed' => ['count' => $result->errorCount() + $result->failureCount(), 'color' => 'fg-red'],
'skipped' => ['count' => $result->skippedCount(), 'color' => 'fg-cyan'],
'warned' => ['count' => $result->warningCount(), 'color' => 'fg-cyan'],
'risked' => ['count' => $result->riskyCount(), 'color' => 'fg-cyan'],
'incomplete' => ['count' => $result->notImplementedCount(), 'color' => 'fg-cyan'],
'passed' => ['count' => $this->successfulTestCount($result), 'color' => 'fg-green'],
];
$filteredResults = array_filter($results, function ($item): bool {
return $item['count'] > 0;
});
foreach ($filteredResults as $key => $info) {
$this->writeProgressWithColor($info['color'], $info['count'] . " $key");
if ($key !== array_reverse(array_keys($filteredResults))[0]) {
$this->write(', ');
}
}
$this->writeNewLine();
$this->write("Assertions: $this->numAssertions");
$this->writeNewLine();
$this->write("Time: {$result->time()}s");
$this->writeNewLine();
}
private function successfulTestCount(TestResult $result): int
{
return $result->count()
- $result->failureCount()
- $result->errorCount()
- $result->skippedCount()
- $result->warningCount()
- $result->notImplementedCount()
- $result->riskyCount();
}
/** @phpstan-ignore-next-line */
public function startTestSuite(TestSuite $suite): void
{
@ -115,6 +152,44 @@ final class TeamCity extends DefaultResultPrinter
]);
}
/**
* Verify that the given test suite is a valid Pest suite.
*
* @param TestSuite<Test> $suite
*/
private static function isPestTestSuite(TestSuite $suite): bool
{
return strncmp($suite->getName(), 'P\\', strlen('P\\')) === 0;
}
/**
* @param array<string, string|int> $params
*/
private function printEvent(string $eventName, array $params = []): void
{
$this->write("\n##teamcity[{$eventName}");
if ($this->flowId !== 0) {
$params['flowId'] = $this->flowId;
}
foreach ($params as $key => $value) {
$escapedValue = self::escapeValue((string) $value);
$this->write(" {$key}='{$escapedValue}'");
}
$this->write("]\n");
}
private static function escapeValue(string $text): string
{
return str_replace(
['|', "'", "\n", "\r", ']', '['],
['||', "|'", '|n', '|r', '|]', '|['],
$text
);
}
/** @phpstan-ignore-next-line */
public function endTestSuite(TestSuite $suite): void
{
@ -158,6 +233,14 @@ final class TeamCity extends DefaultResultPrinter
]);
}
public static function isPestTest(Test $test): bool
{
/** @var array<string, string> $uses */
$uses = class_uses($test);
return in_array(Testable::class, $uses, true);
}
/**
* @param Test|Testable $test
*/
@ -169,6 +252,11 @@ final class TeamCity extends DefaultResultPrinter
return;
}
$this->printEvent('testFinished', [
self::NAME => $test->getName(),
self::DURATION => self::toMilliseconds($time),
]);
if (!$this->lastTestFailed) {
$this->writePestTestOutput($test->getName(), 'fg-green, bold', '✓');
}
@ -180,11 +268,6 @@ final class TeamCity extends DefaultResultPrinter
}
$this->lastTestFailed = false;
$this->printEvent('testFinished', [
self::NAME => $test->getName(),
self::DURATION => self::toMilliseconds($time),
]);
}
private function writePestTestOutput(string $message, string $color, string $symbol, string $suffix = null): void
@ -198,6 +281,11 @@ final class TeamCity extends DefaultResultPrinter
}
}
private static function toMilliseconds(float $time): int
{
return (int) round($time * 1000);
}
/**
* @param Test|Testable $test
*/
@ -206,11 +294,7 @@ final class TeamCity extends DefaultResultPrinter
$this->lastTestFailed = true;
$this->writePestTestOutput($test->getName(), 'fg-red, bold', '');
$this->outputStack[] = function () use ($test, $t, $time): void {
$this->writeNewLine();
$this->writeWithColor('fg-red', "{$test->getPrintableTestCaseName()} > {$test->getName()}");
$this->phpunitTeamCity->addError($test, $t, $time);
};
}
public function addFailure(Test $test, AssertionFailedError $e, float $time): void
@ -218,11 +302,7 @@ final class TeamCity extends DefaultResultPrinter
$this->lastTestFailed = true;
$this->writePestTestOutput($test->getName(), 'fg-red, bold', '');
$this->outputStack[] = function () use ($test, $e, $time): void {
$this->writeNewLine();
$this->writeWithColor('fg-red', "{$test->getPrintableTestCaseName()} > {$test->getName()}");
$this->phpunitTeamCity->addFailure($test, $e, $time);
};
}
/**
@ -236,6 +316,11 @@ final class TeamCity extends DefaultResultPrinter
$this->writeWarning($test, $e);
}
private function writeWarning(Test $test, Throwable $t): void
{
$this->writePestTestOutput($test->getName(), 'fg-yellow, bold', '-', $t->getMessage());
}
public function addIncompleteTest(Test $test, Throwable $t, float $time): void
{
$this->lastTestFailed = true;
@ -252,114 +337,6 @@ final class TeamCity extends DefaultResultPrinter
{
$this->lastTestFailed = true;
$this->writeWarning($test, $t);
}
/**
* @param array<string, string|int> $params
*/
private function printEvent(string $eventName, array $params = []): void
{
$this->write("\n##teamcity[{$eventName}");
if ($this->flowId !== 0) {
$params['flowId'] = $this->flowId;
}
foreach ($params as $key => $value) {
$escapedValue = self::escapeValue((string) $value);
$this->write(" {$key}='{$escapedValue}'");
}
$this->write("]\n");
}
private function writeWarning(Test $test, Throwable $t): void
{
$this->writePestTestOutput($test->getName(), 'fg-cyan, bold', '-', $t->getMessage());
}
private function successfulTestCount(TestResult $result): int
{
return $result->count()
- $result->failureCount()
- $result->errorCount()
- $result->skippedCount()
- $result->warningCount()
- $result->notImplementedCount()
- $result->riskyCount();
}
protected function printHeader(TestResult $result): void
{
foreach ($this->outputStack as $callable) {
$callable();
}
}
protected function printFooter(TestResult $result): void
{
$this->writeNewLine();
$this->writeProgress('Tests: ');
$results = [
'failed' => ['count' => $result->errorCount() + $result->failureCount(), 'color' => 'fg-red'],
'skipped' => ['count' => $result->skippedCount(), 'color' => 'fg-cyan'],
'warned' => ['count' => $result->warningCount(), 'color' => 'fg-cyan'],
'risked' => ['count' => $result->riskyCount(), 'color' => 'fg-cyan'],
'incomplete' => ['count' => $result->notImplementedCount(), 'color' => 'fg-cyan'],
'passed' => ['count' => $this->successfulTestCount($result), 'color' => 'fg-green'],
];
$filteredResults = array_filter($results, function ($item): bool {
return $item['count'] > 0;
});
foreach ($filteredResults as $key => $info) {
$this->writeProgressWithColor($info['color'], $info['count'] . " $key");
if ($key !== array_reverse(array_keys($filteredResults))[0]) {
$this->write(', ');
}
}
$this->writeNewLine();
$this->write("Assertions: $this->numAssertions");
$this->writeNewLine();
$this->write("Time: {$result->time()}s");
$this->writeNewLine();
}
private static function escapeValue(string $text): string
{
return str_replace(
['|', "'", "\n", "\r", ']', '['],
['||', "|'", '|n', '|r', '|]', '|['],
$text
);
}
private static function toMilliseconds(float $time): int
{
return (int) round($time * 1000);
}
public static function isPestTest(Test $test): bool
{
/** @var array<string, string> $uses */
$uses = class_uses($test);
return in_array(Testable::class, $uses, true);
}
/**
* Verify that the given test suite is a valid Pest suite.
*
* @param TestSuite<Test> $suite
*/
private static function isPestTestSuite(TestSuite $suite): bool
{
return strncmp($suite->getName(), 'P\\', strlen('P\\')) === 0;
$this->phpunitTeamCity->printIgnoredTest($test->getName(), $t, $time);
}
}

View File

@ -578,6 +578,9 @@
PASS Tests\Visual\Help
✓ visual snapshot of help command output
PASS Tests\Visual\JUnit
✓ it is can successfully call all public methods
PASS Tests\Visual\SingleTestOrDirectory
✓ allows to run a single test
✓ allows to run a directory
@ -587,6 +590,9 @@
WARN Tests\Visual\Success
- visual snapshot of test suite on success
PASS Tests\Visual\TeamCity
✓ it is can successfully call all public methods
PASS Tests\Features\Depends
✓ first
✓ second
@ -601,5 +607,5 @@
✓ it is a test
✓ it uses correct parent class
Tests: 4 incompleted, 9 skipped, 381 passed
Tests: 4 incompleted, 9 skipped, 383 passed