-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathFileMessageTest.php
More file actions
238 lines (184 loc) · 7.05 KB
/
FileMessageTest.php
File metadata and controls
238 lines (184 loc) · 7.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
use DirectoryTree\ImapEngine\Attachment;
use DirectoryTree\ImapEngine\Exceptions\RuntimeException;
use DirectoryTree\ImapEngine\FileMessage;
test('it throws an exception if the message is empty', function () {
$message = new FileMessage('');
$message->parse();
})->throws(RuntimeException::class);
test('it can parse a standard EML message and read basic headers', function () {
$contents = <<<'EOT'
From: "John Doe" <john@example.com>
To: "Jane Roe" <jane@example.com>
Subject: Test Subject
Date: Wed, 19 Feb 2025 12:34:56 -0500
Message-ID: <unique-message-id@server.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Hello World
EOT;
$message = new FileMessage($contents);
expect($message->subject())->toBe('Test Subject');
expect($message->messageId())->toBe('unique-message-id@server.example.com');
expect($message->date()->toDateTimeString())->toBe('2025-02-19 12:34:56');
expect($message->from()->email())->toBe('john@example.com');
expect($message->from()->name())->toBe('John Doe');
expect($message->to())->toHaveCount(1);
expect($message->to()[0]->email())->toBe('jane@example.com');
expect($message->to()[0]->name())->toBe('Jane Roe');
expect($message->text())->toBe('Hello World');
});
test('it can parse HTML content in a multipart message', function () {
$contents = <<<'EOT'
From: "John Doe" <john@example.com>
To: "Jane Roe" <jane@example.com>
Subject: HTML Email
Date: Wed, 19 Feb 2025 12:34:56 -0500
Message-ID: <html-message@server.example.com>
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="----BOUNDARY-ID----"
------BOUNDARY-ID----
Content-Type: text/plain; charset="UTF-8"
Hello Plain
------BOUNDARY-ID----
Content-Type: text/html; charset="UTF-8"
<html><body><p>Hello <strong>HTML</strong></p></body></html>
------BOUNDARY-ID------
EOT;
$message = new FileMessage($contents);
expect($message->text())->toBe("Hello Plain\n");
expect($message->html())->toBe('<html><body><p>Hello <strong>HTML</strong></p></body></html>');
});
test('it can parse attachments', function () {
// Simple example with one attachment
$contents = <<<'EOT'
From: "John Doe" <john@example.com>
To: "Jane Roe" <jane@example.com>
Subject: EML with Attachment
Date: Wed, 19 Feb 2025 12:34:56 -0500
Message-ID: <attachment-message@server.example.com>
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----BOUNDARY-ID----"
------BOUNDARY-ID----
Content-Type: text/plain; charset="UTF-8"
Hello with Attachment
------BOUNDARY-ID----
Content-Type: application/pdf; name="file.pdf"
Content-Disposition: attachment; filename="file.pdf"
Content-Transfer-Encoding: base64
JVBERi0xLjUKJeLjz9MKMyAwIG9iago8PC9MZW5ndGggNCAgIC9GaWx0ZXIvQXNjaWlIYXgg
ICAgPj5zdHJlYW0Kc3R1ZmYKZW5kc3RyZWFtCmVuZG9iajAK
------BOUNDARY-ID------
EOT;
$message = new FileMessage($contents);
expect($message->hasAttachments())->toBeTrue();
expect($message->attachmentCount())->toBe(1);
$attachments = $message->attachments();
expect($attachments)->toHaveCount(1);
$attachment = $attachments[0];
expect($attachment)->toBeInstanceOf(Attachment::class);
expect($attachment->filename())->toBe('file.pdf');
expect($attachment->contentType())->toBe('application/pdf');
});
test('it recognizes when there are no attachments', function () {
$contents = <<<'EOT'
From: "John Doe" <john@example.com>
To: "Jane Roe" <jane@example.com>
Subject: No Attachments
Date: Wed, 19 Feb 2025 12:34:56 -0500
Content-Type: text/plain; charset="UTF-8"
Just a plain text email without attachments.
EOT;
$message = new FileMessage($contents);
expect($message->hasAttachments())->toBeFalse();
expect($message->attachmentCount())->toBe(0);
expect($message->attachments())->toBe([]);
});
test('it can parse other header fields like IN-REPLY-TO', function () {
$contents = <<<'EOT'
From: "John Doe" <john@example.com>
In-Reply-To: <some-other-message@server.example.com>, <another-message@server.example.com>
Subject: In-Reply-To Check
Date: Wed, 19 Feb 2025 12:34:56 -0500
Content-Type: text/plain; charset="UTF-8"
Check the in-reply-to header
EOT;
$message = new FileMessage($contents);
$address = $message->inReplyTo();
expect($address)->not->toBeNull();
expect($address->email())->toBe('some-other-message@server.example.com');
});
test('it can be cast to a string via __toString()', function () {
$contents = <<<'EOT'
From: "John Doe" <john@example.com>
Subject: Stringable Test
Date: Wed, 19 Feb 2025 12:34:56 -0500
Content-Type: text/plain; charset="UTF-8"
Testing __toString
EOT;
$message = new FileMessage($contents);
expect((string) $message)->toBe($contents);
});
test('it can read inline attachment', function () {
$contents = <<<'EOT'
From: "Example Sender" <sender@example.com>
To: "Example Recipient" <recipient@example.com>
Subject: Test Email With Inline Image
MIME-Version: 1.0
Content-Type: multipart/related; boundary="BOUNDARY_STRING"
--BOUNDARY_STRING
Content-Type: text/html; charset=UTF-8
<html>
<body>
<p>This is a test email with an inline image:</p>
<img src="cid:inline-image-id" alt="Inline Image" />
</body>
</html>
--BOUNDARY_STRING
Content-Type: image/png; name="inline_image.png"
Content-Transfer-Encoding: base64
Content-ID: <inline-image-id>
Content-Disposition: inline; filename="inline_image.png"
iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8
z8BQDwABAgEA0xzY2QAAAABJRU5ErkJggg==
--BOUNDARY_STRING--
EOT;
$message = new FileMessage($contents);
$attachments = $message->attachments();
expect($attachments)->toHaveCount(1);
expect($attachments[0]->contentType())->toBe('image/png');
expect($attachments[0]->contentId())->toBe('inline-image-id');
expect($attachments[0]->filename())->toBe('inline_image.png');
});
test('it can determine if two messages are the same', function () {
$contents1 = <<<'EOT'
From: "John Doe" <john@example.com>
Subject: Test Subject
Date: Wed, 19 Feb 2025 12:34:56 -0500
Content-Type: text/plain; charset="UTF-8"
Test content
EOT;
$contents2 = <<<'EOT'
From: "John Doe" <john@example.com>
Subject: Test Subject
Date: Wed, 19 Feb 2025 12:34:56 -0500
Content-Type: text/plain; charset="UTF-8"
Test content
EOT;
$contents3 = <<<'EOT'
From: "John Doe" <john@example.com>
Subject: Different Subject
Date: Wed, 19 Feb 2025 12:34:56 -0500
Content-Type: text/plain; charset="UTF-8"
Different content
EOT;
$message1 = new FileMessage($contents1);
$message2 = new FileMessage($contents2);
$message3 = new FileMessage($contents3);
// Same content
expect($message1->is($message2))->toBeTrue();
// Different content
expect($message1->is($message3))->toBeFalse();
});