Skip to content

Commit

Permalink
feat: issues and prs
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro committed Jul 4, 2024
1 parent 038b99a commit 08e9d95
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 14 deletions.
42 changes: 42 additions & 0 deletions src/Adapters/Phpunit/Printers/DefaultPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ final class DefaultPrinter
*/
private static bool $profile = false;

/**
* The issues link.
*/
private static ?string $issuesLink = null;

/**
* The PRs link.
*/
private static ?string $prsLink = null;

/**
* When profiling, holds a list of slow tests.
*/
Expand Down Expand Up @@ -119,6 +129,38 @@ public static function compact(?bool $value = null): bool
return ! self::$verbose && self::$compact;
}

/**
* Links issues with the given prefix.
*/
public static function linkIssuesWith(string $linkPrefix): void
{
self::$issuesLink = $linkPrefix;
}

/**
* Get the issues link.
*/
public static function issuesLink(): ?string
{
return self::$issuesLink;
}

/**
* Links PRs with the given prefix.
*/
public static function linkPrsWith(string $linkPrefix): void
{
self::$prsLink = $linkPrefix;
}

/**
* Get the PRs link.
*/
public static function prsLink(): ?string
{
return self::$prsLink;
}

/**
* If the printer instances should profile.
*/
Expand Down
34 changes: 26 additions & 8 deletions src/Adapters/Phpunit/Style.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,18 +251,12 @@ 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%s)</>',
' <fg=gray>Tests:</> <fg=default>%s</><fg=gray> (%s assertions)</>',
implode('<fg=gray>,</> ', $tests),
$result->numberOfAssertions(),
$notesCount > 0 ? ', '.$notesCount.($notesCount > 1 ? ' notes' : ' note') : '',
),
]);
}
Expand Down Expand Up @@ -459,7 +453,31 @@ private function writeDescriptionLine(TestResult $result): void
}
}

$description = preg_replace('/`([^`]+)`/', '<span class="text-white">$1</span>', $result->description);
$description = $result->description;

$issues = [];
$prs = [];

if (($link = DefaultPrinter::issuesLink()) && count($result->issues) > 0) {
$issues = array_map(function (int $issue) use ($link): string {
return sprintf('<a href="%s">#%s</a>', sprintf($link, $issue), $issue);
}, $result->issues);
}

if (($link = DefaultPrinter::prsLink()) && count($result->prs) > 0) {
$prs = array_map(function (int $pr) use ($link): string {
return sprintf('<a href="%s">#%s</a>', sprintf($link, $pr), $pr);
}, $result->prs);
}

if (count($issues) > 0 || count($prs) > 0) {
$description .= ' '.implode(', ', array_merge(
$issues,
$prs,
));
}

$description = preg_replace('/`([^`]+)`/', '<span class="text-white">$1</span>', $description);

renderUsing($this->output);
render(sprintf(<<<'HTML'
Expand Down
18 changes: 12 additions & 6 deletions src/Adapters/Phpunit/TestResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ final class TestResult

public array $notes;

public array $issues;

public array $prs;

public ?Throwable $throwable;

public string $warning = '';
Expand All @@ -65,7 +69,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, array $notes, ?Throwable $throwable = null)
private function __construct(string $id, string $testCaseName, string $description, string $type, string $icon, string $compactIcon, string $color, string $compactColor, array $notes, array $issues, array $prs, ?Throwable $throwable = null)
{
$this->id = $id;
$this->testCaseName = $testCaseName;
Expand All @@ -76,6 +80,8 @@ private function __construct(string $id, string $testCaseName, string $descripti
$this->color = $color;
$this->compactColor = $compactColor;
$this->notes = $notes;
$this->issues = $issues;
$this->prs = $prs;
$this->throwable = $throwable;

$this->duration = 0.0;
Expand Down Expand Up @@ -140,8 +146,10 @@ public static function fromTestCase(Test $test, string $type, ?Throwable $throwa
$compactColor = self::makeCompactColor($type);

$notes = method_exists($test->className(), 'getPrintableTestCaseMethodNotes') ? $test->className()::getPrintableTestCaseMethodNotes() : [];
$issues = method_exists($test->className(), 'getPrintableTestCaseMethodIssues') ? $test->className()::getPrintableTestCaseMethodIssues() : [];
$prs = method_exists($test->className(), 'getPrintableTestCaseMethodPrs') ? $test->className()::getPrintableTestCaseMethodPrs() : [];

return new self($test->id(), $testCaseName, $description, $type, $icon, $compactIcon, $color, $compactColor, $notes, $throwable);
return new self($test->id(), $testCaseName, $description, $type, $icon, $compactIcon, $color, $compactColor, $notes, $issues, $prs, $throwable);
}

/**
Expand Down Expand Up @@ -173,9 +181,7 @@ public static function fromPestParallelTestCase(Test $test, string $type, ?Throw

$compactColor = self::makeCompactColor($type);

$notes = method_exists($test, 'getPrintableTestCaseMethodNotes') ? $test::getPrintableTestCaseMethodNotes() : []; // @phpstan-ignore-line

return new self($test->id(), $testCaseName, $description, $type, $icon, $compactIcon, $color, $compactColor, $notes, $throwable);
return new self($test->id(), $testCaseName, $description, $type, $icon, $compactIcon, $color, $compactColor, [], [], [], $throwable);
}

/**
Expand All @@ -199,7 +205,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 08e9d95

Please sign in to comment.