From 14424552e39518def707b87446afb7fa7a68b998 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Sun, 24 Jan 2021 13:04:30 +0100 Subject: [PATCH] Enhancement: Implement ToMillisecondsDurationFormatter --- CHANGELOG.md | 2 + .../ToMillisecondsDurationFormatter.php | 35 +++++++ .../ToMillisecondsDurationFormatterTest.php | 97 +++++++++++++++++++ 3 files changed, 134 insertions(+) create mode 100644 src/Formatter/ToMillisecondsDurationFormatter.php create mode 100644 test/Unit/Formatter/ToMillisecondsDurationFormatterTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index e533194e..734ddbba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ 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 @@ -21,5 +22,6 @@ For a full diff see [`7afa59c...main`][7afa59c...main]. [#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 diff --git a/src/Formatter/ToMillisecondsDurationFormatter.php b/src/Formatter/ToMillisecondsDurationFormatter.php new file mode 100644 index 00000000..0940d5c4 --- /dev/null +++ b/src/Formatter/ToMillisecondsDurationFormatter.php @@ -0,0 +1,35 @@ +seconds() * (10 ** 3) + + (int) \round($duration->nanoseconds() / (10 ** 6)); + } +} diff --git a/test/Unit/Formatter/ToMillisecondsDurationFormatterTest.php b/test/Unit/Formatter/ToMillisecondsDurationFormatterTest.php new file mode 100644 index 00000000..72f28eea --- /dev/null +++ b/test/Unit/Formatter/ToMillisecondsDurationFormatterTest.php @@ -0,0 +1,97 @@ +format($duration)); + } + + /** + * @return array + */ + 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', + ], + ]; + } +}