Skip to content

Commit 89a0590

Browse files
committed
Handle Carbon 3 timestamp parsing and add regression coverage for timestamp handling
Bug: T426592
1 parent 0219964 commit 89a0590

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

app/Helper/MWTimestampHelper.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@ class MWTimestampHelper {
1515
private const MWTimestampFormat = 'YmdHis';
1616

1717
public static function getCarbonFromMWTimestamp(string $MWTimestamp): CarbonImmutable {
18-
$carbon = CarbonImmutable::createFromFormat(self::MWTimestampFormat, $MWTimestamp);
19-
if ($carbon === null) {
18+
try {
19+
$carbon = CarbonImmutable::createFromFormat(self::MWTimestampFormat, $MWTimestamp);
20+
} catch (InvalidFormatException $exception) {
21+
throw new InvalidFormatException('Unable to create Carbon object', 0, $exception);
22+
}
23+
24+
if (!$carbon instanceof CarbonImmutable) {
2025
throw new InvalidFormatException('Unable to create Carbon object');
2126
}
2227

tests/Helper/MWTimestampHelperTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public function testGetCarbonFromMWTimestamp() {
1919

2020
public function testGetCarbonFromMWTimestampWithInvalidTimestamp() {
2121
$this->expectException(InvalidFormatException::class);
22+
$this->expectExceptionMessage('Unable to create Carbon object');
2223

2324
$invalidMwTimestamp = 'invalid_timestamp';
2425
MWTimestampHelper::getCarbonFromMWTimestamp($invalidMwTimestamp);

0 commit comments

Comments
 (0)