diff --git a/CHANGELOG.md b/CHANGELOG.md index 31833706..7cc7f322 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ For a full diff see [`1.0.0...main`][1.0.0...main]. - Allowed configuring the maximum duration via `maximum-duration` parameter ([#212]), by [@localheinz] - Allowed configuring the maximum count via `maximum-count` parameter ([#217]), by [@localheinz] - Marked classes and interfaces as internal ([#219]), by [@localheinz] +- Brought duration formatting in line with `phpunit/php-timer` ([#220]), by [@localheinz] ### Fixed @@ -84,5 +85,6 @@ For a full diff see [`7afa59c...1.0.0`][7afa59c...1.0.0]. [#217]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/217 [#218]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/218 [#219]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/219 +[#220]: https://github.com/ergebnis/phpunit-slow-test-detector/pull/220 [@localheinz]: https://github.com/localheinz diff --git a/README.md b/README.md index cc3be544..5505d6de 100644 --- a/README.md +++ b/README.md @@ -109,7 +109,7 @@ vendor/bin/phpunit When the extension has detected slow tests, it will report them: -```sh +```txt PHPUnit 10.0.0 by Sebastian Bergmann and contributors. Runtime: PHP 8.1.0 diff --git a/psalm-baseline.xml b/psalm-baseline.xml index f841c48c..76a5b48e 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -5,6 +5,16 @@ Extension + + + $duration->seconds() * 1000 + $duration->nanoseconds() / 1000000 + $durationInMilliseconds - $hoursInMilliseconds + $durationInMilliseconds - $hoursInMilliseconds + $durationInMilliseconds / 60 + $durationInMilliseconds / 60 + ($durationInMilliseconds - $hoursInMilliseconds - $minutesInMilliseconds) / 1000 + + Metadata\Annotation\Parser\Registry::getInstance() @@ -76,9 +86,9 @@ InvalidMaximumDurationTest - + - ToMillisecondsDurationFormatterTest + DefaultDurationFormatterTest diff --git a/src/Extension.php b/src/Extension.php index f7d6a903..3dce51e4 100644 --- a/src/Extension.php +++ b/src/Extension.php @@ -38,7 +38,7 @@ public function bootstrap( $collector = new Collector\DefaultCollector(); $reporter = new Reporter\DefaultReporter( - new Formatter\ToMillisecondsDurationFormatter(), + new Formatter\DefaultDurationFormatter(), $maximumDuration, $maximumCount, ); diff --git a/src/Formatter/DefaultDurationFormatter.php b/src/Formatter/DefaultDurationFormatter.php new file mode 100644 index 00000000..19ce335e --- /dev/null +++ b/src/Formatter/DefaultDurationFormatter.php @@ -0,0 +1,67 @@ +seconds() * 1000 + $duration->nanoseconds() / 1000000; + + $hours = (int) \floor($durationInMilliseconds / 60 / 60 / 1000); + $hoursInMilliseconds = $hours * 60 * 60 * 1000; + + $minutes = ((int) \floor($durationInMilliseconds / 60 / 1000)) % 60; + $minutesInMilliseconds = $minutes * 60 * 1000; + + $seconds = (int) \floor(($durationInMilliseconds - $hoursInMilliseconds - $minutesInMilliseconds) / 1000); + $secondsInMilliseconds = $seconds * 1000; + + $milliseconds = (int) ($durationInMilliseconds - $hoursInMilliseconds - $minutesInMilliseconds - $secondsInMilliseconds); + + if (0 < $hours) { + return \sprintf( + '%d:%02d:%02d.%03d', + $hours, + $minutes, + $seconds, + $milliseconds, + ); + } + + if (0 < $minutes) { + return \sprintf( + '%d:%02d.%03d', + $minutes, + $seconds, + $milliseconds, + ); + } + + return \sprintf( + '%d.%03d', + $seconds, + $milliseconds, + ); + } +} diff --git a/src/Formatter/ToMillisecondsDurationFormatter.php b/src/Formatter/ToMillisecondsDurationFormatter.php deleted file mode 100644 index c390dac7..00000000 --- a/src/Formatter/ToMillisecondsDurationFormatter.php +++ /dev/null @@ -1,34 +0,0 @@ -seconds() * (10 ** 3) + (int) \round($duration->nanoseconds() / (10 ** 6)); - - return \sprintf( - '%s ms', - \number_format($milliseconds), - ); - } -} diff --git a/test/EndToEnd/Default/test.phpt b/test/EndToEnd/Default/test.phpt index 5572bbce..3f78034e 100644 --- a/test/EndToEnd/Default/test.phpt +++ b/test/EndToEnd/Default/test.phpt @@ -25,16 +25,16 @@ Random Seed: %s Detected 11 tests that took longer than expected. -1,6%s ms (500 ms) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Default\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#9 -1,5%s ms (500 ms) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Default\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#8 -1,4%s ms (500 ms) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Default\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#7 -1,3%s ms (500 ms) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Default\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#6 -1,2%s ms (500 ms) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Default\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#5 -1,1%s ms (500 ms) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Default\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#4 -1,0%s ms (500 ms) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Default\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#3 - 9%s ms (500 ms) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Default\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#2 - 8%s ms (500 ms) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Default\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#1 - 7%s ms (500 ms) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Default\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#0 +1.6%s (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Default\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#9 +1.5%s (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Default\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#8 +1.4%s (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Default\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#7 +1.3%s (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Default\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#6 +1.2%s (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Default\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#5 +1.1%s (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Default\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#4 +1.0%s (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Default\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#3 +0.9%s (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Default\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#2 +0.8%s (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Default\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#1 +0.7%s (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Default\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#0 There is one additional slow test that is not listed here. diff --git a/test/EndToEnd/MaximumCount/Five/test.phpt b/test/EndToEnd/MaximumCount/Five/test.phpt index f208696d..5d268723 100644 --- a/test/EndToEnd/MaximumCount/Five/test.phpt +++ b/test/EndToEnd/MaximumCount/Five/test.phpt @@ -25,11 +25,11 @@ Random Seed: %s Detected 7 tests that took longer than expected. -1,2%s ms (500 ms) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\MaximumCount\Five\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#5 -1,1%s ms (500 ms) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\MaximumCount\Five\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#4 -1,0%s ms (500 ms) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\MaximumCount\Five\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#3 - 9%s ms (500 ms) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\MaximumCount\Five\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#2 - 8%s ms (500 ms) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\MaximumCount\Five\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#1 +1.2%s (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\MaximumCount\Five\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#5 +1.1%s (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\MaximumCount\Five\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#4 +1.0%s (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\MaximumCount\Five\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#3 +0.9%s (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\MaximumCount\Five\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#2 +0.8%s (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\MaximumCount\Five\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider#1 There are 2 additional slow tests that are not listed here. diff --git a/test/EndToEnd/MaximumDuration/Fifty/test.phpt b/test/EndToEnd/MaximumDuration/Fifty/test.phpt index d6f3c1cb..a0aac08c 100644 --- a/test/EndToEnd/MaximumDuration/Fifty/test.phpt +++ b/test/EndToEnd/MaximumDuration/Fifty/test.phpt @@ -25,11 +25,11 @@ Random Seed: %s Detected 5 tests that took longer than expected. -1%s ms (50 ms) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\MaximumDuration\Fifty\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromSlowThresholdAnnotation -1%s ms (50 ms) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\MaximumDuration\Fifty\SleeperTest::testSleeperSleepsWithDocBlockWithSlowThresholdAnnotationWhereValueIsNotAnInt - 9%s ms (50 ms) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\MaximumDuration\Fifty\SleeperTest::testSleeperSleepsShorterThanMaximumDurationFromSlowThresholdAnnotation - 8%s ms (50 ms) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\MaximumDuration\Fifty\SleeperTest::testSleeperSleepsWithDocBlockWithoutSlowThresholdAnnotation - 6%s ms (50 ms) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\MaximumDuration\Fifty\SleeperTest::testSleeperSleepsJustAboveDefaultMaximumDuration +0.1%s (0.050) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\MaximumDuration\Fifty\SleeperTest::testSleeperSleepsLongerThanMaximumDurationFromSlowThresholdAnnotation +0.1%s (0.050) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\MaximumDuration\Fifty\SleeperTest::testSleeperSleepsWithDocBlockWithSlowThresholdAnnotationWhereValueIsNotAnInt +0.0%s (0.050) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\MaximumDuration\Fifty\SleeperTest::testSleeperSleepsShorterThanMaximumDurationFromSlowThresholdAnnotation +0.0%s (0.050) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\MaximumDuration\Fifty\SleeperTest::testSleeperSleepsWithDocBlockWithoutSlowThresholdAnnotation +0.0%s (0.050) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\MaximumDuration\Fifty\SleeperTest::testSleeperSleepsJustAboveDefaultMaximumDuration Time: %s, Memory: %s diff --git a/test/Unit/Formatter/ToMillisecondsDurationFormatterTest.php b/test/Unit/Formatter/DefaultDurationFormatterTest.php similarity index 61% rename from test/Unit/Formatter/ToMillisecondsDurationFormatterTest.php rename to test/Unit/Formatter/DefaultDurationFormatterTest.php index 88d953fe..e48cd1e3 100644 --- a/test/Unit/Formatter/ToMillisecondsDurationFormatterTest.php +++ b/test/Unit/Formatter/DefaultDurationFormatterTest.php @@ -21,9 +21,9 @@ /** * @internal * - * @covers \Ergebnis\PHPUnit\SlowTestDetector\Formatter\ToMillisecondsDurationFormatter + * @covers \Ergebnis\PHPUnit\SlowTestDetector\Formatter\DefaultDurationFormatter */ -final class ToMillisecondsDurationFormatterTest extends Framework\TestCase +final class DefaultDurationFormatterTest extends Framework\TestCase { use Test\Util\Helper; @@ -34,7 +34,7 @@ public function testFormatFormats( Event\Telemetry\Duration $duration, string $formattedDuration, ): void { - $formatter = new Formatter\ToMillisecondsDurationFormatter(); + $formatter = new Formatter\DefaultDurationFormatter(); self::assertSame($formattedDuration, $formatter->format($duration)); } @@ -50,49 +50,56 @@ public static function provideDurationAndFormattedDuration(): array 0, 0, ), - '0 ms', + '0.000', ], - 'nanoseconds-rounded-down' => [ + 'milliseconds' => [ Event\Telemetry\Duration::fromSecondsAndNanoseconds( 0, - 499_999, + 123_999_000, ), - '0 ms', + '0.123', ], - 'nanoseconds-rounded-up' => [ + 'seconds-digits-one' => [ Event\Telemetry\Duration::fromSecondsAndNanoseconds( - 0, - 500_000, + 1, + 234_456_789, ), - '1 ms', + '1.234', ], - 'milliseconds-one' => [ + 'seconds-digits-two' => [ Event\Telemetry\Duration::fromSecondsAndNanoseconds( - 0, - 1_000_000, + 12, + 345_678_912, ), - '1 ms', + '12.345', ], - 'milliseconds-hundreds' => [ + 'minutes-digits-one' => [ Event\Telemetry\Duration::fromSecondsAndNanoseconds( - 0, - 123 * 1_000_000, + 1 * 60 + 23, + 456_789_012, ), - '123 ms', + '1:23.456', ], - 'seconds' => [ + 'minutes-digits-two' => [ Event\Telemetry\Duration::fromSecondsAndNanoseconds( - 1, - 1_000_000, + 12 * 60 + 34, + 567_890_123, ), - '1,001 ms', + '12:34.567', ], - 'thousands-of-seconds' => [ + 'hours-digits-one' => [ Event\Telemetry\Duration::fromSecondsAndNanoseconds( - 1_234, + 60 * 60 + 23 * 60 + 45, 567_890_123, ), - '1,234,568 ms', + '1:23:45.567', + ], + 'hours-digits-two' => [ + Event\Telemetry\Duration::fromSecondsAndNanoseconds( + 12 * 60 * 60 + 34 * 60 + 56, + 789_012_345, + ), + '12:34:56.789', ], ]; }