generated from ergebnis/php-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from ergebnis/feature/to-milliseconds-duration…
…-formatter Enhancement: Implement ToMillisecondsDurationFormatter
- Loading branch information
Showing
3 changed files
with
134 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?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\Formatter; | ||
|
||
use PHPUnit\Event; | ||
|
||
final class ToMillisecondsDurationFormatter implements Event\Telemetry\DurationFormatter | ||
{ | ||
public function format(Event\Telemetry\Duration $duration): string | ||
{ | ||
$milliseconds = self::toMilliseconds($duration); | ||
|
||
return \sprintf( | ||
'%s ms', | ||
\number_format($milliseconds) | ||
); | ||
} | ||
|
||
private static function toMilliseconds(Event\Telemetry\Duration $duration): int | ||
{ | ||
return $duration->seconds() * (10 ** 3) | ||
+ (int) \round($duration->nanoseconds() / (10 ** 6)); | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
test/Unit/Formatter/ToMillisecondsDurationFormatterTest.php
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,97 @@ | ||
<?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\Formatter; | ||
|
||
use Ergebnis\PHPUnit\SlowTestDetector\Formatter\ToMillisecondsDurationFormatter; | ||
use Ergebnis\Test\Util; | ||
use PHPUnit\Event; | ||
use PHPUnit\Framework; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @covers \Ergebnis\PHPUnit\SlowTestDetector\Formatter\ToMillisecondsDurationFormatter | ||
*/ | ||
final class ToMillisecondsDurationFormatterTest extends Framework\TestCase | ||
{ | ||
use Util\Helper; | ||
|
||
/** | ||
* @dataProvider provideDurationAndFormattedDuration | ||
*/ | ||
public function testFormatFormats(Event\Telemetry\Duration $duration, string $formattedDuration): void | ||
{ | ||
$formatter = new ToMillisecondsDurationFormatter(); | ||
|
||
self::assertSame($formattedDuration, $formatter->format($duration)); | ||
} | ||
|
||
/** | ||
* @return array<string, array{0: Event\Telemetry\Duration, 1: string}> | ||
*/ | ||
public function provideDurationAndFormattedDuration(): array | ||
{ | ||
return [ | ||
'zero' => [ | ||
Event\Telemetry\Duration::fromSecondsAndNanoseconds( | ||
0, | ||
0 | ||
), | ||
'0 ms', | ||
], | ||
'nanoseconds-rounded-down' => [ | ||
Event\Telemetry\Duration::fromSecondsAndNanoseconds( | ||
0, | ||
499_999 | ||
), | ||
'0 ms', | ||
], | ||
'nanoseconds-rounded-up' => [ | ||
Event\Telemetry\Duration::fromSecondsAndNanoseconds( | ||
0, | ||
500_000 | ||
), | ||
'1 ms', | ||
], | ||
'milliseconds-one' => [ | ||
Event\Telemetry\Duration::fromSecondsAndNanoseconds( | ||
0, | ||
1_000_000 | ||
), | ||
'1 ms', | ||
], | ||
'milliseconds-hundreds' => [ | ||
Event\Telemetry\Duration::fromSecondsAndNanoseconds( | ||
0, | ||
123 * 1_000_000 | ||
), | ||
'123 ms', | ||
], | ||
'seconds' => [ | ||
Event\Telemetry\Duration::fromSecondsAndNanoseconds( | ||
1, | ||
1_000_000 | ||
), | ||
'1,001 ms', | ||
], | ||
'thousands-of-seconds' => [ | ||
Event\Telemetry\Duration::fromSecondsAndNanoseconds( | ||
1_234, | ||
567_890_123 | ||
), | ||
'1,234,568 ms', | ||
], | ||
]; | ||
} | ||
} |