Skip to content

Commit 6fce737

Browse files
committed
Enhancement: Format durations similar to phpunit/php-timer:^4.0.0
1 parent 3d5c9b9 commit 6fce737

File tree

3 files changed

+49
-38
lines changed

3 files changed

+49
-38
lines changed

src/Formatter/DefaultDurationFormatter.php

+9-12
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace Ergebnis\PHPUnit\SlowTestDetector\Formatter;
1515

1616
use Ergebnis\PHPUnit\SlowTestDetector\Duration;
17+
use SebastianBergmann\Timer;
1718

1819
/**
1920
* @internal
@@ -23,7 +24,8 @@
2324
final class DefaultDurationFormatter implements DurationFormatter
2425
{
2526
/**
26-
* @see https://github.com/sebastianbergmann/php-timer/blob/6.0.0/src/Duration.php
27+
* @see https://github.com/sebastianbergmann/php-timer/blob/4.0.0/src/Duration.php
28+
* /
2729
*/
2830
public function format(Duration $duration): string
2931
{
@@ -40,27 +42,22 @@ public function format(Duration $duration): string
4042

4143
$milliseconds = (int) ($durationInMilliseconds - $hoursInMilliseconds - $minutesInMilliseconds - $secondsInMilliseconds);
4244

45+
/**
46+
* @see https://github.com/sebastianbergmann/php-timer/blob/4.0.0/src/Duration.php
47+
*/
4348
if (0 < $hours) {
4449
return \sprintf(
45-
'%d:%02d:%02d.%03d',
50+
'%02d:%02d:%02d.%03d',
4651
$hours,
4752
$minutes,
4853
$seconds,
4954
$milliseconds
5055
);
5156
}
5257

53-
if (0 < $minutes) {
54-
return \sprintf(
55-
'%d:%02d.%03d',
56-
$minutes,
57-
$seconds,
58-
$milliseconds
59-
);
60-
}
61-
6258
return \sprintf(
63-
'%d.%03d',
59+
'%02d:%02d.%03d',
60+
$minutes,
6461
$seconds,
6562
$milliseconds
6663
);

test/Unit/Formatter/DefaultDurationFormatterTest.php

+20-6
Original file line numberDiff line numberDiff line change
@@ -47,35 +47,35 @@ public static function provideDurationAndFormattedDuration(): iterable
4747
0,
4848
0
4949
),
50-
'0.000',
50+
'00:00.000',
5151
],
5252
'milliseconds' => [
5353
Duration::fromSecondsAndNanoseconds(
5454
0,
5555
123999000
5656
),
57-
'0.123',
57+
'00:00.123',
5858
],
5959
'seconds-digits-one' => [
6060
Duration::fromSecondsAndNanoseconds(
6161
1,
6262
234456789
6363
),
64-
'1.234',
64+
'00:01.234',
6565
],
6666
'seconds-digits-two' => [
6767
Duration::fromSecondsAndNanoseconds(
6868
12,
6969
345678912
7070
),
71-
'12.345',
71+
'00:12.345',
7272
],
7373
'minutes-digits-one' => [
7474
Duration::fromSecondsAndNanoseconds(
7575
1 * 60 + 23,
7676
456789012
7777
),
78-
'1:23.456',
78+
'01:23.456',
7979
],
8080
'minutes-digits-two' => [
8181
Duration::fromSecondsAndNanoseconds(
@@ -89,7 +89,7 @@ public static function provideDurationAndFormattedDuration(): iterable
8989
60 * 60 + 23 * 60 + 45,
9090
567890123
9191
),
92-
'1:23:45.567',
92+
'01:23:45.567',
9393
],
9494
'hours-digits-two' => [
9595
Duration::fromSecondsAndNanoseconds(
@@ -98,6 +98,20 @@ public static function provideDurationAndFormattedDuration(): iterable
9898
),
9999
'12:34:56.789',
100100
],
101+
'hours-digits-two-nanoseconds-zero' => [
102+
Duration::fromSecondsAndNanoseconds(
103+
12 * 60 * 60 + 34 * 60 + 56,
104+
00
105+
),
106+
'12:34:56.000',
107+
],
108+
'hours-digits-two-seconds-zero' => [
109+
Duration::fromSecondsAndNanoseconds(
110+
12 * 60 * 60 + 34 * 60,
111+
00
112+
),
113+
'12:34:00.000',
114+
],
101115
];
102116

103117
foreach ($values as $key => list($duration, $formattedDuration)) {

test/Unit/Reporter/DefaultReporterTest.php

+20-20
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public static function provideExpectedReportMaximumCountAndSlowTestList(): itera
9191
<<<'TXT'
9292
Detected 1 test where the duration exceeded the maximum duration.
9393
94-
1. 0.300 (0.100) FooTest::test
94+
1. 00:00.300 (00:00.100) FooTest::test
9595
TXT
9696
,
9797
MaximumCount::fromCount(Count::fromInt(1)),
@@ -108,8 +108,8 @@ public static function provideExpectedReportMaximumCountAndSlowTestList(): itera
108108
<<<'TXT'
109109
Detected 2 tests where the duration exceeded the maximum duration.
110110
111-
1. 0.300 (0.100) FooTest::test
112-
2. 0.275 (0.100) BarTest::test
111+
1. 00:00.300 (00:00.100) FooTest::test
112+
2. 00:00.275 (00:00.100) BarTest::test
113113
TXT
114114
,
115115
MaximumCount::fromCount(Count::fromInt(2)),
@@ -132,9 +132,9 @@ public static function provideExpectedReportMaximumCountAndSlowTestList(): itera
132132
<<<'TXT'
133133
Detected 3 tests where the duration exceeded the maximum duration.
134134
135-
1. 0.300 (0.100) FooTest::test
136-
2. 0.275 (0.100) BarTest::test
137-
3. 0.250 (0.100) BazTest::test
135+
1. 00:00.300 (00:00.100) FooTest::test
136+
2. 00:00.275 (00:00.100) BarTest::test
137+
3. 00:00.250 (00:00.100) BazTest::test
138138
TXT
139139
,
140140
MaximumCount::fromCount(Count::fromInt(3)),
@@ -163,9 +163,9 @@ public static function provideExpectedReportMaximumCountAndSlowTestList(): itera
163163
<<<'TXT'
164164
Detected 3 tests where the duration exceeded the maximum duration.
165165
166-
1. 0.300 (0.100) FooTest::test
167-
2. 0.275 (0.100) BarTest::test
168-
3. 0.250 (0.100) BazTest::test
166+
1. 00:00.300 (00:00.100) FooTest::test
167+
2. 00:00.275 (00:00.100) BarTest::test
168+
3. 00:00.250 (00:00.100) BazTest::test
169169
TXT
170170
,
171171
MaximumCount::fromCount(Count::fromInt(3)),
@@ -195,15 +195,15 @@ public static function provideExpectedReportMaximumCountAndSlowTestList(): itera
195195
Detected 10 tests where the duration exceeded the maximum duration.
196196
197197
1. 20:50.000 (16:40.000) FooTest::test
198-
2. 9:35.000 ( 8:20.000) BarTest::test
199-
3. 0.250 ( 0.100) BazTest::test
200-
4. 0.200 ( 0.100) QuxTest::test
201-
5. 0.160 ( 0.100) QuuxTest::test
202-
6. 0.150 ( 0.100) CorgeTest::test
203-
7. 0.140 ( 0.100) GraultTest::test
204-
8. 0.130 ( 0.100) GarplyTest::test
205-
9. 0.120 ( 0.100) WaldoTest::test
206-
10. 0.110 ( 0.100) FredTest::test
198+
2. 09:35.000 (08:20.000) BarTest::test
199+
3. 00:00.250 (00:00.100) BazTest::test
200+
4. 00:00.200 (00:00.100) QuxTest::test
201+
5. 00:00.160 (00:00.100) QuuxTest::test
202+
6. 00:00.150 (00:00.100) CorgeTest::test
203+
7. 00:00.140 (00:00.100) GraultTest::test
204+
8. 00:00.130 (00:00.100) GarplyTest::test
205+
9. 00:00.120 (00:00.100) WaldoTest::test
206+
10. 00:00.110 (00:00.100) FredTest::test
207207
TXT
208208
,
209209
MaximumCount::fromCount(Count::fromInt(10)),
@@ -274,7 +274,7 @@ public static function provideExpectedReportMaximumCountAndSlowTestList(): itera
274274
<<<'TXT'
275275
Detected 2 tests where the duration exceeded the maximum duration.
276276
277-
1. 0.300 (0.100) FooTest::test
277+
1. 00:00.300 (00:00.100) FooTest::test
278278
279279
There is 1 additional slow test that is not listed here.
280280
TXT
@@ -299,7 +299,7 @@ public static function provideExpectedReportMaximumCountAndSlowTestList(): itera
299299
<<<'TXT'
300300
Detected 3 tests where the duration exceeded the maximum duration.
301301
302-
1. 0.300 (0.100) FooTest::test
302+
1. 00:00.300 (00:00.100) FooTest::test
303303
304304
There are 2 additional slow tests that are not listed here.
305305
TXT

0 commit comments

Comments
 (0)