From fb2c4223df3dd1d8b5338e336aac6c7b61e0f260 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Thu, 27 Jun 2024 01:13:03 +0100 Subject: [PATCH] Adds notes --- src/Adapters/Phpunit/Style.php | 19 +++++++++++++++++-- src/Adapters/Phpunit/TestResult.php | 25 ++++++++++++++++--------- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/Adapters/Phpunit/Style.php b/src/Adapters/Phpunit/Style.php index c78ca81..0a100d8 100644 --- a/src/Adapters/Phpunit/Style.php +++ b/src/Adapters/Phpunit/Style.php @@ -251,12 +251,18 @@ public function writeRecap(State $state, Info $telemetry, PHPUnitTestResult $res $this->output->writeln(['']); + $notesCount = 0; + foreach ($state->suiteTests as $test) { + $notesCount += count($test->notes); + } + if (! empty($tests)) { $this->output->writeln([ sprintf( - ' Tests: %s (%s assertions)', + ' Tests: %s (%s assertions%s)', implode(', ', $tests), - $result->numberOfAssertions() + $result->numberOfAssertions(), + $notesCount > 0 ? ', '.$notesCount.($notesCount > 1 ? ' notes' : ' note') : '', ), ]); } @@ -463,6 +469,15 @@ private function writeDescriptionLine(TestResult $result): void %s HTML, $seconds === '' ? '' : 'flex space-x-1 justify-between', $truncateClasses, $result->color, $result->icon, $description, $warning, $seconds)); + + foreach ($result->notes as $note) { + render(sprintf(<<<'HTML' +
+ // %s +
+ HTML, $note, + )); + } } /** diff --git a/src/Adapters/Phpunit/TestResult.php b/src/Adapters/Phpunit/TestResult.php index 9a1a22c..faf4a21 100644 --- a/src/Adapters/Phpunit/TestResult.php +++ b/src/Adapters/Phpunit/TestResult.php @@ -54,6 +54,8 @@ final class TestResult public float $duration; + public array $notes; + public ?Throwable $throwable; public string $warning = ''; @@ -63,7 +65,7 @@ final class TestResult /** * Creates a new TestResult instance. */ - private function __construct(string $id, string $testCaseName, string $description, string $type, string $icon, string $compactIcon, string $color, string $compactColor, ?Throwable $throwable = null) + private function __construct(string $id, string $testCaseName, string $description, string $type, string $icon, string $compactIcon, string $color, string $compactColor, array $notes, ?Throwable $throwable = null) { $this->id = $id; $this->testCaseName = $testCaseName; @@ -73,16 +75,17 @@ private function __construct(string $id, string $testCaseName, string $descripti $this->compactIcon = $compactIcon; $this->color = $color; $this->compactColor = $compactColor; + $this->notes = $notes; $this->throwable = $throwable; $this->duration = 0.0; $asWarning = $this->type === TestResult::WARN - || $this->type === TestResult::RISKY - || $this->type === TestResult::SKIPPED - || $this->type === TestResult::DEPRECATED - || $this->type === TestResult::NOTICE - || $this->type === TestResult::INCOMPLETE; + || $this->type === TestResult::RISKY + || $this->type === TestResult::SKIPPED + || $this->type === TestResult::DEPRECATED + || $this->type === TestResult::NOTICE + || $this->type === TestResult::INCOMPLETE; if ($throwable instanceof Throwable && $asWarning) { if (in_array($this->type, [TestResult::DEPRECATED, TestResult::NOTICE])) { @@ -136,7 +139,9 @@ public static function fromTestCase(Test $test, string $type, ?Throwable $throwa $compactColor = self::makeCompactColor($type); - return new self($test->id(), $testCaseName, $description, $type, $icon, $compactIcon, $color, $compactColor, $throwable); + $notes = method_exists($test->className(), 'getPrintableTestCaseMethodNotes') ? $test->className()::getPrintableTestCaseMethodNotes() : []; + + return new self($test->id(), $testCaseName, $description, $type, $icon, $compactIcon, $color, $compactColor, $notes, $throwable); } /** @@ -168,7 +173,9 @@ public static function fromPestParallelTestCase(Test $test, string $type, ?Throw $compactColor = self::makeCompactColor($type); - return new self($test->id(), $testCaseName, $description, $type, $icon, $compactIcon, $color, $compactColor, $throwable); + $notes = method_exists($test, 'getPrintableTestCaseMethodNotes') ? $test::getPrintableTestCaseMethodNotes() : []; // @phpstan-ignore-line + + return new self($test->id(), $testCaseName, $description, $type, $icon, $compactIcon, $color, $compactColor, $notes, $throwable); } /** @@ -192,7 +199,7 @@ public static function fromBeforeFirstTestMethodErrored(BeforeFirstTestMethodErr $compactColor = self::makeCompactColor(self::FAIL); - return new self($testCaseName, $testCaseName, $description, self::FAIL, $icon, $compactIcon, $color, $compactColor, $event->throwable()); + return new self($testCaseName, $testCaseName, $description, self::FAIL, $icon, $compactIcon, $color, $compactColor, [], $event->throwable()); } /**