|
| 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\Mailtrap\Tests\Transport; |
| 13 | + |
| 14 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | +use Symfony\Component\HttpClient\MockHttpClient; |
| 17 | +use Symfony\Component\HttpClient\Response\JsonMockResponse; |
| 18 | +use Symfony\Component\Mailer\Bridge\Mailtrap\Transport\MailtrapApiSandboxTransport; |
| 19 | +use Symfony\Component\Mailer\Envelope; |
| 20 | +use Symfony\Component\Mailer\Exception\HttpTransportException; |
| 21 | +use Symfony\Component\Mailer\Exception\TransportException; |
| 22 | +use Symfony\Component\Mailer\Header\MetadataHeader; |
| 23 | +use Symfony\Component\Mailer\Header\TagHeader; |
| 24 | +use Symfony\Component\Mime\Address; |
| 25 | +use Symfony\Component\Mime\Email; |
| 26 | +use Symfony\Contracts\HttpClient\ResponseInterface; |
| 27 | + |
| 28 | +class MailtrapApiSandboxTransportTest extends TestCase |
| 29 | +{ |
| 30 | + #[DataProvider('getTransportData')] |
| 31 | + public function testToString(MailtrapApiSandboxTransport $transport, string $expected) |
| 32 | + { |
| 33 | + $this->assertSame($expected, (string) $transport); |
| 34 | + } |
| 35 | + |
| 36 | + public static function getTransportData(): array |
| 37 | + { |
| 38 | + return [ |
| 39 | + [ |
| 40 | + new MailtrapApiSandboxTransport('KEY', 123456), |
| 41 | + 'mailtrap+sandbox://sandbox.api.mailtrap.io/?inboxId=123456', |
| 42 | + ], |
| 43 | + [ |
| 44 | + (new MailtrapApiSandboxTransport('KEY', 123456))->setHost('example.com'), |
| 45 | + 'mailtrap+sandbox://example.com/?inboxId=123456', |
| 46 | + ], |
| 47 | + [ |
| 48 | + (new MailtrapApiSandboxTransport('KEY', 123456))->setHost('example.com')->setPort(99), |
| 49 | + 'mailtrap+sandbox://example.com:99/?inboxId=123456', |
| 50 | + ], |
| 51 | + [ |
| 52 | + new MailtrapApiSandboxTransport('KEY', 123456), |
| 53 | + 'mailtrap+sandbox://sandbox.api.mailtrap.io/?inboxId=123456', |
| 54 | + ], |
| 55 | + ]; |
| 56 | + } |
| 57 | + |
| 58 | + public function testSend() |
| 59 | + { |
| 60 | + $client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface { |
| 61 | + $this->assertSame('POST', $method); |
| 62 | + $this->assertSame('https://sandbox.api.mailtrap.io/api/send/123456', $url); |
| 63 | + |
| 64 | + $body = json_decode($options['body'], true); |
| 65 | + $this->assertSame(['email' => 'fabpot@symfony.com', 'name' => 'Fabien'], $body['from']); |
| 66 | + $this->assertSame([['email' => 'kevin@symfony.com', 'name' => 'Kevin']], $body['to']); |
| 67 | + $this->assertSame('Hello!', $body['subject']); |
| 68 | + $this->assertSame('Hello There!', $body['text']); |
| 69 | + |
| 70 | + return new JsonMockResponse([], [ |
| 71 | + 'http_code' => 200, |
| 72 | + ]); |
| 73 | + }); |
| 74 | + |
| 75 | + $transport = new MailtrapApiSandboxTransport('KEY', 123456, $client); |
| 76 | + |
| 77 | + $mail = new Email(); |
| 78 | + $mail->subject('Hello!') |
| 79 | + ->to(new Address('kevin@symfony.com', 'Kevin')) |
| 80 | + ->from(new Address('fabpot@symfony.com', 'Fabien')) |
| 81 | + ->text('Hello There!'); |
| 82 | + |
| 83 | + $transport->send($mail); |
| 84 | + } |
| 85 | + |
| 86 | + public function testSendThrowsForErrorResponse() |
| 87 | + { |
| 88 | + $client = new MockHttpClient(static fn (string $method, string $url, array $options): ResponseInterface => new JsonMockResponse(['errors' => ['i\'m a teapot']], [ |
| 89 | + 'http_code' => 418, |
| 90 | + ])); |
| 91 | + $transport = new MailtrapApiSandboxTransport('KEY', 123456, $client); |
| 92 | + $transport->setPort(8984); |
| 93 | + |
| 94 | + $mail = new Email(); |
| 95 | + $mail->subject('Hello!') |
| 96 | + ->to(new Address('kevin@symfony.com', 'Kevin')) |
| 97 | + ->from(new Address('fabpot@symfony.com', 'Fabien')) |
| 98 | + ->text('Hello There!'); |
| 99 | + |
| 100 | + $this->expectException(HttpTransportException::class); |
| 101 | + $this->expectExceptionMessage('Unable to send email: "i\'m a teapot" (status code 418).'); |
| 102 | + $transport->send($mail); |
| 103 | + } |
| 104 | + |
| 105 | + public function testTagAndMetadataHeaders() |
| 106 | + { |
| 107 | + $email = new Email(); |
| 108 | + $email->getHeaders()->add(new TagHeader('password-reset')); |
| 109 | + $email->getHeaders()->add(new MetadataHeader('Color', 'blue')); |
| 110 | + $email->getHeaders()->add(new MetadataHeader('Client-ID', '12345')); |
| 111 | + $envelope = new Envelope(new Address('alice@system.com'), [new Address('bob@system.com')]); |
| 112 | + |
| 113 | + $transport = new MailtrapApiSandboxTransport('ACCESS_KEY', 123456); |
| 114 | + $method = new \ReflectionMethod(MailtrapApiSandboxTransport::class, 'getPayload'); |
| 115 | + $payload = $method->invoke($transport, $email, $envelope); |
| 116 | + |
| 117 | + $this->assertArrayNotHasKey('Headers', $payload); |
| 118 | + $this->assertArrayHasKey('category', $payload); |
| 119 | + $this->assertArrayHasKey('custom_variables', $payload); |
| 120 | + |
| 121 | + $this->assertSame('password-reset', $payload['category']); |
| 122 | + $this->assertSame(['Color' => 'blue', 'Client-ID' => '12345'], $payload['custom_variables']); |
| 123 | + } |
| 124 | + |
| 125 | + public function testMultipleTagsAreNotAllowed() |
| 126 | + { |
| 127 | + $email = new Email(); |
| 128 | + $email->getHeaders()->add(new TagHeader('tag1')); |
| 129 | + $email->getHeaders()->add(new TagHeader('tag2')); |
| 130 | + $envelope = new Envelope(new Address('alice@system.com'), [new Address('bob@system.com')]); |
| 131 | + |
| 132 | + $transport = new MailtrapApiSandboxTransport('ACCESS_KEY', 123456); |
| 133 | + $method = new \ReflectionMethod(MailtrapApiSandboxTransport::class, 'getPayload'); |
| 134 | + |
| 135 | + $this->expectException(TransportException::class); |
| 136 | + |
| 137 | + $method->invoke($transport, $email, $envelope); |
| 138 | + } |
| 139 | +} |
0 commit comments