Skip to content

Commit 77b8430

Browse files
authored
Merge pull request #663 from ergebnis/feature/extract
Enhancement: Extract `MaximumCount`
2 parents b08645f + cc45618 commit 77b8430

File tree

12 files changed

+227
-28
lines changed

12 files changed

+227
-28
lines changed

phpstan-baseline.neon

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,11 @@ parameters:
210210
count: 1
211211
path: test/Unit/Exception/InvalidCountTest.php
212212

213+
-
214+
message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Test\\\\Unit\\\\Exception\\\\InvalidMaximumCountTest\\:\\:testNotGreaterThanZeroReturnsException\\(\\) has no return type specified\\.$#"
215+
count: 1
216+
path: test/Unit/Exception/InvalidMaximumCountTest.php
217+
213218
-
214219
message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Test\\\\Unit\\\\Exception\\\\InvalidMillisecondsTest\\:\\:testNotGreaterThanZeroReturnsException\\(\\) has no return type specified\\.$#"
215220
count: 1
@@ -265,6 +270,21 @@ parameters:
265270
count: 1
266271
path: test/Unit/Formatter/DefaultDurationFormatterTest.php
267272

273+
-
274+
message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Test\\\\Unit\\\\MaximumCountTest\\:\\:testDefaultReturnsMaximumCount\\(\\) has no return type specified\\.$#"
275+
count: 1
276+
path: test/Unit/MaximumCountTest.php
277+
278+
-
279+
message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Test\\\\Unit\\\\MaximumCountTest\\:\\:testFromCountRejectsInvalidCount\\(\\) has no return type specified\\.$#"
280+
count: 1
281+
path: test/Unit/MaximumCountTest.php
282+
283+
-
284+
message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Test\\\\Unit\\\\MaximumCountTest\\:\\:testFromCountReturnsMaximumCount\\(\\) has no return type specified\\.$#"
285+
count: 1
286+
path: test/Unit/MaximumCountTest.php
287+
268288
-
269289
message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Test\\\\Unit\\\\MaximumDurationTest\\:\\:testDefaultReturnsMaximumDuration\\(\\) has no return type specified\\.$#"
270290
count: 1

src/Count.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ private function __construct(int $value)
3333
*/
3434
public static function fromInt(int $value): self
3535
{
36-
if (0 >= $value) {
37-
throw Exception\InvalidCount::notGreaterThanZero($value);
36+
if (0 > $value) {
37+
throw Exception\InvalidCount::notGreaterThanOrEqualToZero($value);
3838
}
3939

4040
return new self($value);

src/Exception/InvalidCount.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
*/
1919
final class InvalidCount extends \InvalidArgumentException
2020
{
21-
public static function notGreaterThanZero(int $value): self
21+
public static function notGreaterThanOrEqualToZero(int $value): self
2222
{
2323
return new self(\sprintf(
24-
'Value should be greater than 0, but %d is not.',
24+
'Value should be greater than or equal to 0, but %d is not.',
2525
$value
2626
));
2727
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Copyright (c) 2021-2025 Andreas Möller
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE.md file that was distributed with this source code.
10+
*
11+
* @see https://github.com/ergebnis/phpunit-slow-test-detector
12+
*/
13+
14+
namespace Ergebnis\PHPUnit\SlowTestDetector\Exception;
15+
16+
/**
17+
* @internal
18+
*/
19+
final class InvalidMaximumCount extends \InvalidArgumentException
20+
{
21+
public static function notGreaterThanZero(int $value): self
22+
{
23+
return new self(\sprintf(
24+
'Value should be greater than 0, but %d is not.',
25+
$value
26+
));
27+
}
28+
}

src/Extension.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ final class Extension implements Framework\TestListener
5353

5454
public function __construct(array $options = [])
5555
{
56-
$maximumCount = Count::fromInt(10);
56+
$maximumCount = MaximumCount::default();
5757

5858
if (\array_key_exists('maximum-count', $options)) {
59-
$maximumCount = Count::fromInt((int) $options['maximum-count']);
59+
$maximumCount = MaximumCount::fromCount(Count::fromInt((int) $options['maximum-count']));
6060
}
6161

6262
$maximumDuration = MaximumDuration::default();
@@ -261,10 +261,10 @@ final class Extension implements
261261

262262
public function __construct(array $options = [])
263263
{
264-
$maximumCount = Count::fromInt(10);
264+
$maximumCount = MaximumCount::default();
265265

266266
if (\array_key_exists('maximum-count', $options)) {
267-
$maximumCount = Count::fromInt((int) $options['maximum-count']);
267+
$maximumCount = MaximumCount::fromCount(Count::fromInt((int) $options['maximum-count']));
268268
}
269269

270270
$maximumDuration = MaximumDuration::default();
@@ -415,10 +415,10 @@ public function bootstrap(
415415
return;
416416
}
417417

418-
$maximumCount = Count::fromInt(10);
418+
$maximumCount = MaximumCount::default();
419419

420420
if ($parameters->has('maximum-count')) {
421-
$maximumCount = Count::fromInt((int) $parameters->get('maximum-count'));
421+
$maximumCount = MaximumCount::fromCount(Count::fromInt((int) $parameters->get('maximum-count')));
422422
}
423423

424424
$maximumDuration = MaximumDuration::default();

src/MaximumCount.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Copyright (c) 2021-2025 Andreas Möller
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE.md file that was distributed with this source code.
10+
*
11+
* @see https://github.com/ergebnis/phpunit-slow-test-detector
12+
*/
13+
14+
namespace Ergebnis\PHPUnit\SlowTestDetector;
15+
16+
/**
17+
* @internal
18+
*/
19+
final class MaximumCount
20+
{
21+
private $count;
22+
23+
private function __construct(Count $count)
24+
{
25+
$this->count = $count;
26+
}
27+
28+
/**
29+
* @throws Exception\InvalidMaximumCount
30+
*/
31+
public static function fromCount(Count $count): self
32+
{
33+
if ($count->toInt() <= 0) {
34+
throw Exception\InvalidMaximumCount::notGreaterThanZero($count->toInt());
35+
}
36+
37+
return new self($count);
38+
}
39+
40+
public static function default(): self
41+
{
42+
return new self(Count::fromInt(10));
43+
}
44+
45+
public function toCount(): Count
46+
{
47+
return $this->count;
48+
}
49+
}

src/Reporter/DefaultReporter.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
namespace Ergebnis\PHPUnit\SlowTestDetector\Reporter;
1515

1616
use Ergebnis\PHPUnit\SlowTestDetector\Comparator;
17-
use Ergebnis\PHPUnit\SlowTestDetector\Count;
1817
use Ergebnis\PHPUnit\SlowTestDetector\Formatter;
18+
use Ergebnis\PHPUnit\SlowTestDetector\MaximumCount;
1919
use Ergebnis\PHPUnit\SlowTestDetector\MaximumDuration;
2020
use Ergebnis\PHPUnit\SlowTestDetector\SlowTest;
2121

@@ -35,7 +35,7 @@ final class DefaultReporter implements Reporter
3535
private $maximumDuration;
3636

3737
/**
38-
* @var Count
38+
* @var MaximumCount
3939
*/
4040
private $maximumCount;
4141

@@ -47,7 +47,7 @@ final class DefaultReporter implements Reporter
4747
public function __construct(
4848
Formatter\DurationFormatter $durationFormatter,
4949
MaximumDuration $maximumDuration,
50-
Count $maximumCount
50+
MaximumCount $maximumCount
5151
) {
5252
$this->durationFormatter = $durationFormatter;
5353
$this->maximumDuration = $maximumDuration;
@@ -110,7 +110,7 @@ private function list(SlowTest ...$slowTests): string
110110
$slowTestsToReport = \array_slice(
111111
$slowTests,
112112
0,
113-
$this->maximumCount->toInt()
113+
$this->maximumCount->toCount()->toInt()
114114
);
115115

116116
/** @var SlowTest $slowTestWithLongestTestDuration */
@@ -175,7 +175,7 @@ static function (MaximumDuration $maximumDuration, SlowTest $slowTest): MaximumD
175175
private function footer(SlowTest ...$slowTests): string
176176
{
177177
$additionalSlowTestCount = \max(
178-
\count($slowTests) - $this->maximumCount->toInt(),
178+
\count($slowTests) - $this->maximumCount->toCount()->toInt(),
179179
0
180180
);
181181

test/Unit/CountTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ final class CountTest extends Framework\TestCase
2626
{
2727
/**
2828
* @dataProvider \Ergebnis\PHPUnit\SlowTestDetector\Test\DataProvider\IntProvider::lessThanZero
29-
* @dataProvider \Ergebnis\PHPUnit\SlowTestDetector\Test\DataProvider\IntProvider::zero
3029
*/
3130
public function testFromIntRejectsInvalidValue(int $value)
3231
{
@@ -37,6 +36,7 @@ public function testFromIntRejectsInvalidValue(int $value)
3736

3837
/**
3938
* @dataProvider \Ergebnis\PHPUnit\SlowTestDetector\Test\DataProvider\IntProvider::greaterThanZero
39+
* @dataProvider \Ergebnis\PHPUnit\SlowTestDetector\Test\DataProvider\IntProvider::zero
4040
*/
4141
public function testFromIntReturnsCount(int $value)
4242
{

test/Unit/Exception/InvalidCountTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ public function testNotGreaterThanZeroReturnsException()
2828
{
2929
$value = self::faker()->numberBetween();
3030

31-
$exception = Exception\InvalidCount::notGreaterThanZero($value);
31+
$exception = Exception\InvalidCount::notGreaterThanOrEqualToZero($value);
3232

3333
$message = \sprintf(
34-
'Value should be greater than 0, but %d is not.',
34+
'Value should be greater than or equal to 0, but %d is not.',
3535
$value
3636
);
3737

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Copyright (c) 2021-2025 Andreas Möller
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE.md file that was distributed with this source code.
10+
*
11+
* @see https://github.com/ergebnis/phpunit-slow-test-detector
12+
*/
13+
14+
namespace Ergebnis\PHPUnit\SlowTestDetector\Test\Unit\Exception;
15+
16+
use Ergebnis\PHPUnit\SlowTestDetector\Exception;
17+
use Ergebnis\PHPUnit\SlowTestDetector\Test;
18+
use PHPUnit\Framework;
19+
20+
/**
21+
* @covers \Ergebnis\PHPUnit\SlowTestDetector\Exception\InvalidMaximumCount
22+
*/
23+
final class InvalidMaximumCountTest extends Framework\TestCase
24+
{
25+
use Test\Util\Helper;
26+
27+
public function testNotGreaterThanZeroReturnsException()
28+
{
29+
$value = self::faker()->numberBetween();
30+
31+
$exception = Exception\InvalidMaximumCount::notGreaterThanZero($value);
32+
33+
$message = \sprintf(
34+
'Value should be greater than 0, but %d is not.',
35+
$value
36+
);
37+
38+
self::assertSame($message, $exception->getMessage());
39+
}
40+
}

0 commit comments

Comments
 (0)