Skip to content

Commit

Permalink
Adds assignees
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro committed Aug 3, 2024
1 parent 6fb1133 commit 41e50ca
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 8 deletions.
15 changes: 15 additions & 0 deletions bin/pest
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Pest\Kernel;
use Pest\Panic;
use Pest\TestCaseFilters\GitDirtyTestCaseFilter;
use Pest\TestCaseMethodFilters\AssigneeTestCaseFilter;
use Pest\TestCaseMethodFilters\IssueTestCaseFilter;
use Pest\TestCaseMethodFilters\NotesTestCaseFilter;
use Pest\TestCaseMethodFilters\PrTestCaseFilter;
Expand Down Expand Up @@ -59,6 +60,16 @@ use Symfony\Component\Console\Output\ConsoleOutput;
unset($arguments[$key]);
}

if (str_contains($value, '--assignee=')) {
unset($arguments[$key]);
} else if ($value === '--assignee') {
unset($arguments[$key]);

if (isset($arguments[$key + 1])) {
unset($arguments[$key + 1]);
}
}

if (str_contains($value, '--issue=')) {
unset($arguments[$key]);
} else if ($value === '--issue') {
Expand Down Expand Up @@ -142,6 +153,10 @@ use Symfony\Component\Console\Output\ConsoleOutput;
$testSuite->tests->addTestCaseMethodFilter(new NotesTestCaseFilter());
}

if ($assignee = $input->getParameterOption('--assignee')) {
$testSuite->tests->addTestCaseMethodFilter(new AssigneeTestCaseFilter((string) $assignee));
}

if ($issue = $input->getParameterOption('--issue')) {
$testSuite->tests->addTestCaseMethodFilter(new IssueTestCaseFilter((int) $issue));
}
Expand Down
16 changes: 13 additions & 3 deletions src/Collision/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,32 @@ public static function beforeTestMethodDescription(TestResult $result, string $d
renderUsing(self::$output);

[
'assignees' => $assignees,
'issues' => $issues,
'prs' => $prs,
] = $context;

if ((($link = Context::getInstance()->issues) !== '' && ($link = Context::getInstance()->issues) !== '0')) {
if (($link = Context::getInstance()->issues) !== '') {
$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')) {
if (($link = Context::getInstance()->prs) !== '') {
$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) {
if (($link = Context::getInstance()->assignees) !== '' && count($assignees) > 0) {
$assigneesDescription = array_map(fn (string $assignee): string => sprintf(
'<a href="%s">@%s</a>',
sprintf($link, $assignee),
$assignee,
), $assignees);
}

if (count($assignees) > 0 || count($issues) > 0 || count($prs) > 0) {
$description .= ' '.implode(', ', array_merge(
$issuesDescription ?? [],
$prsDescription ?? [],
isset($assigneesDescription) ? ['['.implode(', ', $assigneesDescription).']'] : [],
));
}

Expand Down
8 changes: 8 additions & 0 deletions src/Concerns/Testable.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ trait Testable
*/
private static string $__latestDescription;

/**
* The test's assignees.
*/
private static array $__latestAssignees = [];

/**
* The test's notes.
*/
Expand Down Expand Up @@ -105,6 +110,7 @@ public function __construct(string $name)
if ($test->hasMethod($name)) {
$method = $test->getMethod($name);
$this->__description = self::$__latestDescription = $method->description;
self::$__latestAssignees = $method->assignees;
self::$__latestNotes = $method->notes;
self::$__latestIssues = $method->issues;
self::$__latestPrs = $method->prs;
Expand Down Expand Up @@ -258,6 +264,7 @@ protected function setUp(...$arguments): void
}

$this->__description = self::$__latestDescription = $description;
self::$__latestAssignees = $method->assignees;
self::$__latestNotes = $method->notes;
self::$__latestIssues = $method->issues;
self::$__latestPrs = $method->prs;
Expand Down Expand Up @@ -449,6 +456,7 @@ public static function getLatestPrintableTestCaseMethodName(): string
public static function getPrintableContext(): array
{
return [
'assignees' => self::$__latestAssignees,
'issues' => self::$__latestIssues,
'prs' => self::$__latestPrs,
'notes' => self::$__latestNotes,
Expand Down
19 changes: 18 additions & 1 deletion src/Configuration/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
*/
final class Context
{
/**
* The assignees link.
*
* @internal
*/
public string $assignees = '';

/**
* The issues link.
*
Expand Down Expand Up @@ -44,6 +51,8 @@ public function github(string $project): self
$this->issues = "https://github.com/{$project}/issues/%s";
$this->prs = "https://github.com/{$project}/pull/%s";

$this->assignees = 'https://github.com/%s';

return $this;
}

Expand All @@ -55,6 +64,8 @@ public function gitlab(string $project): self
$this->issues = "https://gitlab.com/{$project}/issues/%s";
$this->prs = "https://gitlab.com/{$project}/merge_requests/%s";

$this->assignees = 'https://gitlab.com/%s';

return $this;
}

Expand All @@ -66,6 +77,8 @@ public function bitbucket(string $project): self
$this->issues = 'https://bitbucket.org/{$project}/issues/%s';
$this->prs = "https://bitbucket.org/{$project}/pull-requests/%s";

$this->assignees = 'https://bitbucket.org/%s';

return $this;
}

Expand All @@ -76,17 +89,21 @@ public function jira(string $namespace, string $project): self
{
$this->issues = "https://{$namespace}.atlassian.net/browse/{$project}-%s";

$this->assignees = "https://{$namespace}.atlassian.net/secure/ViewProfile?name=%s";

return $this;
}

/**
* Sets the test context to custom.
*/
public function using(string $issues, string $prs): self
public function using(string $issues, string $prs, string $assignees): self
{
$this->issues = $issues;
$this->prs = $prs;

$this->assignees = $assignees;

return $this;
}
}
7 changes: 7 additions & 0 deletions src/Factories/TestCaseMethodFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ final class TestCaseMethodFactory
*/
public array $issues = [];

/**
* The test assignees.
*
* @var array<int, string>
*/
public array $assignees = [];

/**
* The associated PRs numbers.
*
Expand Down
16 changes: 14 additions & 2 deletions src/PendingCalls/TestCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,18 @@ public function ticket(array|string|int $number): self
return $this->issue($number);
}

/**
* Sets the test assignee(s).
*/
public function assignee(array|string $assignee): self
{
$assignees = is_array($assignee) ? $assignee : [$assignee];

$this->testCaseMethod->assignees = array_unique(array_merge($this->testCaseMethod->assignees, $assignees));

return $this;
}

/**
* Associates the test with the given pull request(s).
*
Expand All @@ -401,7 +413,7 @@ public function pr(array|string|int $number): self

$number = array_map(fn (string|int $number): int => (int) ltrim((string) $number, '#'), $number);

$this->testCaseMethod->prs = array_merge($this->testCaseMethod->issues, $number);
$this->testCaseMethod->prs = array_unique(array_merge($this->testCaseMethod->issues, $number));

return $this;
}
Expand All @@ -415,7 +427,7 @@ public function note(array|string $note): self
{
$notes = is_array($note) ? $note : [$note];

$this->testCaseMethod->notes = array_merge($this->testCaseMethod->notes, $notes);
$this->testCaseMethod->notes = array_unique(array_merge($this->testCaseMethod->notes, $notes));

return $this;
}
Expand Down
27 changes: 27 additions & 0 deletions src/TestCaseMethodFilters/AssigneeTestCaseFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Pest\TestCaseMethodFilters;

use Pest\Contracts\TestCaseMethodFilter;
use Pest\Factories\TestCaseMethodFactory;

final readonly class AssigneeTestCaseFilter implements TestCaseMethodFilter
{
/**
* Create a new filter instance.
*/
public function __construct(private string $assignee)
{
//
}

/**
* Filter the test case methods.
*/
public function accept(TestCaseMethodFactory $factory): bool
{
return array_filter($factory->assignees, fn ($assignee): bool => str_starts_with($assignee, $this->assignee)) !== [];
}
}
7 changes: 6 additions & 1 deletion tests/.snapshots/success.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
✓ it does not get executed before the test
✓ it gets executed after the test

PASS Tests\Features\Assignee
✓ it may be associated with an assignee [@nunomaduro, @taylorotwell]
✓ nested → it may be associated with an assignee [@nunomaduro, @jamesbrooks, @joedixon, @taylorotwell]
// an note between an the assignee

PASS Tests\Features\BeforeAll
✓ it gets executed before tests
✓ it do not get executed before each test
Expand Down Expand Up @@ -1537,4 +1542,4 @@
WARN Tests\Visual\Version
- visual snapshot of help command output

Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 13 todos, 24 skipped, 1076 passed (2628 assertions)
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 13 todos, 24 skipped, 1078 passed (2632 assertions)
15 changes: 15 additions & 0 deletions tests/Features/Assignee.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

beforeEach(function () {
expect(true)->toBeTrue();
})->assignee('nunomaduro');

it('may be associated with an assignee', function () {
expect(true)->toBeTrue();
})->assignee('taylorotwell');

describe('nested', function () {
it('may be associated with an assignee', function () {
expect(true)->toBeTrue();
})->assignee('taylorotwell');
})->assignee('nunomaduro')->note('an note between an the assignee')->assignee(['jamesbrooks', 'joedixon']);
2 changes: 1 addition & 1 deletion tests/Visual/Parallel.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

test('parallel', function () use ($run) {
expect($run('--exclude-group=integration'))
->toContain('Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 13 todos, 19 skipped, 1062 passed (2596 assertions)')
->toContain('Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 13 todos, 19 skipped, 1064 passed (2600 assertions)')
->toContain('Parallel: 3 processes');
})->skipOnWindows();

Expand Down

0 comments on commit 41e50ca

Please sign in to comment.