Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Implement ToMillisecondsDurationFormatter #17

Merged
merged 1 commit into from
Jan 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
],
];
}
}