|
| 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\Postal\Tests\Transport; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\HttpClient\MockHttpClient; |
| 16 | +use Symfony\Component\HttpClient\Response\JsonMockResponse; |
| 17 | +use Symfony\Component\Mailer\Bridge\Postal\Transport\PostalApiTransport; |
| 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 PostalApiTransportTest extends TestCase |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @dataProvider getTransportData |
| 28 | + */ |
| 29 | + public function testToString(PostalApiTransport $transport, string $expected) |
| 30 | + { |
| 31 | + $this->assertSame($expected, (string) $transport); |
| 32 | + } |
| 33 | + |
| 34 | + public static function getTransportData(): array |
| 35 | + { |
| 36 | + return [ |
| 37 | + [ |
| 38 | + (new PostalApiTransport('TOKEN', 'postal.localhost')), |
| 39 | + 'postal+api://postal.localhost', |
| 40 | + ], |
| 41 | + [ |
| 42 | + (new PostalApiTransport('TOKEN', 'postal.localhost'))->setPort(99), |
| 43 | + 'postal+api://postal.localhost:99', |
| 44 | + ], |
| 45 | + ]; |
| 46 | + } |
| 47 | + |
| 48 | + public function testSend() |
| 49 | + { |
| 50 | + $client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface { |
| 51 | + $this->assertSame('POST', $method); |
| 52 | + $this->assertSame('https://postal.localhost:8984/api/v1/send/message', $url); |
| 53 | + $this->assertStringContainsString('X-Server-API-Key: TOKEN', $options['headers'][0] ?? $options['request_headers'][0]); |
| 54 | + |
| 55 | + $body = json_decode($options['body'], true); |
| 56 | + $this->assertSame('fabpot@symfony.com', $body['from']); |
| 57 | + $this->assertSame('saif.gmati@symfony.com', $body['to'][0]); |
| 58 | + $this->assertSame('Hello!', $body['subject']); |
| 59 | + $this->assertSame('Hello There!', $body['plain_body']); |
| 60 | + $this->assertSame('<h1>Hello There!</h1>', $body['html_body']); |
| 61 | + $this->assertCount(1, $body['attachments']); |
| 62 | + $this->assertSame('attachment.txt', $body['attachments'][0]['name']); |
| 63 | + $this->assertSame('text/plain', $body['attachments'][0]['content_type']); |
| 64 | + $this->assertSame(base64_encode('some attachment'), $body['attachments'][0]['data']); |
| 65 | + $this->assertSame('foo@bar.fr', $body['reply_to']); |
| 66 | + |
| 67 | + return new JsonMockResponse(['message_id' => 'foobar'], [ |
| 68 | + 'http_code' => 200, |
| 69 | + ]); |
| 70 | + }); |
| 71 | + $transport = new PostalApiTransport('TOKEN', 'postal.localhost', $client); |
| 72 | + $transport->setPort(8984); |
| 73 | + |
| 74 | + $mail = new Email(); |
| 75 | + $mail->subject('Hello!') |
| 76 | + ->to(new Address('saif.gmati@symfony.com', 'Saif Eddin')) |
| 77 | + ->from(new Address('fabpot@symfony.com', 'Fabien')) |
| 78 | + ->replyTo(new Address('foo@bar.fr', 'Foo')) |
| 79 | + ->text('Hello There!') |
| 80 | + ->html('<h1>Hello There!</h1>') |
| 81 | + ->addPart(new DataPart('some attachment', 'attachment.txt', 'text/plain')); |
| 82 | + |
| 83 | + $message = $transport->send($mail); |
| 84 | + |
| 85 | + $this->assertSame('foobar', $message->getMessageId()); |
| 86 | + } |
| 87 | + |
| 88 | + public function testSendThrowsForErrorResponse() |
| 89 | + { |
| 90 | + $client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface { |
| 91 | + return new JsonMockResponse(['message' => 'i\'m a teapot'], [ |
| 92 | + 'http_code' => 418, |
| 93 | + ]); |
| 94 | + }); |
| 95 | + $transport = new PostalApiTransport('TOKEN', 'postal.localhost', $client); |
| 96 | + |
| 97 | + $mail = new Email(); |
| 98 | + $mail->subject('Hello!') |
| 99 | + ->to(new Address('saif.gmati@symfony.com', 'Saif Eddin')) |
| 100 | + ->from(new Address('fabpot@symfony.com', 'Fabien')) |
| 101 | + ->text('Hello There!'); |
| 102 | + |
| 103 | + $this->expectException(HttpTransportException::class); |
| 104 | + $this->expectExceptionMessage('Unable to send an email: i\'m a teapot (code 418).'); |
| 105 | + $transport->send($mail); |
| 106 | + } |
| 107 | +} |
0 commit comments