|
| 1 | +<?php |
| 2 | + |
| 3 | +use DirectoryTree\ImapEngine\Connection\Responses\Data\ListData; |
| 4 | +use DirectoryTree\ImapEngine\Connection\Responses\MessageResponseParser; |
| 5 | +use DirectoryTree\ImapEngine\Connection\Responses\UntaggedResponse; |
| 6 | +use DirectoryTree\ImapEngine\Connection\Tokens\Atom; |
| 7 | +use DirectoryTree\ImapEngine\Connection\Tokens\Literal; |
| 8 | + |
| 9 | +it('parses flags from untagged response', function () { |
| 10 | + $response = new UntaggedResponse([ |
| 11 | + new Atom('*'), // Untagged marker |
| 12 | + new Atom('1'), // Sequence number (example) |
| 13 | + new Atom('FETCH'), // Command type |
| 14 | + new ListData([ // Data list |
| 15 | + new Atom('UID'), |
| 16 | + new Atom('12345'), // The message UID |
| 17 | + new Atom('FLAGS'), |
| 18 | + new ListData([ // The list of flags |
| 19 | + new Atom('\Seen'), |
| 20 | + new Atom('\Answered'), |
| 21 | + new Atom('$Important'), // Example custom flag |
| 22 | + ]), |
| 23 | + ]), |
| 24 | + ]); |
| 25 | + |
| 26 | + $parsedFlags = MessageResponseParser::getFlags($response); |
| 27 | + |
| 28 | + expect($parsedFlags)->toBe([ |
| 29 | + '12345' => ['\Seen', '\Answered', '$Important'], |
| 30 | + ]); |
| 31 | +}); |
| 32 | + |
| 33 | +it('parses body header from untagged response', function () { |
| 34 | + $headerContent = "From: [email protected]\r\nTo: [email protected]\r\nSubject: Test Email Header\r\n"; |
| 35 | + |
| 36 | + $response = new UntaggedResponse([ |
| 37 | + new Atom('*'), // Untagged marker |
| 38 | + new Atom('2'), // Sequence number (example) |
| 39 | + new Atom('FETCH'), // Command type |
| 40 | + new ListData([ // Data list |
| 41 | + new Atom('UID'), |
| 42 | + new Atom('54321'), // The message UID |
| 43 | + new Atom('BODY'), |
| 44 | + new Atom('[HEADER]'), // Specifies header part |
| 45 | + new Literal($headerContent), // The header content as a literal |
| 46 | + ]), |
| 47 | + ]); |
| 48 | + |
| 49 | + $parsedHeader = MessageResponseParser::getBodyHeader($response); // |
| 50 | + |
| 51 | + expect($parsedHeader)->toBe(['54321' => $headerContent]); |
| 52 | +}); |
| 53 | + |
| 54 | +it('parses body text from untagged response', function () { |
| 55 | + $textContent = "This is the plain text body of the email.\r\nIt might have multiple lines.\r\n"; |
| 56 | + |
| 57 | + $response = new UntaggedResponse([ |
| 58 | + new Atom('*'), // Untagged marker |
| 59 | + new Atom('3'), // Sequence number (example) |
| 60 | + new Atom('FETCH'), // Command type |
| 61 | + new ListData([ // Data list |
| 62 | + new Atom('UID'), |
| 63 | + new Atom('98765'), // The message UID |
| 64 | + new Atom('BODY'), |
| 65 | + new Atom('[TEXT]'), // Specifies text part |
| 66 | + new Literal($textContent), // The text content as a literal |
| 67 | + ]), |
| 68 | + ]); |
| 69 | + |
| 70 | + $parsedText = MessageResponseParser::getBodyText($response); |
| 71 | + |
| 72 | + expect($parsedText)->toBe(['98765' => $textContent]); |
| 73 | +}); |
| 74 | + |
| 75 | +it('handles empty flags list correctly', function () { |
| 76 | + $response = new UntaggedResponse([ |
| 77 | + new Atom('*'), |
| 78 | + new Atom('4'), |
| 79 | + new Atom('FETCH'), |
| 80 | + new ListData([ |
| 81 | + new Atom('UID'), |
| 82 | + new Atom('11111'), |
| 83 | + new Atom('FLAGS'), |
| 84 | + new ListData([]), // Empty flags list |
| 85 | + ]), |
| 86 | + ]); |
| 87 | + |
| 88 | + $parsedFlags = MessageResponseParser::getFlags($response); |
| 89 | + |
| 90 | + expect($parsedFlags)->toBe(['11111' => []]); |
| 91 | +}); |
0 commit comments