|
| 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\Brevo\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\Brevo\Transport\BrevoApiTransport; |
| 18 | +use Symfony\Component\Mailer\Envelope; |
| 19 | +use Symfony\Component\Mailer\Exception\HttpTransportException; |
| 20 | +use Symfony\Component\Mailer\Header\MetadataHeader; |
| 21 | +use Symfony\Component\Mailer\Header\TagHeader; |
| 22 | +use Symfony\Component\Mime\Address; |
| 23 | +use Symfony\Component\Mime\Email; |
| 24 | +use Symfony\Component\Mime\Part\DataPart; |
| 25 | +use Symfony\Contracts\HttpClient\ResponseInterface; |
| 26 | + |
| 27 | +class BrevoApiTransportTest extends TestCase |
| 28 | +{ |
| 29 | + /** |
| 30 | + * @dataProvider getTransportData |
| 31 | + */ |
| 32 | + public function testToString(BrevoApiTransport $transport, string $expected) |
| 33 | + { |
| 34 | + $this->assertSame($expected, (string) $transport); |
| 35 | + } |
| 36 | + |
| 37 | + public static function getTransportData() |
| 38 | + { |
| 39 | + yield [ |
| 40 | + new BrevoApiTransport('ACCESS_KEY'), |
| 41 | + 'brevo+api://api.brevo.com', |
| 42 | + ]; |
| 43 | + |
| 44 | + yield [ |
| 45 | + (new BrevoApiTransport('ACCESS_KEY'))->setHost('example.com'), |
| 46 | + 'brevo+api://example.com', |
| 47 | + ]; |
| 48 | + |
| 49 | + yield [ |
| 50 | + (new BrevoApiTransport('ACCESS_KEY'))->setHost('example.com')->setPort(99), |
| 51 | + 'brevo+api://example.com:99', |
| 52 | + ]; |
| 53 | + } |
| 54 | + |
| 55 | + public function testCustomHeader() |
| 56 | + { |
| 57 | + $params = ['param1' => 'foo', 'param2' => 'bar']; |
| 58 | + $json = json_encode(['"custom_header_1' => 'custom_value_1']); |
| 59 | + |
| 60 | + $email = new Email(); |
| 61 | + $email->getHeaders() |
| 62 | + ->add(new MetadataHeader('custom', $json)) |
| 63 | + ->add(new TagHeader('TagInHeaders')) |
| 64 | + ->addTextHeader('templateId', 1) |
| 65 | + ->addParameterizedHeader('params', 'params', $params) |
| 66 | + ->addTextHeader('foo', 'bar'); |
| 67 | + $envelope = new Envelope(new Address('alice@system.com', 'Alice'), [new Address('bob@system.com', 'Bob')]); |
| 68 | + |
| 69 | + $transport = new BrevoApiTransport('ACCESS_KEY'); |
| 70 | + $method = new \ReflectionMethod(BrevoApiTransport::class, 'getPayload'); |
| 71 | + $payload = $method->invoke($transport, $email, $envelope); |
| 72 | + |
| 73 | + $this->assertArrayHasKey('X-Mailin-Custom', $payload['headers']); |
| 74 | + $this->assertEquals($json, $payload['headers']['X-Mailin-Custom']); |
| 75 | + |
| 76 | + $this->assertArrayHasKey('tags', $payload); |
| 77 | + $this->assertEquals('TagInHeaders', current($payload['tags'])); |
| 78 | + $this->assertArrayHasKey('templateId', $payload); |
| 79 | + $this->assertEquals(1, $payload['templateId']); |
| 80 | + $this->assertArrayHasKey('params', $payload); |
| 81 | + $this->assertEquals('foo', $payload['params']['param1']); |
| 82 | + $this->assertEquals('bar', $payload['params']['param2']); |
| 83 | + $this->assertArrayHasKey('foo', $payload['headers']); |
| 84 | + $this->assertEquals('bar', $payload['headers']['foo']); |
| 85 | + } |
| 86 | + |
| 87 | + public function testSendThrowsForErrorResponse() |
| 88 | + { |
| 89 | + $client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface { |
| 90 | + $this->assertSame('POST', $method); |
| 91 | + $this->assertSame('https://api.brevo.com:8984/v3/smtp/email', $url); |
| 92 | + $this->assertStringContainsString('Accept: */*', $options['headers'][2] ?? $options['request_headers'][1]); |
| 93 | + |
| 94 | + return new MockResponse(json_encode(['message' => 'i\'m a teapot']), [ |
| 95 | + 'http_code' => 418, |
| 96 | + 'response_headers' => [ |
| 97 | + 'content-type' => 'application/json', |
| 98 | + ], |
| 99 | + ]); |
| 100 | + }); |
| 101 | + |
| 102 | + $transport = new BrevoApiTransport('ACCESS_KEY', $client); |
| 103 | + $transport->setPort(8984); |
| 104 | + |
| 105 | + $mail = new Email(); |
| 106 | + $mail->subject('Hello!') |
| 107 | + ->to(new Address('saif.gmati@symfony.com', 'Saif Eddin')) |
| 108 | + ->from(new Address('fabpot@symfony.com', 'Fabien')) |
| 109 | + ->text('Hello There!'); |
| 110 | + |
| 111 | + $this->expectException(HttpTransportException::class); |
| 112 | + $this->expectExceptionMessage('Unable to send an email: i\'m a teapot (code 418).'); |
| 113 | + $transport->send($mail); |
| 114 | + } |
| 115 | + |
| 116 | + public function testSend() |
| 117 | + { |
| 118 | + $client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface { |
| 119 | + $this->assertSame('POST', $method); |
| 120 | + $this->assertSame('https://api.brevo.com:8984/v3/smtp/email', $url); |
| 121 | + $this->assertStringContainsString('Accept: */*', $options['headers'][2] ?? $options['request_headers'][1]); |
| 122 | + |
| 123 | + return new MockResponse(json_encode(['messageId' => 'foobar']), [ |
| 124 | + 'http_code' => 201, |
| 125 | + ]); |
| 126 | + }); |
| 127 | + |
| 128 | + $transport = new BrevoApiTransport('ACCESS_KEY', $client); |
| 129 | + $transport->setPort(8984); |
| 130 | + |
| 131 | + $mail = new Email(); |
| 132 | + $mail->subject('Hello!') |
| 133 | + ->to(new Address('saif.gmati@symfony.com', 'Saif Eddin')) |
| 134 | + ->from(new Address('fabpot@symfony.com', 'Fabien')) |
| 135 | + ->text('Hello here!') |
| 136 | + ->html('Hello there!') |
| 137 | + ->addCc('foo@bar.fr') |
| 138 | + ->addBcc('foo@bar.fr') |
| 139 | + ->addReplyTo('foo@bar.fr') |
| 140 | + ->addPart(new DataPart('body')); |
| 141 | + |
| 142 | + $message = $transport->send($mail); |
| 143 | + |
| 144 | + $this->assertSame('foobar', $message->getMessageId()); |
| 145 | + } |
| 146 | +} |
0 commit comments