Skip to content

Commit 2a9638f

Browse files
authored
Merge pull request #127 from DirectoryTree/bug-126
Fix null date handling in `HasParsedMessage`
2 parents 01ac9ed + 8a9458b commit 2a9638f

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/HasParsedMessage.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,15 @@ public function date(): ?CarbonInterface
3232
return null;
3333
}
3434

35-
if ($header instanceof DateHeader) {
36-
return Carbon::instance($header->getDateTime());
35+
if (! $header instanceof DateHeader) {
36+
return null;
37+
}
38+
39+
if (! $date = $header->getDateTime()) {
40+
return null;
3741
}
3842

39-
return null;
43+
return Carbon::instance($date);
4044
}
4145

4246
/**

tests/Unit/FileMessageTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,24 @@
2929
expect($message->flags())->toBe([]);
3030
});
3131

32+
test('it handles invalid date header that returns null from getDateTime', function () {
33+
$contents = <<<'EOT'
34+
From: "John Doe" <[email protected]>
35+
To: "Jane Roe" <[email protected]>
36+
Subject: Test Subject
37+
Date: Invalid Date String
38+
Message-ID: <[email protected]>
39+
MIME-Version: 1.0
40+
Content-Type: text/plain; charset="UTF-8"
41+
42+
Hello World
43+
EOT;
44+
45+
$message = new FileMessage($contents);
46+
47+
expect($message->date())->toBeNull();
48+
});
49+
3250
test('it can parse a standard EML message and read basic headers', function () {
3351
$contents = <<<'EOT'
3452
From: "John Doe" <[email protected]>

0 commit comments

Comments
 (0)