|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Mailer\Bridge\MailerSend\Tests\Transport; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\HttpClient\MockHttpClient; |
| 16 | +use Symfony\Component\HttpClient\Response\MockResponse; |
| 17 | +use Symfony\Component\Mailer\Bridge\MailerSend\Transport\MailerSendApiTransport; |
| 18 | +use Symfony\Component\Mailer\Exception\HttpTransportException; |
| 19 | +use Symfony\Component\Mime\Address; |
| 20 | +use Symfony\Component\Mime\Email; |
| 21 | +use Symfony\Component\Mime\Part\DataPart; |
| 22 | +use Symfony\Contracts\HttpClient\ResponseInterface; |
| 23 | + |
| 24 | +class MailerSendApiTransportTest extends TestCase |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @dataProvider getTransportData |
| 28 | + */ |
| 29 | + public function testToString(MailerSendApiTransport $transport, string $expected) |
| 30 | + { |
| 31 | + $this->assertSame($expected, (string) $transport); |
| 32 | + } |
| 33 | + |
| 34 | + public static function getTransportData() |
| 35 | + { |
| 36 | + yield [ |
| 37 | + new MailerSendApiTransport('ACCESS_KEY'), |
| 38 | + 'mailersend+api://api.mailersend.com', |
| 39 | + ]; |
| 40 | + |
| 41 | + yield [ |
| 42 | + (new MailerSendApiTransport('ACCESS_KEY'))->setHost('example.com'), |
| 43 | + 'mailersend+api://example.com', |
| 44 | + ]; |
| 45 | + |
| 46 | + yield [ |
| 47 | + (new MailerSendApiTransport('ACCESS_KEY'))->setHost('example.com')->setPort(99), |
| 48 | + 'mailersend+api://example.com:99', |
| 49 | + ]; |
| 50 | + } |
| 51 | + |
| 52 | + public function testSendBasicEmail() |
| 53 | + { |
| 54 | + $client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface { |
| 55 | + $this->assertSame('POST', $method); |
| 56 | + $this->assertSame('https://api.mailersend.com/v1/email', $url); |
| 57 | + |
| 58 | + $body = json_decode($options['body'], true); |
| 59 | + $this->assertSame('test_from@example.com', $body['from']['email']); |
| 60 | + $this->assertSame('Test from name', $body['from']['name']); |
| 61 | + $this->assertSame('test_to@example.com', $body['to'][0]['email']); |
| 62 | + $this->assertSame('Test to name', $body['to'][0]['name']); |
| 63 | + $this->assertSame('Test subject', $body['subject']); |
| 64 | + $this->assertSame('Lorem ipsum.', $body['text']); |
| 65 | + $this->assertSame('<html><body><p>Lorem ipsum.</p></body></html>', $body['html']); |
| 66 | + |
| 67 | + return new MockResponse('', [ |
| 68 | + 'http_code' => 202, |
| 69 | + 'response_headers' => ['x-message-id' => 'test_message_id'], |
| 70 | + ]); |
| 71 | + }); |
| 72 | + |
| 73 | + $transport = new MailerSendApiTransport('ACCESS_KEY', $client); |
| 74 | + |
| 75 | + $mail = new Email(); |
| 76 | + $mail->subject('Test subject') |
| 77 | + ->to(new Address('test_to@example.com', 'Test to name')) |
| 78 | + ->from(new Address('test_from@example.com', 'Test from name')) |
| 79 | + ->addCc('test_cc@example.com') |
| 80 | + ->addBcc('test_bcc@example.com') |
| 81 | + ->addReplyTo('test_reply_to@example.com') |
| 82 | + ->text('Lorem ipsum.') |
| 83 | + ->html('<html><body><p>Lorem ipsum.</p></body></html>'); |
| 84 | + |
| 85 | + $message = $transport->send($mail); |
| 86 | + |
| 87 | + $this->assertSame('test_message_id', $message->getMessageId()); |
| 88 | + } |
| 89 | + |
| 90 | + public function testSendEmailWithAttachment() |
| 91 | + { |
| 92 | + $client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface { |
| 93 | + $this->assertSame('POST', $method); |
| 94 | + $this->assertSame('https://api.mailersend.com/v1/email', $url); |
| 95 | + |
| 96 | + $body = json_decode($options['body'], true); |
| 97 | + |
| 98 | + $this->assertSame('content', base64_decode($body['attachments'][0]['content'])); |
| 99 | + $this->assertSame('attachment.txt', $body['attachments'][0]['filename']); |
| 100 | + $this->assertSame('inline content', base64_decode($body['attachments'][1]['content'])); |
| 101 | + $this->assertSame('inline.txt', $body['attachments'][1]['filename']); |
| 102 | + $this->assertSame('inline', $body['attachments'][1]['disposition']); |
| 103 | + $this->assertSame('test_cid@symfony', $body['attachments'][1]['id']); |
| 104 | + |
| 105 | + return new MockResponse('', [ |
| 106 | + 'http_code' => 202, |
| 107 | + 'response_headers' => ['x-message-id' => 'test_message_id'], |
| 108 | + ]); |
| 109 | + }); |
| 110 | + |
| 111 | + $transport = new MailerSendApiTransport('ACCESS_KEY', $client); |
| 112 | + |
| 113 | + $mail = new Email(); |
| 114 | + $mail->subject('Test subject') |
| 115 | + ->to(new Address('test_to@example.com', 'Test to name')) |
| 116 | + ->from(new Address('test_from@example.com', 'Test from name')) |
| 117 | + ->addCc('test_cc@example.com') |
| 118 | + ->addBcc('test_bcc@example.com') |
| 119 | + ->addReplyTo('test_reply_to@example.com') |
| 120 | + ->html('<html><body><p>Lorem ipsum.</p><img src="cid:test_cid@symfony"></body></html>') |
| 121 | + ->addPart(new DataPart('content', 'attachment.txt', 'text/plain')) |
| 122 | + ->addPart((new DataPart('inline content', 'inline.txt', 'text/plain'))->asInline()->setContentId('test_cid@symfony')); |
| 123 | + |
| 124 | + $message = $transport->send($mail); |
| 125 | + |
| 126 | + $this->assertSame('test_message_id', $message->getMessageId()); |
| 127 | + } |
| 128 | + |
| 129 | + public function testSendThrowsForErrorResponse() |
| 130 | + { |
| 131 | + $client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface { |
| 132 | + return new MockResponse(json_encode(['message' => 'i\'m a teapot']), [ |
| 133 | + 'http_code' => 418, |
| 134 | + ]); |
| 135 | + }); |
| 136 | + |
| 137 | + $transport = new MailerSendApiTransport('ACCESS_KEY', $client); |
| 138 | + |
| 139 | + $mail = new Email(); |
| 140 | + $mail->subject('Test subject') |
| 141 | + ->to(new Address('test_to@example.com', 'Test to name')) |
| 142 | + ->from(new Address('test_from@example.com', 'Test from name')) |
| 143 | + ->text('Lorem ipsum.'); |
| 144 | + |
| 145 | + $this->expectException(HttpTransportException::class); |
| 146 | + $this->expectExceptionMessage('Unable to send an email: i\'m a teapot (code 418).'); |
| 147 | + $transport->send($mail); |
| 148 | + } |
| 149 | + |
| 150 | + public function testSendThrowsForAllSuppressed() |
| 151 | + { |
| 152 | + $client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface { |
| 153 | + return new MockResponse(json_encode([ |
| 154 | + 'message' => 'There are some warnings for your request.', |
| 155 | + 'warnings' => [ |
| 156 | + [ |
| 157 | + 'type' => 'ALL_SUPPRESSED', |
| 158 | + ], |
| 159 | + ], |
| 160 | + ], \JSON_THROW_ON_ERROR), [ |
| 161 | + 'http_code' => 202, |
| 162 | + ]); |
| 163 | + }); |
| 164 | + |
| 165 | + $transport = new MailerSendApiTransport('ACCESS_KEY', $client); |
| 166 | + |
| 167 | + $mail = new Email(); |
| 168 | + $mail->subject('Test subject') |
| 169 | + ->to(new Address('test_to@example.com', 'Test to name')) |
| 170 | + ->from(new Address('test_from@example.com', 'Test from name')) |
| 171 | + ->text('Lorem ipsum.'); |
| 172 | + |
| 173 | + $this->expectException(HttpTransportException::class); |
| 174 | + $this->expectExceptionMessage('Unable to send an email: There are some warnings for your request.'); |
| 175 | + $transport->send($mail); |
| 176 | + } |
| 177 | + |
| 178 | + public function testSendThrowsForBadResponse() |
| 179 | + { |
| 180 | + $client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface { |
| 181 | + return new MockResponse('test', [ |
| 182 | + 'http_code' => 202, |
| 183 | + ]); |
| 184 | + }); |
| 185 | + |
| 186 | + $transport = new MailerSendApiTransport('ACCESS_KEY', $client); |
| 187 | + |
| 188 | + $mail = new Email(); |
| 189 | + $mail->subject('Test subject') |
| 190 | + ->to(new Address('test_to@example.com', 'Test to name')) |
| 191 | + ->from(new Address('test_from@example.com', 'Test from name')) |
| 192 | + ->text('Lorem ipsum.'); |
| 193 | + |
| 194 | + $this->expectException(HttpTransportException::class); |
| 195 | + $this->expectExceptionMessage('Unable to send an email: "test" (code 202).'); |
| 196 | + $transport->send($mail); |
| 197 | + } |
| 198 | +} |
0 commit comments