Skip to content

Commit

Permalink
Adds notes
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro committed Jun 27, 2024
1 parent 655d5b6 commit fb2c422
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
19 changes: 17 additions & 2 deletions src/Adapters/Phpunit/Style.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
' <fg=gray>Tests:</> <fg=default>%s</><fg=gray> (%s assertions)</>',
' <fg=gray>Tests:</> <fg=default>%s</><fg=gray> (%s assertions%s)</>',
implode('<fg=gray>,</> ', $tests),
$result->numberOfAssertions()
$result->numberOfAssertions(),
$notesCount > 0 ? ', '.$notesCount.($notesCount > 1 ? ' notes' : ' note') : '',
),
]);
}
Expand Down Expand Up @@ -463,6 +469,15 @@ private function writeDescriptionLine(TestResult $result): void
</span>%s
</div>
HTML, $seconds === '' ? '' : 'flex space-x-1 justify-between', $truncateClasses, $result->color, $result->icon, $description, $warning, $seconds));

foreach ($result->notes as $note) {
render(sprintf(<<<'HTML'
<div class="ml-2">
<span class="text-gray"> // %s</span>
</div>
HTML, $note,
));
}
}

/**
Expand Down
25 changes: 16 additions & 9 deletions src/Adapters/Phpunit/TestResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ final class TestResult

public float $duration;

public array $notes;

public ?Throwable $throwable;

public string $warning = '';
Expand All @@ -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;
Expand All @@ -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])) {
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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());
}

/**
Expand Down

0 comments on commit fb2c422

Please sign in to comment.