|
12 | 12 | use Laminas\Mail\Message; |
13 | 13 | use Laminas\Mime\Message as MimeMessage; |
14 | 14 | use Laminas\Mime\Mime; |
| 15 | +use Laminas\Mime\Part; |
15 | 16 | use Laminas\Mime\Part as MimePart; |
16 | 17 | use PHPUnit\Framework\TestCase; |
17 | 18 | use Smalot\PdfParser\Parser; |
18 | 19 | use stdClass; |
19 | 20 |
|
| 21 | +use function array_pop; |
20 | 22 | use function count; |
21 | 23 | use function date; |
22 | 24 | use function fclose; |
|
25 | 27 | use function fwrite; |
26 | 28 | use function implode; |
27 | 29 | use function sprintf; |
| 30 | +use function str_starts_with; |
28 | 31 | use function substr; |
29 | 32 | use function sys_get_temp_dir; |
30 | 33 | use function trim; |
@@ -888,12 +891,45 @@ public function testCanParseMultipartEmail(): void |
888 | 891 | '<div>This is a test email with 1 attachment.</div>', |
889 | 892 | trim($partOne->getParts()[1]->getRawContent()) |
890 | 893 | ); |
| 894 | + } |
| 895 | + |
| 896 | + public function testCanReturnPlainTextAndHTMLMessageBodyIfAvailable() |
| 897 | + { |
| 898 | + $raw = file_get_contents(__DIR__ . '/_files/mail_with_pdf_attachment.eml'); |
| 899 | + $message = Message::fromString($raw); |
| 900 | + $this->assertInstanceOf(Message::class, $message); |
| 901 | + $this->assertTrue($message->getBody()->isMultiPart()); |
| 902 | + $plainTextBody = $message->getPlainTextBodyPart(); |
| 903 | + $this->assertSame("This is a test email with 1 attachment.", trim($plainTextBody->getRawContent())); |
| 904 | + $htmlBody = $message->getHtmlBodyPart(); |
| 905 | + $this->assertSame("<div>This is a test email with 1 attachment.</div>", trim($htmlBody->getRawContent())); |
| 906 | + } |
| 907 | + |
| 908 | + public function testReturnsEmptyAttachmentsListWhenEmailHasNoAttachments() |
| 909 | + { |
| 910 | + $raw = file_get_contents(__DIR__ . '/_files/laminas-mail-19.eml'); |
| 911 | + $message = Message::fromString($raw); |
| 912 | + $this->assertInstanceOf(Message::class, $message); |
| 913 | + $this->assertTrue($message->getBody()->isMultiPart()); |
| 914 | + $this->assertEmpty($message->getAttachments()); |
| 915 | + } |
| 916 | + |
| 917 | + public function testCanRetrieveMessageAttachmentsWhenAttachmentsAreAvailable() |
| 918 | + { |
| 919 | + $raw = file_get_contents(__DIR__ . '/_files/mail_with_pdf_attachment.eml'); |
| 920 | + $message = Message::fromString($raw); |
| 921 | + $this->assertInstanceOf(Message::class, $message); |
| 922 | + $this->assertTrue($message->getBody()->isMultiPart()); |
891 | 923 |
|
892 | | - $attachmentPart = $parts[1]; |
| 924 | + $attachments = $message->getAttachments(); |
| 925 | + $this->assertCount(1, $attachments); |
| 926 | + /** @var Part $attachment */ |
| 927 | + $attachment = array_pop($attachments); |
| 928 | + $this->assertTrue(str_starts_with($attachment->getType(), "application/pdf")); |
893 | 929 |
|
894 | 930 | $tempFile = sprintf("%stemp.pdf", sys_get_temp_dir()); |
895 | 931 | $handle = fopen($tempFile, "w"); |
896 | | - fwrite($handle, $attachmentPart->getRawContent()); |
| 932 | + fwrite($handle, $attachment->getRawContent()); |
897 | 933 | fclose($handle); |
898 | 934 |
|
899 | 935 | $parser = new Parser(); |
|
0 commit comments