Skip to content

Commit

Permalink
Merge pull request #47 from ergebnis/feature/maximum-count
Browse files Browse the repository at this point in the history
Enhancement: Add MaximumCount
  • Loading branch information
ergebnis-bot committed Jan 26, 2021
2 parents 1df12ef + ae59d6d commit 3325f98
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 61 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -21,6 +21,7 @@ For a full diff see [`7afa59c...main`][7afa59c...main].
* Extracted `Collector` ([#23]), by [@localheinz]
* Added `Subscriber\TestSuiteFinishedSubscriber` ([#34]), by [@localheinz]
* Added `MaximumDuration` ([#46]), by [@localheinz]
* Added `MaximumCount` ([#47]), by [@localheinz]

### Changed

Expand Down Expand Up @@ -57,5 +58,6 @@ For a full diff see [`7afa59c...main`][7afa59c...main].
[#37]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/37
[#38]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/38
[#46]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/46
[#47]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/47

[@localheinz]: https://github.com/localheinz
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -64,12 +64,12 @@ Event\Facade::registerSubscriber(new SlowTestDetector\Subscriber\TestPassedSubsc
$collector
));

$maximumNumber = 10;
$maximumCount = SlowTestDetector\MaximumCount::fromInt(10);

$reporter = new SlowTestDetector\Reporter\DefaultReporter(
new SlowTestDetector\Formatter\ToMillisecondsDurationFormatter(),
$maximumDuration,
$maximumNumber
$maximumCount
);

Event\Facade::registerSubscriber(new SlowTestDetector\Subscriber\TestSuiteFinishedSubscriber(
Expand Down
Expand Up @@ -13,12 +13,12 @@

namespace Ergebnis\PHPUnit\SlowTestDetector\Exception;

final class MaximumNumberNotGreaterThanZero extends \InvalidArgumentException
final class InvalidMaximumCount extends \InvalidArgumentException
{
public static function create(int $value): self
public static function notGreaterThanZero(int $value): self
{
return new self(\sprintf(
'Maximum number should be greater than 0, but %d is not.',
'Value should be greater than 0, but %d is not.',
$value
));
}
Expand Down
41 changes: 41 additions & 0 deletions src/MaximumCount.php
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2021 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;

final class MaximumCount
{
private int $value;

private function __construct(int $value)
{
$this->value = $value;
}

/**
* @throws Exception\InvalidMaximumCount
*/
public static function fromInt(int $value): self
{
if (0 >= $value) {
throw Exception\InvalidMaximumCount::notGreaterThanZero($value);
}

return new self($value);
}

public function toInt(): int
{
return $this->value;
}
}
21 changes: 7 additions & 14 deletions src/Reporter/DefaultReporter.php
Expand Up @@ -15,7 +15,7 @@

use Ergebnis\PHPUnit\SlowTestDetector\Comparator;
use Ergebnis\PHPUnit\SlowTestDetector\Console;
use Ergebnis\PHPUnit\SlowTestDetector\Exception;
use Ergebnis\PHPUnit\SlowTestDetector\MaximumCount;
use Ergebnis\PHPUnit\SlowTestDetector\MaximumDuration;
use Ergebnis\PHPUnit\SlowTestDetector\SlowTest;
use PHPUnit\Event;
Expand All @@ -26,26 +26,19 @@ final class DefaultReporter implements Reporter

private Event\Telemetry\DurationFormatter $durationFormatter;

private int $maximumNumber;

private MaximumDuration $maximumDuration;

/**
* @throws Exception\MaximumNumberNotGreaterThanZero
*/
private MaximumCount $maximumCount;

public function __construct(
Event\Telemetry\DurationFormatter $durationFormatter,
MaximumDuration $maximumDuration,
int $maximumNumber
MaximumCount $maximumCount
) {
if (0 >= $maximumNumber) {
throw Exception\MaximumNumberNotGreaterThanZero::create($maximumNumber);
}

$this->durationComparator = new Comparator\DurationComparator();
$this->durationFormatter = $durationFormatter;
$this->maximumDuration = $maximumDuration;
$this->maximumNumber = $maximumNumber;
$this->maximumCount = $maximumCount;
}

public function report(SlowTest ...$slowTests): string
Expand Down Expand Up @@ -103,7 +96,7 @@ private function list(SlowTest ...$slowTests): string
$slowTestsToReport = \array_slice(
$slowTests,
0,
$this->maximumNumber
$this->maximumCount->toInt()
);

/** @var SlowTest $slowestTest */
Expand Down Expand Up @@ -166,7 +159,7 @@ static function (Event\Telemetry\Duration $maximumDuration, SlowTest $slowTest):
private function footer(SlowTest ...$slowTests): string
{
$remainingCount = \max(
\count($slowTests) - $this->maximumNumber,
\count($slowTests) - $this->maximumCount->toInt(),
0
);

Expand Down
6 changes: 3 additions & 3 deletions test/Example/bootstrap.php
Expand Up @@ -16,10 +16,10 @@
use Ergebnis\PHPUnit\SlowTestDetector;
use PHPUnit\Event;

$maximumNumber = 3;
$maximumCount = SlowTestDetector\MaximumCount::fromInt(3);

if (\is_string(\getenv('MAXIMUM_NUMBER'))) {
$maximumNumber = (int) \getenv('MAXIMUM_NUMBER');
$maximumCount = SlowTestDetector\MaximumCount::fromInt((int) \getenv('MAXIMUM_NUMBER'));
}

$maximumDuration = SlowTestDetector\MaximumDuration::fromMilliseconds(125);
Expand All @@ -29,7 +29,7 @@
$reporter = new SlowTestDetector\Reporter\DefaultReporter(
new SlowTestDetector\Formatter\ToMillisecondsDurationFormatter(),
$maximumDuration,
$maximumNumber
$maximumCount
);

$timeKeeper = new SlowTestDetector\TimeKeeper();
Expand Down
Expand Up @@ -13,27 +13,27 @@

namespace Ergebnis\PHPUnit\SlowTestDetector\Test\Unit\Exception;

use Ergebnis\PHPUnit\SlowTestDetector\Exception\MaximumNumberNotGreaterThanZero;
use Ergebnis\PHPUnit\SlowTestDetector\Exception\InvalidMaximumCount;
use Ergebnis\Test\Util;
use PHPUnit\Framework;

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

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

$exception = MaximumNumberNotGreaterThanZero::create($value);
$exception = InvalidMaximumCount::notGreaterThanZero($value);

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

Expand Down
49 changes: 49 additions & 0 deletions test/Unit/MaximumCountTest.php
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2021 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\Exception;
use Ergebnis\PHPUnit\SlowTestDetector\MaximumCount;
use PHPUnit\Framework;

/**
* @internal
*
* @covers \Ergebnis\PHPUnit\SlowTestDetector\MaximumCount
*
* @uses \Ergebnis\PHPUnit\SlowTestDetector\Exception\InvalidMaximumCount
*/
final class MaximumCountTest extends Framework\TestCase
{
/**
* @dataProvider \Ergebnis\Test\Util\DataProvider\IntProvider::lessThanZero()
* @dataProvider \Ergebnis\Test\Util\DataProvider\IntProvider::zero()
*/
public function testFromIntRejectsInvalidValue(int $value): void
{
$this->expectException(Exception\InvalidMaximumCount::class);

MaximumCount::fromInt($value);
}

/**
* @dataProvider \Ergebnis\Test\Util\DataProvider\IntProvider::greaterThanZero()
*/
public function testFromSecondsReturnsMaximumDuration(int $value): void
{
$maximumCount = MaximumCount::fromInt($value);

self::assertSame($value, $maximumCount->toInt());
}
}
47 changes: 14 additions & 33 deletions test/Unit/Reporter/DefaultReporterTest.php
Expand Up @@ -13,8 +13,8 @@

namespace Ergebnis\PHPUnit\SlowTestDetector\Test\Unit\Reporter;

use Ergebnis\PHPUnit\SlowTestDetector\Exception;
use Ergebnis\PHPUnit\SlowTestDetector\Formatter\ToMillisecondsDurationFormatter;
use Ergebnis\PHPUnit\SlowTestDetector\MaximumCount;
use Ergebnis\PHPUnit\SlowTestDetector\MaximumDuration;
use Ergebnis\PHPUnit\SlowTestDetector\Reporter\DefaultReporter;
use Ergebnis\PHPUnit\SlowTestDetector\SlowTest;
Expand All @@ -30,42 +30,23 @@
*
* @uses \Ergebnis\PHPUnit\SlowTestDetector\Comparator\DurationComparator
* @uses \Ergebnis\PHPUnit\SlowTestDetector\Console\Color
* @uses \Ergebnis\PHPUnit\SlowTestDetector\Exception\MaximumNumberNotGreaterThanZero
* @uses \Ergebnis\PHPUnit\SlowTestDetector\Exception\InvalidMaximumCount
* @uses \Ergebnis\PHPUnit\SlowTestDetector\Formatter\ToMillisecondsDurationFormatter
* @uses \Ergebnis\PHPUnit\SlowTestDetector\MaximumCount
* @uses \Ergebnis\PHPUnit\SlowTestDetector\MaximumDuration
* @uses \Ergebnis\PHPUnit\SlowTestDetector\SlowTest
*/
final class DefaultReporterTest extends Framework\TestCase
{
use Util\Helper;

/**
* @dataProvider \Ergebnis\Test\Util\DataProvider\IntProvider::lessThanZero()
* @dataProvider \Ergebnis\Test\Util\DataProvider\IntProvider::zero()
*/
public function testConstructorRejectsMaximumCountLessThanOne(int $maximumCount): void
{
$faker = self::faker();

$durationFormatter = $this->createMock(Event\Telemetry\DurationFormatter::class);
$maximumDuration = MaximumDuration::fromMilliseconds($faker->numberBetween());

$this->expectException(Exception\MaximumNumberNotGreaterThanZero::class);

new DefaultReporter(
$durationFormatter,
$maximumDuration,
$maximumCount
);
}

public function testReportReturnsEmptyStringWhenNoSlowTestsHaveBeenSpecified(): void
{
$faker = self::faker();

$durationFormatter = $this->createMock(Event\Telemetry\DurationFormatter::class);
$maximumDuration = MaximumDuration::fromMilliseconds($faker->numberBetween());
$maximumCount = $faker->numberBetween();
$maximumCount = MaximumCount::fromInt($faker->numberBetween(1));

$reporter = new DefaultReporter(
$durationFormatter,
Expand Down Expand Up @@ -102,12 +83,12 @@ public function testReportReturnsReportWhenTheNumberOfSlowTestsIsSmallerThanTheM

$durationFormatter = new ToMillisecondsDurationFormatter();

$maximumNumber = \count($slowTests);
$maximumCount = MaximumCount::fromInt(\count($slowTests));

$reporter = new DefaultReporter(
$durationFormatter,
$maximumDuration,
$maximumNumber
$maximumCount
);

$report = $reporter->report(...$slowTests);
Expand Down Expand Up @@ -195,12 +176,12 @@ public function testReportReturnsReportWhenTheNumberOfSlowTestsIsSmallerThanTheM

$durationFormatter = new ToMillisecondsDurationFormatter();

$maximumNumber = $faker->numberBetween(\count($slowTests) + 1);
$maximumCount = MaximumCount::fromInt($faker->numberBetween(\count($slowTests) + 1));

$reporter = new DefaultReporter(
$durationFormatter,
$maximumDuration,
$maximumNumber
$maximumCount
);

$report = $reporter->report(...$slowTests);
Expand Down Expand Up @@ -290,12 +271,12 @@ public function testReportReturnsReportWhenTheNumberOfSlowTestsIsEqualToTheMaxim

$durationFormatter = new ToMillisecondsDurationFormatter();

$maximumNumber = \count($slowTests);
$maximumCount = MaximumCount::fromInt(\count($slowTests));

$reporter = new DefaultReporter(
$durationFormatter,
$maximumDuration,
$maximumNumber
$maximumCount
);

$report = $reporter->report(...$slowTests);
Expand Down Expand Up @@ -385,12 +366,12 @@ public function testReportReturnsReportWhenTheNumberOfSlowTestsIsOneMoreThanTheM

$durationFormatter = new ToMillisecondsDurationFormatter();

$maximumNumber = \count($slowTests) - 1;
$maximumCount = MaximumCount::fromInt(\count($slowTests) - 1);

$reporter = new DefaultReporter(
$durationFormatter,
$maximumDuration,
$maximumNumber
$maximumCount
);

$report = $reporter->report(...$slowTests);
Expand Down Expand Up @@ -481,12 +462,12 @@ public function testReportReturnsReportWhenTheNumberOfSlowTestsIsGreaterThanTheM

$durationFormatter = new ToMillisecondsDurationFormatter();

$maximumNumber = \count($slowTests) - 2;
$maximumCount = MaximumCount::fromInt(\count($slowTests) - 2);

$reporter = new DefaultReporter(
$durationFormatter,
$maximumDuration,
$maximumNumber
$maximumCount
);

$report = $reporter->report(...$slowTests);
Expand Down

0 comments on commit 3325f98

Please sign in to comment.