|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpList\Core\Tests\Unit\Domain\Messaging\Service; |
| 6 | + |
| 7 | +use PhpList\Core\Domain\Messaging\Service\EmailService; |
| 8 | +use PHPUnit\Framework\MockObject\MockObject; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | +use Symfony\Component\Mailer\MailerInterface; |
| 11 | +use Symfony\Component\Mime\Email; |
| 12 | + |
| 13 | +class EmailServiceTest extends TestCase |
| 14 | +{ |
| 15 | + private EmailService $emailService; |
| 16 | + private MailerInterface&MockObject $mailer; |
| 17 | + private string $defaultFromEmail = '[email protected]'; |
| 18 | + |
| 19 | + protected function setUp(): void |
| 20 | + { |
| 21 | + $this->mailer = $this->createMock(MailerInterface::class); |
| 22 | + $this->emailService = new EmailService($this->mailer, $this->defaultFromEmail); |
| 23 | + } |
| 24 | + |
| 25 | + public function testSendEmailWithDefaultFrom(): void |
| 26 | + { |
| 27 | + $email = (new Email()) |
| 28 | + |
| 29 | + ->subject('Test Subject') |
| 30 | + ->text('Test Content'); |
| 31 | + |
| 32 | + $this->mailer->expects($this->once()) |
| 33 | + ->method('send') |
| 34 | + ->with($this->callback(function (Email $sentEmail) { |
| 35 | + $fromAddresses = $sentEmail->getFrom(); |
| 36 | + $this->assertCount(1, $fromAddresses); |
| 37 | + $this->assertEquals($this->defaultFromEmail, $fromAddresses[0]->getAddress()); |
| 38 | + return true; |
| 39 | + })); |
| 40 | + |
| 41 | + $this->emailService->sendEmail($email); |
| 42 | + } |
| 43 | + |
| 44 | + public function testSendEmailWithCustomFrom(): void |
| 45 | + { |
| 46 | + $customFrom = '[email protected]'; |
| 47 | + $email = (new Email()) |
| 48 | + ->from($customFrom) |
| 49 | + |
| 50 | + ->subject('Test Subject') |
| 51 | + ->text('Test Content'); |
| 52 | + |
| 53 | + $this->mailer->expects($this->once()) |
| 54 | + ->method('send') |
| 55 | + ->with($this->callback(function (Email $sentEmail) use ($customFrom) { |
| 56 | + $fromAddresses = $sentEmail->getFrom(); |
| 57 | + $this->assertCount(1, $fromAddresses); |
| 58 | + $this->assertEquals($customFrom, $fromAddresses[0]->getAddress()); |
| 59 | + return true; |
| 60 | + })); |
| 61 | + |
| 62 | + $this->emailService->sendEmail($email); |
| 63 | + } |
| 64 | + |
| 65 | + public function testSendEmailWithCcBccAndReplyTo(): void |
| 66 | + { |
| 67 | + $email = (new Email()) |
| 68 | + |
| 69 | + ->subject('Test Subject') |
| 70 | + ->text('Test Content'); |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + $this->mailer->expects($this->once()) |
| 77 | + ->method('send') |
| 78 | + ->with($this->callback(function (Email $sentEmail) use ($cc, $bcc, $replyTo) { |
| 79 | + $ccAddresses = $sentEmail->getCc(); |
| 80 | + $bccAddresses = $sentEmail->getBcc(); |
| 81 | + $replyToAddresses = $sentEmail->getReplyTo(); |
| 82 | + |
| 83 | + $this->assertCount(1, $ccAddresses); |
| 84 | + $this->assertEquals($cc[0], $ccAddresses[0]->getAddress()); |
| 85 | + |
| 86 | + $this->assertCount(1, $bccAddresses); |
| 87 | + $this->assertEquals($bcc[0], $bccAddresses[0]->getAddress()); |
| 88 | + |
| 89 | + $this->assertCount(1, $replyToAddresses); |
| 90 | + $this->assertEquals($replyTo[0], $replyToAddresses[0]->getAddress()); |
| 91 | + |
| 92 | + return true; |
| 93 | + })); |
| 94 | + |
| 95 | + $this->emailService->sendEmail($email, $cc, $bcc, $replyTo); |
| 96 | + } |
| 97 | + |
| 98 | + public function testSendEmailWithAttachments(): void |
| 99 | + { |
| 100 | + $email = (new Email()) |
| 101 | + |
| 102 | + ->subject('Test Subject') |
| 103 | + ->text('Test Content'); |
| 104 | + |
| 105 | + $attachments = ['/path/to/attachment.pdf']; |
| 106 | + |
| 107 | + $this->mailer->expects($this->once()) |
| 108 | + ->method('send'); |
| 109 | + |
| 110 | + $this->emailService->sendEmail($email, [], [], [], $attachments); |
| 111 | + } |
| 112 | + |
| 113 | + public function testSendBulkEmail(): void |
| 114 | + { |
| 115 | + |
| 116 | + $subject = 'Bulk Test Subject'; |
| 117 | + $text = 'Bulk Test Content'; |
| 118 | + $html = '<p>Bulk Test HTML Content</p>'; |
| 119 | + |
| 120 | + $fromName = 'Sender Name'; |
| 121 | + |
| 122 | + $this->mailer->expects($this->exactly(count($recipients))) |
| 123 | + ->method('send') |
| 124 | + ->with($this->callback(function (Email $sentEmail) use ($subject, $text, $html, $from, $fromName) { |
| 125 | + $this->assertEquals($subject, $sentEmail->getSubject()); |
| 126 | + $this->assertEquals($text, $sentEmail->getTextBody()); |
| 127 | + $this->assertEquals($html, $sentEmail->getHtmlBody()); |
| 128 | + |
| 129 | + $fromAddresses = $sentEmail->getFrom(); |
| 130 | + $this->assertCount(1, $fromAddresses); |
| 131 | + $this->assertEquals($from, $fromAddresses[0]->getAddress()); |
| 132 | + $this->assertEquals($fromName, $fromAddresses[0]->getName()); |
| 133 | + |
| 134 | + return true; |
| 135 | + })); |
| 136 | + |
| 137 | + $this->emailService->sendBulkEmail($recipients, $subject, $text, $html, $from, $fromName); |
| 138 | + } |
| 139 | + |
| 140 | + public function testSendBulkEmailWithDefaultFrom(): void |
| 141 | + { |
| 142 | + |
| 143 | + $subject = 'Bulk Test Subject'; |
| 144 | + $text = 'Bulk Test Content'; |
| 145 | + |
| 146 | + $this->mailer->expects($this->exactly(count($recipients))) |
| 147 | + ->method('send') |
| 148 | + ->with($this->callback(function (Email $sentEmail) { |
| 149 | + $fromAddresses = $sentEmail->getFrom(); |
| 150 | + $this->assertCount(1, $fromAddresses); |
| 151 | + $this->assertEquals($this->defaultFromEmail, $fromAddresses[0]->getAddress()); |
| 152 | + return true; |
| 153 | + })); |
| 154 | + |
| 155 | + $this->emailService->sendBulkEmail($recipients, $subject, $text); |
| 156 | + } |
| 157 | +} |
0 commit comments