Skip to content

Commit

Permalink
Merge pull request #17 from ergebnis/feature/to-milliseconds-duration…
Browse files Browse the repository at this point in the history
…-formatter

Enhancement: Implement ToMillisecondsDurationFormatter
  • Loading branch information
ergebnis-bot authored Jan 24, 2021
2 parents 69e0712 + 1442455 commit 3684509
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ For a full diff see [`7afa59c...main`][7afa59c...main].
* Added `SlowTestCollector` ([#8]), by [@localheinz]
* Added `Subscriber\TestPreparedSubscriber` ([#12]), by [@localheinz]
* Added `Subscriber\TestPassedSubscriber` ([#13]), by [@localheinz]
* Added `Formatter\ToMillisecondsDurationFormatter` ([#17]), by [@localheinz]

[7afa59c...main]: https://github.com/ergebnis/phpunit-slow-test-detector/compare/7afa59c...main

[#6]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/6
[#8]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/8
[#12]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/12
[#13]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/13
[#17]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/137

[@localheinz]: https://github.com/localheinz
35 changes: 35 additions & 0 deletions src/Formatter/ToMillisecondsDurationFormatter.php
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 test/Unit/Formatter/ToMillisecondsDurationFormatterTest.php
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',
],
];
}
}

0 comments on commit 3684509

Please sign in to comment.