Skip to content

Commit

Permalink
Merge pull request #352 from ergebnis/feature/merge
Browse files Browse the repository at this point in the history
Enhancement: Merge `MaximumDuration` into `Duration`
  • Loading branch information
localheinz authored Nov 2, 2023
2 parents 3cbb31d + fafb632 commit 070e1f8
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 168 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ For a full diff see [`2.3.2...main`][2.3.2...main].
### Changed

- Extracted `Duration` ([#351]), by [@localheinz]
- Merged `MaximumDuration` into `Duration` ([#352]), by [@localheinz]

### Fixed

Expand Down Expand Up @@ -174,5 +175,6 @@ For a full diff see [`7afa59c...1.0.0`][7afa59c...1.0.0].
[#343]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/343
[#350]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/350
[#351]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/351
[#352]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/352

[@localheinz]: https://github.com/localheinz
8 changes: 4 additions & 4 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@
<code>provideMillisecondsGreaterThanDefaultMaximumDuration</code>
</PossiblyUnusedMethod>
</file>
<file src="test/Unit/Formatter/DefaultDurationFormatterTest.php">
<file src="test/Unit/DurationTest.php">
<PossiblyUnusedMethod>
<code>provideDurationAndFormattedDuration</code>
<code>provideMillisecondsSecondsAndNanoseconds</code>
</PossiblyUnusedMethod>
</file>
<file src="test/Unit/MaximumDurationTest.php">
<file src="test/Unit/Formatter/DefaultDurationFormatterTest.php">
<PossiblyUnusedMethod>
<code>provideMillisecondsAndDuration</code>
<code>provideDurationAndFormattedDuration</code>
</PossiblyUnusedMethod>
</file>
</files>
22 changes: 22 additions & 0 deletions src/Duration.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,28 @@ public static function fromSecondsAndNanoseconds(
);
}

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

$seconds = \intdiv(
$milliseconds,
1_000,
);

$nanoseconds = ($milliseconds - $seconds * 1_000) * 1_000_000;

return new self(
$seconds,
$nanoseconds,
);
}

public function seconds(): int
{
return $this->seconds;
Expand Down
4 changes: 2 additions & 2 deletions src/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ public function bootstrap(
$maximumCount = MaximumCount::fromInt((int) $parameters->get('maximum-count'));
}

$maximumDuration = MaximumDuration::fromMilliseconds(500);
$maximumDuration = Duration::fromMilliseconds(500);

if ($parameters->has('maximum-duration')) {
$maximumDuration = MaximumDuration::fromMilliseconds((int) $parameters->get('maximum-duration'));
$maximumDuration = Duration::fromMilliseconds((int) $parameters->get('maximum-duration'));
}

$collector = new Collector\DefaultCollector();
Expand Down
66 changes: 0 additions & 66 deletions src/MaximumDuration.php

This file was deleted.

5 changes: 2 additions & 3 deletions src/Reporter/DefaultReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Ergebnis\PHPUnit\SlowTestDetector\Duration;
use Ergebnis\PHPUnit\SlowTestDetector\Formatter;
use Ergebnis\PHPUnit\SlowTestDetector\MaximumCount;
use Ergebnis\PHPUnit\SlowTestDetector\MaximumDuration;
use Ergebnis\PHPUnit\SlowTestDetector\SlowTest;

/**
Expand All @@ -29,7 +28,7 @@ final class DefaultReporter implements Reporter

public function __construct(
private readonly Formatter\DurationFormatter $durationFormatter,
private readonly MaximumDuration $maximumDuration,
private readonly Duration $maximumDuration,
private readonly MaximumCount $maximumCount,
) {
$this->durationComparator = new Comparator\DurationComparator();
Expand Down Expand Up @@ -105,7 +104,7 @@ static function (Duration $maximumDuration, SlowTest $slowTest): Duration {

return $maximumDuration;
},
$this->maximumDuration->toDuration(),
$this->maximumDuration,
);

$durationFormatter = $this->durationFormatter;
Expand Down
7 changes: 3 additions & 4 deletions src/Subscriber/TestPassedSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use Ergebnis\PHPUnit\SlowTestDetector\Collector;
use Ergebnis\PHPUnit\SlowTestDetector\Duration;
use Ergebnis\PHPUnit\SlowTestDetector\MaximumDuration;
use Ergebnis\PHPUnit\SlowTestDetector\SlowTest;
use Ergebnis\PHPUnit\SlowTestDetector\TimeKeeper;
use PHPUnit\Event;
Expand All @@ -27,7 +26,7 @@
final class TestPassedSubscriber implements Event\Test\PassedSubscriber
{
public function __construct(
private readonly MaximumDuration $maximumDuration,
private readonly Duration $maximumDuration,
private readonly TimeKeeper $timeKeeper,
private readonly Collector\Collector $collector,
) {
Expand Down Expand Up @@ -85,9 +84,9 @@ private function resolveMaximumDuration(Event\Code\Test $test): Duration
continue;
}

return MaximumDuration::fromMilliseconds((int) $maximumDuration)->toDuration();
return Duration::fromMilliseconds((int) $maximumDuration);
}

return $this->maximumDuration->toDuration();
return $this->maximumDuration;
}
}
54 changes: 54 additions & 0 deletions test/Unit/DurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,60 @@ public function testFromSecondsAndNanosecondsReturnsDuration(): void
self::assertSame($nanoseconds, $duration->nanoseconds());
}

#[Framework\Attributes\DataProviderExternal(DataProvider\IntProvider::class, 'lessThanZero')]
#[Framework\Attributes\DataProviderExternal(DataProvider\IntProvider::class, 'zero')]
public function testFromMillisecondsRejectsInvalidValue(int $milliseconds): void
{
$this->expectException(Exception\InvalidMilliseconds::class);

Duration::fromMilliseconds($milliseconds);
}

#[Framework\Attributes\DataProvider('provideMillisecondsSecondsAndNanoseconds')]
public function testFromMillisecondsReturnsDuration(
int $milliseconds,
int $seconds,
int $nanoseconds,
): void {
$duration = Duration::fromMilliseconds($milliseconds);

self::assertSame($seconds, $duration->seconds());
self::assertSame($nanoseconds, $duration->nanoseconds());
}

/**
* @return \Generator<int, array{0: int, 1: int, 2: int}>
*/
public static function provideMillisecondsSecondsAndNanoseconds(): \Generator
{
$values = [
1 => [
0,
1_000_000,
],
999 => [
0,
999_000_000,
],
1_000 => [
1,
0,
],
1_234 => [
1,
234_000_000,
],
];

foreach ($values as $milliseconds => [$seconds, $nanoseconds]) {
yield $milliseconds => [
$milliseconds,
$seconds,
$nanoseconds,
];
}
}

public function testIsLessThanReturnsFalseWhenSecondsAreGreater(): void
{
$one = Duration::fromSecondsAndNanoseconds(123, 456);
Expand Down
89 changes: 0 additions & 89 deletions test/Unit/MaximumDurationTest.php

This file was deleted.

0 comments on commit 070e1f8

Please sign in to comment.