Adjusts for Collision 8.4

This commit is contained in:
Nuno Maduro
2024-08-03 16:36:01 +01:00
parent 63ba117b33
commit 6fb1133d52
6 changed files with 139 additions and 33 deletions

View File

@ -19,7 +19,7 @@
"require": {
"php": "^8.2.0",
"brianium/paratest": "^7.5.0",
"nunomaduro/collision": "^8.3.0",
"nunomaduro/collision": "^8.4.0",
"nunomaduro/termwind": "^2.0.1",
"pestphp/pest-plugin": "^3.0.0",
"pestphp/pest-plugin-arch": "^3.0.0",

90
src/Collision/Events.php Normal file
View File

@ -0,0 +1,90 @@
<?php
declare(strict_types=1);
namespace Pest\Collision;
use NunoMaduro\Collision\Adapters\Phpunit\TestResult;
use Pest\Configuration\Context;
use Symfony\Component\Console\Output\OutputInterface;
use function Termwind\render;
use function Termwind\renderUsing;
/**
* @internal
*/
final class Events
{
/**
* Sets the output.
*/
private static ?OutputInterface $output = null;
/**
* Sets the output.
*/
public static function setOutput(OutputInterface $output): void
{
self::$output = $output;
}
/**
* Fires before the test method description is printed.
*/
public static function beforeTestMethodDescription(TestResult $result, string $description): string
{
if (($context = $result->context) === []) {
return $description;
}
renderUsing(self::$output);
[
'issues' => $issues,
'prs' => $prs,
] = $context;
if ((($link = Context::getInstance()->issues) !== '' && ($link = Context::getInstance()->issues) !== '0')) {
$issuesDescription = array_map(fn (int $issue): string => sprintf('<a href="%s">#%s</a>', sprintf($link, $issue), $issue), $issues);
}
if ((($link = Context::getInstance()->prs) !== '' && ($link = Context::getInstance()->prs) !== '0')) {
$prsDescription = array_map(fn (int $pr): string => sprintf('<a href="%s">#%s</a>', sprintf($link, $pr), $pr), $prs);
}
if (count($issues) > 0 || count($prs) > 0) {
$description .= ' '.implode(', ', array_merge(
$issuesDescription ?? [],
$prsDescription ?? [],
));
}
return $description;
}
/**
* Fires after the test method description is printed.
*/
public static function afterTestMethodDescription(TestResult $result): void
{
if (($context = $result->context) === []) {
return;
}
renderUsing(self::$output);
[
'notes' => $notes,
] = $context;
foreach ($notes as $note) {
render(sprintf(<<<'HTML'
<div class="ml-2">
<span class="text-gray"> // %s</span>
</div>
HTML, $note,
));
}
}
}

View File

@ -444,26 +444,14 @@ trait Testable
}
/**
* The latest printable test case notes.
* The printable test case method context.
*/
public static function getPrintableTestCaseMethodNotes(): array
public static function getPrintableContext(): array
{
return self::$__latestNotes;
}
/**
* The latest printable test case issues.
*/
public static function getPrintableTestCaseMethodIssues(): array
{
return self::$__latestIssues;
}
/**
* The latest printable test case PRs.
*/
public static function getPrintableTestCaseMethodPrs(): array
{
return self::$__latestPrs;
return [
'issues' => self::$__latestIssues,
'prs' => self::$__latestPrs,
'notes' => self::$__latestNotes,
];
}
}

View File

@ -73,6 +73,6 @@ final class Configuration
*/
public function context(): Configuration\Context
{
return new Configuration\Context;
return Configuration\Context::getInstance();
}
}

View File

@ -4,20 +4,45 @@ declare(strict_types=1);
namespace Pest\Configuration;
use NunoMaduro\Collision\Adapters\Phpunit\Printers\DefaultPrinter;
/**
* @internal
*/
final readonly class Context
final class Context
{
/**
* The issues link.
*
* @internal
*/
public string $issues = '';
/**
* The PRs link.
*
* @internal
*/
public string $prs = '';
/**
* The singleton instance.
*/
private static ?self $instance = null;
/**
* Creates a new instance of the context.
*/
public static function getInstance(): self
{
return self::$instance ??= new self;
}
/**
* Sets the test context to GitHub.
*/
public function github(string $project): self
{
DefaultPrinter::linkIssuesWith("https://github.com/{$project}/issues/%s");
DefaultPrinter::linkPrsWith("https://github.com/{$project}/pull/%s");
$this->issues = "https://github.com/{$project}/issues/%s";
$this->prs = "https://github.com/{$project}/pull/%s";
return $this;
}
@ -27,8 +52,8 @@ final readonly class Context
*/
public function gitlab(string $project): self
{
DefaultPrinter::linkIssuesWith("https://gitlab.com/{$project}/issues/%s");
DefaultPrinter::linkPrsWith("https://gitlab.com/{$project}/merge_requests/%s");
$this->issues = "https://gitlab.com/{$project}/issues/%s";
$this->prs = "https://gitlab.com/{$project}/merge_requests/%s";
return $this;
}
@ -38,8 +63,8 @@ final readonly class Context
*/
public function bitbucket(string $project): self
{
DefaultPrinter::linkIssuesWith('https://bitbucket.org/{$project}/issues/%s');
DefaultPrinter::linkPrsWith("https://bitbucket.org/{$project}/pull-requests/%s");
$this->issues = 'https://bitbucket.org/{$project}/issues/%s';
$this->prs = "https://bitbucket.org/{$project}/pull-requests/%s";
return $this;
}
@ -49,7 +74,7 @@ final readonly class Context
*/
public function jira(string $namespace, string $project): self
{
DefaultPrinter::linkIssuesWith("https://{$namespace}.atlassian.net/browse/{$project}-%s");
$this->issues = "https://{$namespace}.atlassian.net/browse/{$project}-%s";
return $this;
}
@ -59,8 +84,8 @@ final readonly class Context
*/
public function using(string $issues, string $prs): self
{
DefaultPrinter::linkIssuesWith($issues);
DefaultPrinter::linkPrsWith($prs);
$this->issues = $issues;
$this->prs = $prs;
return $this;
}

View File

@ -9,6 +9,9 @@ use Pest\Factories\TestCaseMethodFactory;
final readonly class NotesTestCaseFilter implements TestCaseMethodFilter
{
/**
* Filter the test case methods.
*/
public function accept(TestCaseMethodFactory $factory): bool
{
return $factory->notes !== [];