Skip to content

Commit

Permalink
Merge pull request #663 from ergebnis/feature/extract
Browse files Browse the repository at this point in the history
Enhancement: Extract `MaximumCount`
  • Loading branch information
localheinz authored Feb 22, 2025
2 parents b08645f + cc45618 commit 77b8430
Show file tree
Hide file tree
Showing 12 changed files with 227 additions and 28 deletions.
20 changes: 20 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/Count.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
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
28 changes: 28 additions & 0 deletions src/Exception/InvalidMaximumCount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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 InvalidMaximumCount extends \InvalidArgumentException
{
public static function notGreaterThanZero(int $value): self
{
return new self(\sprintf(
'Value should be greater than 0, but %d is not.',
$value
));
}
}
12 changes: 6 additions & 6 deletions src/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ final class Extension implements Framework\TestListener

public function __construct(array $options = [])
{
$maximumCount = Count::fromInt(10);
$maximumCount = MaximumCount::default();

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

$maximumDuration = MaximumDuration::default();
Expand Down Expand Up @@ -261,10 +261,10 @@ final class Extension implements

public function __construct(array $options = [])
{
$maximumCount = Count::fromInt(10);
$maximumCount = MaximumCount::default();

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

$maximumDuration = MaximumDuration::default();
Expand Down Expand Up @@ -415,10 +415,10 @@ public function bootstrap(
return;
}

$maximumCount = Count::fromInt(10);
$maximumCount = MaximumCount::default();

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

$maximumDuration = MaximumDuration::default();
Expand Down
49 changes: 49 additions & 0 deletions src/MaximumCount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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;

/**
* @internal
*/
final class MaximumCount
{
private $count;

private function __construct(Count $count)
{
$this->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;
}
}
10 changes: 5 additions & 5 deletions src/Reporter/DefaultReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -35,7 +35,7 @@ final class DefaultReporter implements Reporter
private $maximumDuration;

/**
* @var Count
* @var MaximumCount
*/
private $maximumCount;

Expand All @@ -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;
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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
);

Expand Down
2 changes: 1 addition & 1 deletion test/Unit/CountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand Down
4 changes: 2 additions & 2 deletions test/Unit/Exception/InvalidCountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);

Expand Down
40 changes: 40 additions & 0 deletions test/Unit/Exception/InvalidMaximumCountTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?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\Test\Unit\Exception;

use Ergebnis\PHPUnit\SlowTestDetector\Exception;
use Ergebnis\PHPUnit\SlowTestDetector\Test;
use PHPUnit\Framework;

/**
* @covers \Ergebnis\PHPUnit\SlowTestDetector\Exception\InvalidMaximumCount
*/
final class InvalidMaximumCountTest extends Framework\TestCase
{
use Test\Util\Helper;

public function testNotGreaterThanZeroReturnsException()
{
$value = self::faker()->numberBetween();

$exception = Exception\InvalidMaximumCount::notGreaterThanZero($value);

$message = \sprintf(
'Value should be greater than 0, but %d is not.',
$value
);

self::assertSame($message, $exception->getMessage());
}
}
61 changes: 61 additions & 0 deletions test/Unit/MaximumCountTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?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\Test\Unit;

use Ergebnis\PHPUnit\SlowTestDetector\Count;
use Ergebnis\PHPUnit\SlowTestDetector\Exception;
use Ergebnis\PHPUnit\SlowTestDetector\MaximumCount;
use Ergebnis\PHPUnit\SlowTestDetector\Test;
use PHPUnit\Framework;

/**
* @covers \Ergebnis\PHPUnit\SlowTestDetector\MaximumCount
*
* @uses \Ergebnis\PHPUnit\SlowTestDetector\Count
* @uses \Ergebnis\PHPUnit\SlowTestDetector\Exception\InvalidMaximumCount
*/
final class MaximumCountTest extends Framework\TestCase
{
use Test\Util\Helper;

public function testFromCountRejectsInvalidCount()
{
$count = Count::fromInt(0);

$this->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());
}
}
Loading

0 comments on commit 77b8430

Please sign in to comment.