diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 2da34651..de3f39b1 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -210,6 +210,11 @@ parameters: count: 1 path: test/Unit/Exception/InvalidCountTest.php + - + message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Test\\\\Unit\\\\Exception\\\\InvalidMaximumCountTest\\:\\:testNotGreaterThanZeroReturnsException\\(\\) has no return type specified\\.$#" + count: 1 + path: test/Unit/Exception/InvalidMaximumCountTest.php + - message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Test\\\\Unit\\\\Exception\\\\InvalidMillisecondsTest\\:\\:testNotGreaterThanZeroReturnsException\\(\\) has no return type specified\\.$#" count: 1 @@ -265,6 +270,21 @@ parameters: count: 1 path: test/Unit/Formatter/DefaultDurationFormatterTest.php + - + message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Test\\\\Unit\\\\MaximumCountTest\\:\\:testDefaultReturnsMaximumCount\\(\\) has no return type specified\\.$#" + count: 1 + path: test/Unit/MaximumCountTest.php + + - + message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Test\\\\Unit\\\\MaximumCountTest\\:\\:testFromCountRejectsInvalidCount\\(\\) has no return type specified\\.$#" + count: 1 + path: test/Unit/MaximumCountTest.php + + - + message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Test\\\\Unit\\\\MaximumCountTest\\:\\:testFromCountReturnsMaximumCount\\(\\) has no return type specified\\.$#" + count: 1 + path: test/Unit/MaximumCountTest.php + - message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Test\\\\Unit\\\\MaximumDurationTest\\:\\:testDefaultReturnsMaximumDuration\\(\\) has no return type specified\\.$#" count: 1 diff --git a/src/Count.php b/src/Count.php index 3c969771..084e1eaa 100644 --- a/src/Count.php +++ b/src/Count.php @@ -33,8 +33,8 @@ 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); diff --git a/src/Exception/InvalidCount.php b/src/Exception/InvalidCount.php index fd8e746c..3ef651d9 100644 --- a/src/Exception/InvalidCount.php +++ b/src/Exception/InvalidCount.php @@ -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 )); } diff --git a/src/Exception/InvalidMaximumCount.php b/src/Exception/InvalidMaximumCount.php new file mode 100644 index 00000000..4189b1f5 --- /dev/null +++ b/src/Exception/InvalidMaximumCount.php @@ -0,0 +1,28 @@ +has('maximum-count')) { - $maximumCount = Count::fromInt((int) $parameters->get('maximum-count')); + $maximumCount = MaximumCount::fromCount(Count::fromInt((int) $parameters->get('maximum-count'))); } $maximumDuration = MaximumDuration::default(); diff --git a/src/MaximumCount.php b/src/MaximumCount.php new file mode 100644 index 00000000..9138882b --- /dev/null +++ b/src/MaximumCount.php @@ -0,0 +1,49 @@ +count = $count; + } + + /** + * @throws Exception\InvalidMaximumCount + */ + public static function fromCount(Count $count): self + { + if ($count->toInt() <= 0) { + throw Exception\InvalidMaximumCount::notGreaterThanZero($count->toInt()); + } + + return new self($count); + } + + public static function default(): self + { + return new self(Count::fromInt(10)); + } + + public function toCount(): Count + { + return $this->count; + } +} diff --git a/src/Reporter/DefaultReporter.php b/src/Reporter/DefaultReporter.php index 79027da6..6a533977 100644 --- a/src/Reporter/DefaultReporter.php +++ b/src/Reporter/DefaultReporter.php @@ -14,8 +14,8 @@ namespace Ergebnis\PHPUnit\SlowTestDetector\Reporter; use Ergebnis\PHPUnit\SlowTestDetector\Comparator; -use Ergebnis\PHPUnit\SlowTestDetector\Count; use Ergebnis\PHPUnit\SlowTestDetector\Formatter; +use Ergebnis\PHPUnit\SlowTestDetector\MaximumCount; use Ergebnis\PHPUnit\SlowTestDetector\MaximumDuration; use Ergebnis\PHPUnit\SlowTestDetector\SlowTest; @@ -35,7 +35,7 @@ final class DefaultReporter implements Reporter private $maximumDuration; /** - * @var Count + * @var MaximumCount */ private $maximumCount; @@ -47,7 +47,7 @@ final class DefaultReporter implements Reporter public function __construct( Formatter\DurationFormatter $durationFormatter, MaximumDuration $maximumDuration, - Count $maximumCount + MaximumCount $maximumCount ) { $this->durationFormatter = $durationFormatter; $this->maximumDuration = $maximumDuration; @@ -110,7 +110,7 @@ private function list(SlowTest ...$slowTests): string $slowTestsToReport = \array_slice( $slowTests, 0, - $this->maximumCount->toInt() + $this->maximumCount->toCount()->toInt() ); /** @var SlowTest $slowTestWithLongestTestDuration */ @@ -175,7 +175,7 @@ static function (MaximumDuration $maximumDuration, SlowTest $slowTest): MaximumD private function footer(SlowTest ...$slowTests): string { $additionalSlowTestCount = \max( - \count($slowTests) - $this->maximumCount->toInt(), + \count($slowTests) - $this->maximumCount->toCount()->toInt(), 0 ); diff --git a/test/Unit/CountTest.php b/test/Unit/CountTest.php index 4c398a6d..8a8d124b 100644 --- a/test/Unit/CountTest.php +++ b/test/Unit/CountTest.php @@ -26,7 +26,6 @@ final class CountTest extends Framework\TestCase { /** * @dataProvider \Ergebnis\PHPUnit\SlowTestDetector\Test\DataProvider\IntProvider::lessThanZero - * @dataProvider \Ergebnis\PHPUnit\SlowTestDetector\Test\DataProvider\IntProvider::zero */ public function testFromIntRejectsInvalidValue(int $value) { @@ -37,6 +36,7 @@ public function testFromIntRejectsInvalidValue(int $value) /** * @dataProvider \Ergebnis\PHPUnit\SlowTestDetector\Test\DataProvider\IntProvider::greaterThanZero + * @dataProvider \Ergebnis\PHPUnit\SlowTestDetector\Test\DataProvider\IntProvider::zero */ public function testFromIntReturnsCount(int $value) { diff --git a/test/Unit/Exception/InvalidCountTest.php b/test/Unit/Exception/InvalidCountTest.php index 6684ad0e..831c5fd3 100644 --- a/test/Unit/Exception/InvalidCountTest.php +++ b/test/Unit/Exception/InvalidCountTest.php @@ -28,10 +28,10 @@ public function testNotGreaterThanZeroReturnsException() { $value = self::faker()->numberBetween(); - $exception = Exception\InvalidCount::notGreaterThanZero($value); + $exception = Exception\InvalidCount::notGreaterThanOrEqualToZero($value); $message = \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 ); diff --git a/test/Unit/Exception/InvalidMaximumCountTest.php b/test/Unit/Exception/InvalidMaximumCountTest.php new file mode 100644 index 00000000..7ed718a4 --- /dev/null +++ b/test/Unit/Exception/InvalidMaximumCountTest.php @@ -0,0 +1,40 @@ +numberBetween(); + + $exception = Exception\InvalidMaximumCount::notGreaterThanZero($value); + + $message = \sprintf( + 'Value should be greater than 0, but %d is not.', + $value + ); + + self::assertSame($message, $exception->getMessage()); + } +} diff --git a/test/Unit/MaximumCountTest.php b/test/Unit/MaximumCountTest.php new file mode 100644 index 00000000..b7838df9 --- /dev/null +++ b/test/Unit/MaximumCountTest.php @@ -0,0 +1,61 @@ +expectException(Exception\InvalidMaximumCount::class); + + MaximumCount::fromCount($count); + } + + /** + * @dataProvider \Ergebnis\PHPUnit\SlowTestDetector\Test\DataProvider\IntProvider::greaterThanZero + */ + public function testFromCountReturnsMaximumCount(int $value) + { + $count = Count::fromInt($value); + + $maximumCount = MaximumCount::fromCount($count); + + self::assertSame($count, $maximumCount->toCount()); + } + + public function testDefaultReturnsMaximumCount() + { + $maximumCount = MaximumCount::default(); + + $expected = Count::fromInt(10); + + self::assertEquals($expected, $maximumCount->toCount()); + } +} diff --git a/test/Unit/Reporter/DefaultReporterTest.php b/test/Unit/Reporter/DefaultReporterTest.php index eca1d4cf..328b722f 100644 --- a/test/Unit/Reporter/DefaultReporterTest.php +++ b/test/Unit/Reporter/DefaultReporterTest.php @@ -16,6 +16,7 @@ use Ergebnis\PHPUnit\SlowTestDetector\Count; use Ergebnis\PHPUnit\SlowTestDetector\Duration; use Ergebnis\PHPUnit\SlowTestDetector\Formatter; +use Ergebnis\PHPUnit\SlowTestDetector\MaximumCount; use Ergebnis\PHPUnit\SlowTestDetector\MaximumDuration; use Ergebnis\PHPUnit\SlowTestDetector\Reporter; use Ergebnis\PHPUnit\SlowTestDetector\SlowTest; @@ -49,7 +50,7 @@ public function testReportReturnsEmptyStringWhenThereAreNoSlowTests() $reporter = new Reporter\DefaultReporter( new Formatter\DefaultDurationFormatter(), MaximumDuration::fromDuration(Duration::fromMilliseconds($faker->numberBetween(0))), - Count::fromInt($faker->numberBetween(1)) + MaximumCount::fromCount(Count::fromInt($faker->numberBetween(1))) ); $report = $reporter->report(); @@ -65,7 +66,7 @@ public function testReportReturnsEmptyStringWhenThereAreNoSlowTests() public function testReportReturnsReportWhenThereAreFewerSlowTestsThanMaximumCount( string $expectedReport, MaximumDuration $maximumDuration, - Count $maximumCount, + MaximumCount $maximumCount, array $slowTests ) { $reporter = new Reporter\DefaultReporter( @@ -80,7 +81,7 @@ public function testReportReturnsReportWhenThereAreFewerSlowTestsThanMaximumCoun } /** - * @return \Generator}> + * @return \Generator}> */ public static function provideExpectedReportMaximumDurationMaximumCountAndSlowTests(): iterable { @@ -93,7 +94,7 @@ public static function provideExpectedReportMaximumDurationMaximumCountAndSlowTe TXT , MaximumDuration::fromDuration(Duration::fromMilliseconds(500)), - Count::fromInt(1), + MaximumCount::fromCount(Count::fromInt(1)), [ SlowTest::create( TestIdentifier::fromString('FooTest::test'), @@ -112,7 +113,7 @@ public static function provideExpectedReportMaximumDurationMaximumCountAndSlowTe TXT , MaximumDuration::fromDuration(Duration::fromMilliseconds(500)), - Count::fromInt(2), + MaximumCount::fromCount(Count::fromInt(2)), [ SlowTest::create( TestIdentifier::fromString('FooTest::test'), @@ -138,7 +139,7 @@ public static function provideExpectedReportMaximumDurationMaximumCountAndSlowTe TXT , MaximumDuration::fromDuration(Duration::fromMilliseconds(500)), - Count::fromInt(3), + MaximumCount::fromCount(Count::fromInt(3)), [ SlowTest::create( TestIdentifier::fromString('FooTest::test'), @@ -170,7 +171,7 @@ public static function provideExpectedReportMaximumDurationMaximumCountAndSlowTe TXT , MaximumDuration::fromDuration(Duration::fromMilliseconds(500)), - Count::fromInt(3), + MaximumCount::fromCount(Count::fromInt(3)), [ SlowTest::create( TestIdentifier::fromString('BazTest::test'), @@ -209,7 +210,7 @@ public static function provideExpectedReportMaximumDurationMaximumCountAndSlowTe TXT , MaximumDuration::fromDuration(Duration::fromMilliseconds(500)), - Count::fromInt(10), + MaximumCount::fromCount(Count::fromInt(10)), [ SlowTest::create( TestIdentifier::fromString('FooTest::test'), @@ -283,7 +284,7 @@ public static function provideExpectedReportMaximumDurationMaximumCountAndSlowTe TXT , MaximumDuration::fromDuration(Duration::fromMilliseconds(500)), - Count::fromInt(1), + MaximumCount::fromCount(Count::fromInt(1)), [ SlowTest::create( TestIdentifier::fromString('FooTest::test'), @@ -309,7 +310,7 @@ public static function provideExpectedReportMaximumDurationMaximumCountAndSlowTe TXT , MaximumDuration::fromDuration(Duration::fromMilliseconds(500)), - Count::fromInt(1), + MaximumCount::fromCount(Count::fromInt(1)), [ SlowTest::create( TestIdentifier::fromString('FooTest::test'),