Skip to content

Commit 6a001dc

Browse files
committed
Fix Time\Duration tests for lower PHP versions
1 parent 48909df commit 6a001dc

1 file changed

Lines changed: 59 additions & 55 deletions

File tree

tests/Time/DurationTest.php

Lines changed: 59 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,15 @@ public function testFromSecondsRejectsInvalidSeconds(int $seconds, string $excep
4848
Duration::fromSeconds($seconds);
4949
}
5050

51-
public static function invalidSecondsProvider(): iterable
51+
/**
52+
* @return array<non-empty-string, array{0: int, 1: class-string<\Throwable>}>
53+
*/
54+
public static function invalidSecondsProvider(): array
5255
{
53-
yield 'negative value' => [-1, \ValueError::class];
54-
yield 'too large value' => [9_223_372_036, TimeException::class];
56+
return [
57+
'negative value' => [-1, \ValueError::class],
58+
'too large value' => [9_223_372_036, TimeException::class],
59+
];
5560
}
5661

5762
/**
@@ -66,10 +71,15 @@ public function testFromSecondsRejectsInvalidNanoseconds(int $nanoseconds, strin
6671
Duration::fromSeconds(1, $nanoseconds);
6772
}
6873

74+
/**
75+
* @return array<non-empty-string, array{0: int, 1: class-string<\Throwable>}>
76+
*/
6977
public static function invalidNanosecondsProvider(): iterable
7078
{
71-
yield 'negative value' => [-1, \ValueError::class];
72-
yield 'too large value' => [1_000_000_000, TimeException::class];
79+
return [
80+
'negative value' => [-1, \ValueError::class],
81+
'too large value' => [1_000_000_000, TimeException::class],
82+
];
7383
}
7484

7585
public function testFromNanoseconds(): void
@@ -137,17 +147,19 @@ public function testParseIso8601(
137147
}
138148

139149
/**
140-
* @return iterable<array{0: non-empty-string, non-negative-int, non-negative-int, bool}>
150+
* @return array<non-empty-string, array{0: non-empty-string, 1: non-negative-int, 2: non-negative-int, 3: bool}>
141151
*/
142-
public static function validIso8601Provider(): iterable
152+
public static function validIso8601Provider(): array
143153
{
144-
yield ['PT1S', 1, 0, false];
145-
yield ['PT1.5S', 1, 500_000_000, false];
146-
yield ['PT1,5S', 1, 500_000_000, false];
147-
yield ['PT2M30S', 150, 0, false];
148-
yield ['PT1H2M3.123456789S', 3723, 123_456_789, false];
149-
yield ['-PT5S', 5, 0, true];
150-
yield ['-PT0S', 0, 0, false];
154+
return [
155+
'1 second' => ['PT1S', 1, 0, false],
156+
'1.5 seconds (dot)' => ['PT1.5S', 1, 500_000_000, false],
157+
'1.5 seconds (comma)' => ['PT1,5S', 1, 500_000_000, false],
158+
'2 minutes 30 seconds' => ['PT2M30S', 150, 0, false],
159+
'full precision' => ['PT1H2M3.123456789S', 3723, 123_456_789, false],
160+
'negative duration' => ['-PT5S', 5, 0, true],
161+
'negative zero normalizes to positive' => ['-PT0S', 0, 0, false],
162+
];
151163
}
152164

153165
/**
@@ -161,15 +173,17 @@ public function testRejectInvalidIso8601(string $spec): void
161173
}
162174

163175
/**
164-
* @return iterable<array{0: non-empty-string}>
176+
* @return array<non-empty-string, array{0: non-empty-string}>
165177
*/
166-
public static function invalidIso8601Provider(): iterable
178+
public static function invalidIso8601Provider(): array
167179
{
168-
yield ['P1D'];
169-
yield ['PT'];
170-
yield ['P'];
171-
yield ['foo'];
172-
yield ['PT1Y'];
180+
return [
181+
'date component only' => ['P1D'],
182+
'missing time value' => ['PT'],
183+
'empty duration' => ['P'],
184+
'not an ISO 8601 duration' => ['foo'],
185+
'unsupported year component' => ['PT1Y'],
186+
];
173187
}
174188

175189
public function testNegatePositiveDuration(): void
@@ -283,8 +297,10 @@ public function testDivideByRejectsInvalidDivisor(int $divisor): void
283297
*/
284298
public static function invalidDivisorProvider(): iterable
285299
{
286-
yield 'division by zero' => [0];
287-
yield 'division with negative factor' => [-1];
300+
return [
301+
'division by zero' => [0],
302+
'division with negative factor' => [-1],
303+
];
288304
}
289305

290306
public function testCompare(): void
@@ -344,38 +360,26 @@ public function test_it_rejects_invalid_iso8601_durations(string $specification)
344360
}
345361

346362
/**
347-
* @return iterable<non-empty-string, array{0: string}>
363+
* @return array<string, array{0: string}>
348364
*/
349-
public static function invalidIso8601Durations(): iterable
350-
{
351-
yield 'empty string' => [''];
352-
353-
yield 'invalid prefix' => ['1H'];
354-
355-
yield 'missing time designator' => ['P1H'];
356-
357-
yield 'missing value' => ['PT'];
358-
359-
yield 'unsupported years component' => ['P1Y'];
360-
361-
yield 'unsupported months component' => ['P1M'];
362-
363-
yield 'unsupported weeks component' => ['P1W'];
364-
365-
yield 'unsupported days component' => ['P1D'];
366-
367-
yield 'invalid fraction format' => ['PT1.S'];
368-
369-
yield 'invalid unit' => ['PT1X'];
370-
371-
yield 'invalid duplicated seconds' => ['PT1S2S'];
372-
373-
yield 'invalid duplicated fraction' => ['PT1.5.5S'];
374-
375-
yield 'hours overflow integer range' => ['PT2562047789H'];
376-
377-
yield 'combined components overflow integer range' => ['PT2562047788H59M59S'];
378-
379-
yield 'combined components exceeds integer range' => ['PT2562047788015215H59M59S'];
365+
public static function invalidIso8601Durations(): array
366+
{
367+
return [
368+
'empty string' => [''],
369+
'invalid prefix' => ['1H'],
370+
'missing time designator' => ['P1H'],
371+
'missing time value' => ['PT'],
372+
'unsupported years component' => ['P1Y'],
373+
'unsupported months component' => ['P1M'],
374+
'unsupported weeks component' => ['P1W'],
375+
'unsupported days component' => ['P1D'],
376+
'invalid fractional seconds format' => ['PT1.S'],
377+
'unknown time unit' => ['PT1X'],
378+
'duplicate seconds component' => ['PT1S2S'],
379+
'duplicate fractional seconds' => ['PT1.5.5S'],
380+
'hours component overflows integer range' => ['PT2562047789H'],
381+
'combined components overflow integer range' => ['PT2562047788H59M59S'],
382+
'combined components exceed supported range' => ['PT2562047788015215H59M59S'],
383+
];
380384
}
381385
}

0 commit comments

Comments
 (0)