Skip to content

Commit 5e1a874

Browse files
committed
DateInterval converts null value into empty interval
1 parent 475daa2 commit 5e1a874

File tree

4 files changed

+29
-24
lines changed

4 files changed

+29
-24
lines changed

src/Internal/Marshaller/Type/DurationJsonType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function parse($value, $current): CarbonInterval
8181
}
8282

8383
if ($value === null) {
84-
return CarbonInterval::create();
84+
return CarbonInterval::create(0);
8585
}
8686

8787
return DateInterval::parse($value, $this->fallbackFormat);

src/Internal/Support/DateInterval.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
/**
1818
* @psalm-type DateIntervalFormat = DateInterval::FORMAT_*
19-
* @psalm-type DateIntervalValue = string | int | float | \DateInterval | Duration
19+
* @psalm-type DateIntervalValue = string | int | float | \DateInterval | Duration | null
2020
*/
2121
final class DateInterval
2222
{
@@ -115,6 +115,10 @@ public static function parse($interval, string $format = self::FORMAT_MILLISECON
115115
$interval->getSeconds() * 1e6 + $interval->getNanos() / 1e3,
116116
self::FORMAT_MICROSECONDS,
117117
);
118+
119+
case $interval === null:
120+
return CarbonInterval::create(0);
121+
118122
default:
119123
throw new \InvalidArgumentException(self::ERROR_INVALID_DATETIME);
120124
}

tests/Acceptance/Harness/ContinueAsNew/ContinueAsSameTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static function check(
2727
args: [INPUT_DATA],
2828
memo: [MEMO_KEY => MEMO_VALUE],
2929
)]
30-
WorkflowStubInterface $stub
30+
WorkflowStubInterface $stub,
3131
): void {
3232
self::assertSame(INPUT_DATA, $stub->getResult());
3333
# Workflow ID does not change after continue as new

tests/Unit/Internal/Support/DateIntervalTestCase.php

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,31 @@
1212
#[CoversClass(DateInterval::class)]
1313
final class DateIntervalTestCase extends TestCase
1414
{
15+
public static function provideValuesToParse(): iterable
16+
{
17+
yield [1, DateInterval::FORMAT_MICROSECONDS, 1, '0/0/0/0'];
18+
yield [1, DateInterval::FORMAT_MILLISECONDS, 1_000, '0/0/0/0'];
19+
yield [0.25, DateInterval::FORMAT_SECONDS, 250_000, '0/0/0/0'];
20+
yield [1, DateInterval::FORMAT_SECONDS, 1_000_000, '0/0/0/1'];
21+
yield [1.25, DateInterval::FORMAT_SECONDS, 1_250_000, '0/0/0/1'];
22+
yield [1.8, DateInterval::FORMAT_SECONDS, 1_800_000, '0/0/0/1'];
23+
yield [1, DateInterval::FORMAT_MINUTES, 60_000_000, '0/0/1/0'];
24+
yield [1.5, DateInterval::FORMAT_MINUTES, 90_000_000, '0/0/1/30'];
25+
yield [1, DateInterval::FORMAT_HOURS, 3_600_000_000, '0/1/0/0'];
26+
yield [1, DateInterval::FORMAT_DAYS, 86_400_000_000, '1/0/0/0'];
27+
yield [1, DateInterval::FORMAT_WEEKS, 604_800_000_000, '7/0/0/0'];
28+
yield [(0.1 + 0.7) * 10.0, DateInterval::FORMAT_SECONDS, 8_000_000, '0/0/0/8'];
29+
yield [(0.1 + 0.7) * 10.0, DateInterval::FORMAT_DAYS, 691200000000, '8/0/0/0'];
30+
yield [(0.1 + 0.7) * 10.0, DateInterval::FORMAT_WEEKS, 4838400000000, '56/0/0/0'];
31+
yield [null, DateInterval::FORMAT_MILLISECONDS, 0, '0/0/0/0'];
32+
}
33+
1534
#[DataProvider('provideValuesToParse')]
1635
public function testParse(mixed $value, string $format, int $microseconds, string $formatted): void
1736
{
1837
$i = DateInterval::parse($value, $format);
1938

20-
self::assertSame($microseconds, (int)$i->totalMicroseconds);
39+
self::assertSame($microseconds, (int) $i->totalMicroseconds);
2140
self::assertSame($formatted, $i->format('%d/%h/%i/%s'));
2241
if ($i->totalMicroseconds > 1_000_000) {
2342
self::assertGreaterThan(0, $i->totalSeconds);
@@ -28,7 +47,7 @@ public function testParseAndFormat(): void
2847
{
2948
$i = DateInterval::parse(6_200, DateInterval::FORMAT_MILLISECONDS);
3049

31-
$this->assertSame(6_200_000, (int)$i->totalMicroseconds);
50+
$this->assertSame(6_200_000, (int) $i->totalMicroseconds);
3251
self::assertSame('0/0/0/6', $i->format('%y/%h/%i/%s'));
3352
}
3453

@@ -40,25 +59,7 @@ public function testParseFromDuration(): void
4059

4160
$i = DateInterval::parse($duration);
4261

43-
self::assertSame(5124, (int)$i->totalSeconds);
62+
self::assertSame(5124, (int) $i->totalSeconds);
4463
self::assertSame(123_456, $i->microseconds);
4564
}
46-
47-
public static function provideValuesToParse(): iterable
48-
{
49-
yield [1, DateInterval::FORMAT_MICROSECONDS, 1, '0/0/0/0'];
50-
yield [1, DateInterval::FORMAT_MILLISECONDS, 1_000, '0/0/0/0'];
51-
yield [0.25, DateInterval::FORMAT_SECONDS, 250_000, '0/0/0/0'];
52-
yield [1, DateInterval::FORMAT_SECONDS, 1_000_000, '0/0/0/1'];
53-
yield [1.25, DateInterval::FORMAT_SECONDS, 1_250_000, '0/0/0/1'];
54-
yield [1.8, DateInterval::FORMAT_SECONDS, 1_800_000, '0/0/0/1'];
55-
yield [1, DateInterval::FORMAT_MINUTES, 60_000_000, '0/0/1/0'];
56-
yield [1.5, DateInterval::FORMAT_MINUTES, 90_000_000, '0/0/1/30'];
57-
yield [1, DateInterval::FORMAT_HOURS, 3_600_000_000, '0/1/0/0'];
58-
yield [1, DateInterval::FORMAT_DAYS, 86_400_000_000, '1/0/0/0'];
59-
yield [1, DateInterval::FORMAT_WEEKS, 604_800_000_000, '7/0/0/0'];
60-
yield [(0.1 + 0.7) * 10.0, DateInterval::FORMAT_SECONDS, 8_000_000, '0/0/0/8'];
61-
yield [(0.1 + 0.7) * 10.0, DateInterval::FORMAT_DAYS, 691200000000, '8/0/0/0'];
62-
yield [(0.1 + 0.7) * 10.0, DateInterval::FORMAT_WEEKS, 4838400000000, '56/0/0/0'];
63-
}
6465
}

0 commit comments

Comments
 (0)