Skip to content

Commit 928aa13

Browse files
authored
Add tests to validate multiple file attachments
Signed-off-by: APaikens <shamanis@gmail.com>
1 parent bb6eec4 commit 928aa13

1 file changed

Lines changed: 137 additions & 0 deletions

File tree

test/Service/MailServiceTest.php

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
use PHPUnit\Framework\TestCase;
2020
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
2121
use Symfony\Component\Mailer\Transport\TransportInterface;
22+
use Symfony\Component\Mime\Part\DataPart;
23+
use Symfony\Component\Mime\Part\Multipart\MixedPart;
2224
use Symfony\Component\Mime\Part\TextPart;
2325

2426
class MailServiceTest extends TestCase
@@ -153,4 +155,139 @@ public function testMailResultCreatedFromException(): void
153155
$this->assertSame($customException, $mailResult->getException());
154156
$this->assertSame('Custom exception test', $mailResult->getMessage());
155157
}
158+
159+
public function testAttachFilesReturnsFalseWhenNoAttachments(): void
160+
{
161+
$this->message->html('Test body');
162+
$result = $this->mailService->attachFiles();
163+
$this->assertFalse($result);
164+
}
165+
166+
public function testAttachFilesCreatesFlatMixedPart(): void
167+
{
168+
$this->message->html('<p>Test</p>');
169+
170+
$this->mailService->addAttachment(
171+
$this->fileSystem->url() . '/data/mail/attachments/testPdfAttachment.pdf'
172+
);
173+
$this->mailService->addAttachment(
174+
$this->fileSystem->url() . '/data/mail/attachments/testXlsAttachment.xls'
175+
);
176+
177+
$this->mailService->attachFiles();
178+
179+
$body = $this->message->getBody();
180+
$this->assertInstanceOf(MixedPart::class, $body);
181+
182+
$parts = $body->getParts();
183+
// 1 TextPart (html) + 2 DataParts (attachments) = 3 parts at same level
184+
$this->assertCount(3, $parts);
185+
$this->assertInstanceOf(TextPart::class, $parts[0]);
186+
$this->assertInstanceOf(DataPart::class, $parts[1]);
187+
$this->assertInstanceOf(DataPart::class, $parts[2]);
188+
}
189+
190+
public function testAttachFilesSingleAttachmentIsFlat(): void
191+
{
192+
$this->message->html('<p>Single attachment</p>');
193+
194+
$this->mailService->addAttachment(
195+
$this->fileSystem->url() . '/data/mail/attachments/testPdfAttachment.pdf'
196+
);
197+
198+
$this->mailService->attachFiles();
199+
200+
$body = $this->message->getBody();
201+
$this->assertInstanceOf(MixedPart::class, $body);
202+
203+
$parts = $body->getParts();
204+
$this->assertCount(2, $parts);
205+
$this->assertInstanceOf(TextPart::class, $parts[0]);
206+
$this->assertInstanceOf(DataPart::class, $parts[1]);
207+
}
208+
209+
public function testAttachFilesSkipsNonExistentFiles(): void
210+
{
211+
$this->message->html('<p>Test</p>');
212+
213+
$this->mailService->addAttachment('/nonexistent/file.pdf');
214+
$this->mailService->addAttachment(
215+
$this->fileSystem->url() . '/data/mail/attachments/testPdfAttachment.pdf'
216+
);
217+
218+
$this->mailService->attachFiles();
219+
220+
$body = $this->message->getBody();
221+
$this->assertInstanceOf(MixedPart::class, $body);
222+
223+
$parts = $body->getParts();
224+
// only 1 valid attachment + the html body
225+
$this->assertCount(2, $parts);
226+
}
227+
228+
public function testAttachFilesPreservesCustomFilename(): void
229+
{
230+
$this->message->html('<p>Test</p>');
231+
232+
$this->mailService->addAttachment(
233+
$this->fileSystem->url() . '/data/mail/attachments/testPdfAttachment.pdf',
234+
'custom-ticket.pdf'
235+
);
236+
237+
$this->mailService->attachFiles();
238+
239+
$body = $this->message->getBody();
240+
$parts = $body->getParts();
241+
242+
$this->assertCount(2, $parts);
243+
$attachment = $parts[1];
244+
$this->assertInstanceOf(DataPart::class, $attachment);
245+
$this->assertSame('custom-ticket.pdf', $attachment->getFilename());
246+
}
247+
248+
public function testAttachFilesWithTextPartBody(): void
249+
{
250+
$textPart = new TextPart('<h1>HTML content</h1>', 'utf-8', 'html');
251+
$this->mailService->setBody($textPart);
252+
253+
$this->mailService->addAttachment(
254+
$this->fileSystem->url() . '/data/mail/attachments/testPdfAttachment.pdf'
255+
);
256+
$this->mailService->addAttachment(
257+
$this->fileSystem->url() . '/data/mail/attachments/testXlsAttachment.xls'
258+
);
259+
260+
$this->mailService->attachFiles();
261+
262+
$body = $this->message->getBody();
263+
$this->assertInstanceOf(MixedPart::class, $body);
264+
265+
$parts = $body->getParts();
266+
$this->assertCount(3, $parts);
267+
$this->assertInstanceOf(TextPart::class, $parts[0]);
268+
$this->assertInstanceOf(DataPart::class, $parts[1]);
269+
$this->assertInstanceOf(DataPart::class, $parts[2]);
270+
}
271+
272+
public function testAttachFilesNoNestedMixedParts(): void
273+
{
274+
$this->message->html('<p>Test nesting</p>');
275+
276+
$this->mailService->addAttachment(
277+
$this->fileSystem->url() . '/data/mail/attachments/testPdfAttachment.pdf'
278+
);
279+
$this->mailService->addAttachment(
280+
$this->fileSystem->url() . '/data/mail/attachments/testXlsAttachment.xls'
281+
);
282+
283+
$this->mailService->attachFiles();
284+
285+
$body = $this->message->getBody();
286+
$parts = $body->getParts();
287+
288+
// None of the children should be a MixedPart (no nesting)
289+
foreach ($parts as $part) {
290+
$this->assertNotInstanceOf(MixedPart::class, $part);
291+
}
292+
}
156293
}

0 commit comments

Comments
 (0)