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 8f0c291 commit e14452f
Show file tree
Hide file tree
Showing 17 changed files with 750 additions and 154 deletions.
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\\:\\:testLimitReturnsSlowTestListWhenSlowTestListHasFewerSlowTests\\(\\) has no return type specified\\.$#"
count: 1
path: test/Unit/SlowTestListTest.php

-
message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Test\\\\Unit\\\\SlowTestListTest\\:\\:testLimitReturnsSlowTestListWhenSlowTestListHasMoreSlowTests\\(\\) 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 @@ 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();

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

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

if ('' === $report) {
return;
Expand Down Expand Up @@ -277,7 +276,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 @@ -335,13 +333,13 @@ public function executeAfterLastTest(): void
return;
}

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

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

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

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

Expand Down
Loading

0 comments on commit e14452f

Please sign in to comment.