|
464 | 464 | expect($message->hasBodyStructure())->toBeFalse(); |
465 | 465 | expect($message->html(lazy: true))->toBe('<p>Hello World!</p>'); |
466 | 466 | }); |
| 467 | + |
| 468 | +test('it lazy loads attachments from body structure', function () { |
| 469 | + $mailbox = Mailbox::make([ |
| 470 | + 'username' => 'foo', |
| 471 | + 'password' => 'bar', |
| 472 | + ]); |
| 473 | + |
| 474 | + $encodedContent = base64_encode('Hello World!'); |
| 475 | + |
| 476 | + $mailbox->connect(ImapConnection::fake([ |
| 477 | + '* OK Welcome to IMAP', |
| 478 | + 'TAG1 OK Logged in', |
| 479 | + '* 1 FETCH (UID 1 BODY[2] {'.(strlen($encodedContent) + 2).'}', |
| 480 | + $encodedContent, |
| 481 | + ')', |
| 482 | + 'TAG2 OK FETCH completed', |
| 483 | + ])); |
| 484 | + |
| 485 | + $folder = new Folder($mailbox, 'INBOX', [], '/'); |
| 486 | + |
| 487 | + // Multipart mixed with text/plain at part 1 and PDF attachment at part 2 |
| 488 | + $bodyStructureData = parseBodyStructureResponse( |
| 489 | + '* 1 FETCH (BODYSTRUCTURE (("text" "plain" ("charset" "utf-8") NIL NIL "7bit" 100 5 NIL NIL NIL) ("application" "pdf" ("name" "document.pdf") NIL NIL "base64" 5000 NIL ("attachment" ("filename" "document.pdf")) NIL NIL) "mixed" ("boundary" "abc") NIL NIL) UID 1)' |
| 490 | + ); |
| 491 | + |
| 492 | + $message = new Message($folder, 1, [], 'From: test@example.com', '', null, $bodyStructureData); |
| 493 | + |
| 494 | + expect($message->hasBody())->toBeFalse(); |
| 495 | + expect($message->hasBodyStructure())->toBeTrue(); |
| 496 | + |
| 497 | + $attachments = $message->attachments(lazy: true); |
| 498 | + |
| 499 | + expect($attachments)->toHaveCount(1); |
| 500 | + expect($attachments[0]->filename())->toBe('document.pdf'); |
| 501 | + expect($attachments[0]->contentType())->toBe('application/pdf'); |
| 502 | + |
| 503 | + // Content is fetched lazily when contents() is called |
| 504 | + expect($attachments[0]->contents())->toBe('Hello World!'); |
| 505 | +}); |
0 commit comments