Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Extract SlowTestList #658

Merged
merged 1 commit into from
Feb 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 82 additions & 2 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ parameters:
count: 1
path: test/Unit/Console/ColorTest.php

-
message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Test\\\\Unit\\\\CountTest\\:\\:testEqualsReturnsFalseWhenValueIsDifferent\\(\\) has no return type specified\\.$#"
count: 1
path: test/Unit/CountTest.php

-
message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Test\\\\Unit\\\\CountTest\\:\\:testEqualsReturnsTrueWhenValueIsSame\\(\\) has no return type specified\\.$#"
count: 1
path: test/Unit/CountTest.php

-
message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Test\\\\Unit\\\\CountTest\\:\\:testFromIntRejectsInvalidValue\\(\\) has no return type specified\\.$#"
count: 1
Expand Down Expand Up @@ -265,6 +275,11 @@ parameters:
count: 1
path: test/Unit/Exception/PhaseNotStartedTest.php

-
message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Test\\\\Unit\\\\Exception\\\\SlowTestListIsEmptyTest\\:\\:testFromPhaseIdentifierReturnsException\\(\\) has no return type specified\\.$#"
count: 1
path: test/Unit/Exception/SlowTestListIsEmptyTest.php

-
message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Test\\\\Unit\\\\Formatter\\\\DefaultDurationFormatterTest\\:\\:testFormatFormats\\(\\) has no return type specified\\.$#"
count: 1
Expand Down Expand Up @@ -316,15 +331,80 @@ parameters:
path: test/Unit/PhaseTest.php

-
message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Test\\\\Unit\\\\Reporter\\\\DefaultReporterTest\\:\\:testReportReturnsEmptyStringWhenThereAreNoSlowTests\\(\\) has no return type specified\\.$#"
message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Test\\\\Unit\\\\Reporter\\\\DefaultReporterTest\\:\\:testReportReturnsEmptyStringWhenSlowTestListIsEmpty\\(\\) has no return type specified\\.$#"
count: 1
path: test/Unit/Reporter/DefaultReporterTest.php

-
message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Test\\\\Unit\\\\Reporter\\\\DefaultReporterTest\\:\\:testReportReturnsReportWhenThereAreFewerSlowTestsThanMaximumCount\\(\\) has no return type specified\\.$#"
message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Test\\\\Unit\\\\Reporter\\\\DefaultReporterTest\\:\\:testReportReturnsReportWhenSlowTestListHasFewerSlowTestsThanMaximumCount\\(\\) has no return type specified\\.$#"
count: 1
path: test/Unit/Reporter/DefaultReporterTest.php

-
message: "#^Method SlowTestCountTest\\:\\:testEqualsReturnsFalseWhenValueIsDifferent\\(\\) has no return type specified\\.$#"
count: 1
path: test/Unit/SlowTestCountTest.php

-
message: "#^Method SlowTestCountTest\\:\\:testEqualsReturnsFalseWhenValueIsSame\\(\\) has no return type specified\\.$#"
count: 1
path: test/Unit/SlowTestCountTest.php

-
message: "#^Method SlowTestCountTest\\:\\:testFromCountReturnsMaximumCount\\(\\) has no return type specified\\.$#"
count: 1
path: test/Unit/SlowTestCountTest.php

-
message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Test\\\\Unit\\\\SlowTestListTest\\:\\:testCreateReturnsSlowTestList\\(\\) has no return type specified\\.$#"
count: 1
path: test/Unit/SlowTestListTest.php

-
message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Test\\\\Unit\\\\SlowTestListTest\\:\\:testFirstReturnsFirstSlowTestWhenSlowTestListIsNotEmpty\\(\\) has no return type specified\\.$#"
count: 1
path: test/Unit/SlowTestListTest.php

-
message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Test\\\\Unit\\\\SlowTestListTest\\:\\:testFirstThrowsExceptionWhenSlowTestListIsEmpty\\(\\) has no return type specified\\.$#"
count: 1
path: test/Unit/SlowTestListTest.php

-
message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Test\\\\Unit\\\\SlowTestListTest\\:\\:testIsEmptyReturnsFalseWhenSlowTestListIsNotEmpty\\(\\) has no return type specified\\.$#"
count: 1
path: test/Unit/SlowTestListTest.php

-
message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Test\\\\Unit\\\\SlowTestListTest\\:\\:testIsEmptyReturnsTrueWhenSlowTestListIsEmpty\\(\\) has no return type specified\\.$#"
count: 1
path: test/Unit/SlowTestListTest.php

-
message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Test\\\\Unit\\\\SlowTestListTest\\:\\:testLimitToReturnsSlowTestListLimitedToMaximumCountWhenSlowTestListHasFewerSlowTests\\(\\) has no return type specified\\.$#"
count: 1
path: test/Unit/SlowTestListTest.php

-
message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Test\\\\Unit\\\\SlowTestListTest\\:\\:testLimitToReturnsSlowTestListLimitedToMaximumCountWhenSlowTestListHasMoreSlowTests\\(\\) has no return type specified\\.$#"
count: 1
path: test/Unit/SlowTestListTest.php

-
message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Test\\\\Unit\\\\SlowTestListTest\\:\\:testSlowTestCountReturnsCountOfSlowTests\\(\\) has no return type specified\\.$#"
count: 1
path: test/Unit/SlowTestListTest.php

-
message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Test\\\\Unit\\\\SlowTestListTest\\:\\:testSortByMaximumDurationDescendingReturnsSlowTestListWhereSlowTestsAreSortedByMaximumDurationDescending\\(\\) has no return type specified\\.$#"
count: 1
path: test/Unit/SlowTestListTest.php

-
message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Test\\\\Unit\\\\SlowTestListTest\\:\\:testSortByTestDurationDescendingReturnsSlowTestListWhereSlowTestsAreSortedByTestDurationDescending\\(\\) has no return type specified\\.$#"
count: 1
path: test/Unit/SlowTestListTest.php

-
message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Test\\\\Unit\\\\SlowTestTest\\:\\:testCreateReturnsSlowTest\\(\\) has no return type specified\\.$#"
count: 1
Expand Down
8 changes: 2 additions & 6 deletions src/Collector/Collector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Ergebnis\PHPUnit\SlowTestDetector\Collector;

use Ergebnis\PHPUnit\SlowTestDetector\SlowTest;
use Ergebnis\PHPUnit\SlowTestDetector\SlowTestList;

/**
* @internal
Expand All @@ -22,10 +23,5 @@ interface Collector
{
public function collectSlowTest(SlowTest $slowTest);

/**
* @phpstan-return list<SlowTest>
*
* @return list<SlowTest>
*/
public function collected(): array;
public function slowTestList(): SlowTestList;
}
10 changes: 3 additions & 7 deletions src/Collector/DefaultCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Ergebnis\PHPUnit\SlowTestDetector\Collector;

use Ergebnis\PHPUnit\SlowTestDetector\SlowTest;
use Ergebnis\PHPUnit\SlowTestDetector\SlowTestList;

/**
* @internal
Expand Down Expand Up @@ -44,13 +45,8 @@ public function collectSlowTest(SlowTest $slowTest)
$this->slowTests[$key] = $slowTest;
}

/**
* @phpstan-return list<SlowTest>
*
* @return list<SlowTest>
*/
public function collected(): array
public function slowTestList(): SlowTestList
{
return \array_values($this->slowTests);
return SlowTestList::create(...\array_values($this->slowTests));
}
}
5 changes: 5 additions & 0 deletions src/Count.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public static function fromInt(int $value): self
return new self($value);
}

public function equals(self $other): bool
{
return $this->value === $other->value;
}

public function toInt(): int
{
return $this->value;
Expand Down
25 changes: 25 additions & 0 deletions src/Exception/SlowTestListIsEmpty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2021-2025 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/phpunit-slow-test-detector
*/

namespace Ergebnis\PHPUnit\SlowTestDetector\Exception;

/**
* @internal
*/
final class SlowTestListIsEmpty extends \RuntimeException
{
public static function create(): self
{
return new self('Slow test list is empty.');
}
}
15 changes: 6 additions & 9 deletions src/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
$this->collector = new Collector\DefaultCollector();
$this->reporter = new Reporter\DefaultReporter(
new Formatter\DefaultDurationFormatter(),
$maximumDuration,
$maximumCount
);
}
Expand Down Expand Up @@ -129,13 +128,13 @@
return;
}

$slowTests = $this->collector->collected();
$slowTestList = $this->collector->slowTestList();

Check warning on line 131 in src/Extension.php

View check run for this annotation

Codecov / codecov/patch

src/Extension.php#L131

Added line #L131 was not covered by tests

if ([] === $slowTests) {
if ($slowTestList->isEmpty()) {

Check warning on line 133 in src/Extension.php

View check run for this annotation

Codecov / codecov/patch

src/Extension.php#L133

Added line #L133 was not covered by tests
return;
}

$report = $this->reporter->report(...$slowTests);
$report = $this->reporter->report($slowTestList);

Check warning on line 137 in src/Extension.php

View check run for this annotation

Codecov / codecov/patch

src/Extension.php#L137

Added line #L137 was not covered by tests

if ('' === $report) {
return;
Expand Down Expand Up @@ -277,7 +276,6 @@
$this->collector = new Collector\DefaultCollector();
$this->reporter = new Reporter\DefaultReporter(
new Formatter\DefaultDurationFormatter(),
$maximumDuration,
$maximumCount
);
}
Expand Down Expand Up @@ -335,13 +333,13 @@
return;
}

$slowTests = $this->collector->collected();
$slowTestList = $this->collector->slowTestList();

Check warning on line 336 in src/Extension.php

View check run for this annotation

Codecov / codecov/patch

src/Extension.php#L336

Added line #L336 was not covered by tests

if ([] === $slowTests) {
if ($slowTestList->isEmpty()) {

Check warning on line 338 in src/Extension.php

View check run for this annotation

Codecov / codecov/patch

src/Extension.php#L338

Added line #L338 was not covered by tests
return;
}

$report = $this->reporter->report(...$slowTests);
$report = $this->reporter->report($slowTestList);

Check warning on line 342 in src/Extension.php

View check run for this annotation

Codecov / codecov/patch

src/Extension.php#L342

Added line #L342 was not covered by tests

if ('' === $report) {
return;
Expand Down Expand Up @@ -431,7 +429,6 @@
$collector = new Collector\DefaultCollector();
$reporter = new Reporter\DefaultReporter(
new Formatter\DefaultDurationFormatter(),
$maximumDuration,
$maximumCount
);

Expand Down
Loading
Loading