generated from ergebnis/php-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhancement: Implement MaximumDuration
- Loading branch information
1 parent
7c85a95
commit e6bd9ba
Showing
11 changed files
with
291 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?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\Exception; | ||
|
||
final class InvalidMaximumDuration extends \InvalidArgumentException | ||
{ | ||
public static function notGreaterThanZero(int $value): self | ||
{ | ||
return new self(\sprintf( | ||
'Value should be greater than 0, but %d is not.', | ||
$value | ||
)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?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; | ||
|
||
use PHPUnit\Event; | ||
|
||
final class MaximumDuration | ||
{ | ||
private Event\Telemetry\Duration $duration; | ||
|
||
private function __construct(Event\Telemetry\Duration $duration) | ||
{ | ||
$this->duration = $duration; | ||
} | ||
|
||
/** | ||
* @throws Exception\InvalidMaximumDuration | ||
*/ | ||
public static function fromSeconds(int $seconds): self | ||
{ | ||
if (0 >= $seconds) { | ||
throw Exception\InvalidMaximumDuration::notGreaterThanZero($seconds); | ||
} | ||
|
||
return new self(Event\Telemetry\Duration::fromSecondsAndNanoseconds( | ||
$seconds, | ||
0 | ||
)); | ||
} | ||
|
||
/** | ||
* @throws Exception\InvalidMaximumDuration | ||
*/ | ||
public static function fromMilliseconds(int $milliseconds): self | ||
{ | ||
if (0 >= $milliseconds) { | ||
throw Exception\InvalidMaximumDuration::notGreaterThanZero($milliseconds); | ||
} | ||
|
||
$seconds = \intdiv( | ||
$milliseconds, | ||
1_000 | ||
); | ||
|
||
$nanoseconds = ($milliseconds - $seconds * 1_000) * 1_000_000; | ||
|
||
return new self(Event\Telemetry\Duration::fromSecondsAndNanoseconds( | ||
$seconds, | ||
$nanoseconds | ||
)); | ||
} | ||
|
||
public function toTelemetryDuration(): Event\Telemetry\Duration | ||
{ | ||
return $this->duration; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?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\Exception; | ||
|
||
use Ergebnis\PHPUnit\SlowTestDetector\Exception\InvalidMaximumDuration; | ||
use Ergebnis\Test\Util; | ||
use PHPUnit\Framework; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @covers \Ergebnis\PHPUnit\SlowTestDetector\Exception\InvalidMaximumDuration | ||
*/ | ||
final class InvalidMaximumDurationTest extends Framework\TestCase | ||
{ | ||
use Util\Helper; | ||
|
||
public function testNotGreaterThanReturnsException(): void | ||
{ | ||
$value = self::faker()->numberBetween(); | ||
|
||
$exception = InvalidMaximumDuration::notGreaterThanZero($value); | ||
|
||
$message = \sprintf( | ||
'Value should be greater than 0, but %d is not.', | ||
$value | ||
); | ||
|
||
self::assertSame($message, $exception->getMessage()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?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\MaximumDuration; | ||
use Ergebnis\Test\Util; | ||
use PHPUnit\Event; | ||
use PHPUnit\Framework; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @covers \Ergebnis\PHPUnit\SlowTestDetector\MaximumDuration | ||
* | ||
* @uses \Ergebnis\PHPUnit\SlowTestDetector\Exception\InvalidMaximumDuration | ||
*/ | ||
final class MaximumDurationTest extends Framework\TestCase | ||
{ | ||
use Util\Helper; | ||
|
||
/** | ||
* @dataProvider \Ergebnis\Test\Util\DataProvider\IntProvider::lessThanZero() | ||
* @dataProvider \Ergebnis\Test\Util\DataProvider\IntProvider::zero() | ||
*/ | ||
public function testFromMillisecondsRejectsInvalidValue(int $milliseconds): void | ||
{ | ||
$this->expectException(Exception\InvalidMaximumDuration::class); | ||
|
||
MaximumDuration::fromMilliseconds($milliseconds); | ||
} | ||
|
||
/** | ||
* @dataProvider provideMillisecondsAndTelemetryDuration | ||
*/ | ||
public function testFromMillisecondsReturnsMaximumDuration(int $milliseconds, Event\Telemetry\Duration $duration): void | ||
{ | ||
$maximumDuration = MaximumDuration::fromMilliseconds($milliseconds); | ||
|
||
self::assertEquals($duration, $maximumDuration->toTelemetryDuration()); | ||
} | ||
|
||
/** | ||
* @return \Generator<int, array{0: int, Event\Telemetry\Duration}> | ||
*/ | ||
public function provideMillisecondsAndTelemetryDuration(): \Generator | ||
{ | ||
$values = [ | ||
1 => Event\Telemetry\Duration::fromSecondsAndNanoseconds(0, 1_000_000), | ||
999 => Event\Telemetry\Duration::fromSecondsAndNanoseconds(0, 999_000_000), | ||
1_000 => Event\Telemetry\Duration::fromSecondsAndNanoseconds(1, 0), | ||
1_234 => Event\Telemetry\Duration::fromSecondsAndNanoseconds(1, 234_000_000), | ||
]; | ||
|
||
foreach ($values as $milliseconds => $duration) { | ||
yield $milliseconds => [ | ||
$milliseconds, | ||
$duration, | ||
]; | ||
} | ||
} | ||
|
||
/** | ||
* @dataProvider \Ergebnis\Test\Util\DataProvider\IntProvider::lessThanZero() | ||
* @dataProvider \Ergebnis\Test\Util\DataProvider\IntProvider::zero() | ||
*/ | ||
public function testFromSecondsRejectsInvalidValue(int $seconds): void | ||
{ | ||
$this->expectException(Exception\InvalidMaximumDuration::class); | ||
|
||
MaximumDuration::fromSeconds($seconds); | ||
} | ||
|
||
/** | ||
* @dataProvider \Ergebnis\Test\Util\DataProvider\IntProvider::greaterThanZero() | ||
*/ | ||
public function testFromSecondsReturnsMaximumDuration(int $seconds): void | ||
{ | ||
$maximumDuration = MaximumDuration::fromSeconds($seconds); | ||
|
||
$expected = Event\Telemetry\Duration::fromSecondsAndNanoseconds( | ||
$seconds, | ||
0 | ||
); | ||
|
||
self::assertEquals($expected, $maximumDuration->toTelemetryDuration()); | ||
} | ||
} |
Oops, something went wrong.