Skip to content

Commit

Permalink
Enhancement: Extract SlowTestList
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Feb 22, 2025
1 parent 33fd846 commit 5a665d5
Show file tree
Hide file tree
Showing 17 changed files with 602 additions and 148 deletions.
61 changes: 58 additions & 3 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 @@ -206,7 +216,7 @@ parameters:
path: test/Unit/DurationTest.php

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

Expand Down Expand Up @@ -260,6 +270,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 All @@ -286,15 +301,55 @@ 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 SlowTestListTest\\:\\:testCountReturnsNumberOfSlowTests\\(\\) has no return type specified\\.$#"
count: 1
path: test/Unit/SlowTestListTest.php

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

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

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

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

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

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

-
message: "#^Method SlowTestListTest\\:\\:testSortReturnsSlowTestListWhereSlowTestsAreSorted\\(\\) 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));
}
}
9 changes: 7 additions & 2 deletions src/Count.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,18 @@ private function __construct(int $value)
*/
public static function fromInt(int $value): self
{
if (0 >= $value) {
throw Exception\InvalidCount::notGreaterThanZero($value);
if (0 > $value) {
throw Exception\InvalidCount::notGreaterThanOrEqualToZero($value);
}

return new self($value);
}

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

public function toInt(): int
{
return $this->value;
Expand Down
4 changes: 2 additions & 2 deletions src/Exception/InvalidCount.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
*/
final class InvalidCount extends \InvalidArgumentException
{
public static function notGreaterThanZero(int $value): self
public static function notGreaterThanOrEqualToZero(int $value): self
{
return new self(\sprintf(
'Value should be greater than 0, but %d is not.',
'Value should be greater than or equal to 0, but %d is not.',
$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 @@ public function __construct(array $options = [])
$this->collector = new Collector\DefaultCollector();
$this->reporter = new Reporter\DefaultReporter(
new Formatter\DefaultDurationFormatter(),
$maximumDuration,
$maximumCount
);
}
Expand Down Expand Up @@ -129,13 +128,13 @@ public function endTestSuite(Framework\TestSuite $suite)
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 @@ -281,7 +280,6 @@ public function __construct(array $options = [])
$this->collector = new Collector\DefaultCollector();
$this->reporter = new Reporter\DefaultReporter(
new Formatter\DefaultDurationFormatter(),
$maximumDuration,
$maximumCount
);
}
Expand Down Expand Up @@ -342,13 +340,13 @@ public function executeAfterLastTest(): void
return;
}

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

Check warning on line 343 in src/Extension.php

View check run for this annotation

Codecov / codecov/patch

src/Extension.php#L343

Added line #L343 was not covered by tests

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

Check warning on line 345 in src/Extension.php

View check run for this annotation

Codecov / codecov/patch

src/Extension.php#L345

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

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

Check warning on line 349 in src/Extension.php

View check run for this annotation

Codecov / codecov/patch

src/Extension.php#L349

Added line #L349 was not covered by tests

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

Expand Down
Loading

0 comments on commit 5a665d5

Please sign in to comment.