|
| 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\Mailomat\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\Mailomat\Transport\MailomatApiTransport; |
| 18 | +use Symfony\Component\Mailer\Exception\HttpTransportException; |
| 19 | +use Symfony\Component\Mime\Address; |
| 20 | +use Symfony\Component\Mime\Email; |
| 21 | +use Symfony\Contracts\HttpClient\ResponseInterface; |
| 22 | + |
| 23 | +class MailomatApiTransportTest extends TestCase |
| 24 | +{ |
| 25 | + private const KEY = 'K3Y'; |
| 26 | + |
| 27 | + /** |
| 28 | + * @dataProvider getTransportData |
| 29 | + */ |
| 30 | + public function testToString(MailomatApiTransport $transport, string $expected): void |
| 31 | + { |
| 32 | + $this->assertSame($expected, (string) $transport); |
| 33 | + } |
| 34 | + |
| 35 | + public static function getTransportData(): iterable |
| 36 | + { |
| 37 | + yield [ |
| 38 | + new MailomatApiTransport(self::KEY), |
| 39 | + 'mailomat+api://api.mailomat.swiss', |
| 40 | + ]; |
| 41 | + |
| 42 | + yield [ |
| 43 | + (new MailomatApiTransport(self::KEY))->setHost('example.com'), |
| 44 | + 'mailomat+api://example.com', |
| 45 | + ]; |
| 46 | + |
| 47 | + yield [ |
| 48 | + (new MailomatApiTransport(self::KEY))->setHost('example.com')->setPort(99), |
| 49 | + 'mailomat+api://example.com:99', |
| 50 | + ]; |
| 51 | + } |
| 52 | + |
| 53 | + public function testSend() |
| 54 | + { |
| 55 | + $client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface { |
| 56 | + $this->assertSame('POST', $method); |
| 57 | + $this->assertSame('https://api.mailomat.swiss/message', $url); |
| 58 | + $this->assertContains('Authorization: Bearer '.self::KEY, $options['headers']); |
| 59 | + $this->assertContains('Content-Type: application/json', $options['headers']); |
| 60 | + $this->assertContains('Accept: application/json', $options['headers']); |
| 61 | + |
| 62 | + $body = json_decode($options['body'], true); |
| 63 | + $this->assertSame('from@mailomat.swiss', $body['from']['email']); |
| 64 | + $this->assertSame('From Doe', $body['from']['name']); |
| 65 | + |
| 66 | + $this->assertSame('to@mailomat.swiss', $body['to'][0]['email']); |
| 67 | + $this->assertSame('To Doe', $body['to'][0]['name']); |
| 68 | + $this->assertSame('to-simple@mailomat.swiss', $body['to'][1]['email']); |
| 69 | + |
| 70 | + $this->assertSame('cc@mailomat.swiss', $body['cc'][0]['email']); |
| 71 | + $this->assertSame('Cc Doe', $body['cc'][0]['name']); |
| 72 | + $this->assertSame('cc-simple@mailomat.swiss', $body['cc'][1]['email']); |
| 73 | + |
| 74 | + $this->assertSame('bcc@mailomat.swiss', $body['bcc'][0]['email']); |
| 75 | + $this->assertSame('Bcc Doe', $body['bcc'][0]['name']); |
| 76 | + $this->assertSame('bcc-simple@mailomat.swiss', $body['bcc'][1]['email']); |
| 77 | + |
| 78 | + $this->assertSame('replyto@mailomat.swiss', $body['replyTo'][0]['email']); |
| 79 | + $this->assertSame('ReplyTo Doe', $body['replyTo'][0]['name']); |
| 80 | + $this->assertSame('replyto-simple@mailomat.swiss', $body['replyTo'][1]['email']); |
| 81 | + |
| 82 | + $this->assertSame('Hello!', $body['subject']); |
| 83 | + $this->assertSame('Hello There!', $body['text']); |
| 84 | + $this->assertSame('<p>Hello There!</p>', $body['html']); |
| 85 | + |
| 86 | + return new JsonMockResponse(['messageUuid' => 'foobar'], [ |
| 87 | + 'http_code' => 202, |
| 88 | + ]); |
| 89 | + }); |
| 90 | + |
| 91 | + $transport = new MailomatApiTransport(self::KEY, $client); |
| 92 | + |
| 93 | + $mail = new Email(); |
| 94 | + $mail->subject('Hello!') |
| 95 | + ->from(new Address('from@mailomat.swiss', 'From Doe')) |
| 96 | + ->to(new Address('to@mailomat.swiss', 'To Doe'), 'to-simple@mailomat.swiss') |
| 97 | + ->cc(new Address('cc@mailomat.swiss', 'Cc Doe'), 'cc-simple@mailomat.swiss') |
| 98 | + ->bcc(new Address('bcc@mailomat.swiss', 'Bcc Doe'), 'bcc-simple@mailomat.swiss') |
| 99 | + ->replyTo(new Address('replyto@mailomat.swiss', 'ReplyTo Doe'), 'replyto-simple@mailomat.swiss') |
| 100 | + ->text('Hello There!') |
| 101 | + ->html('<p>Hello There!</p>'); |
| 102 | + |
| 103 | + $message = $transport->send($mail); |
| 104 | + |
| 105 | + $this->assertSame('foobar', $message->getMessageId()); |
| 106 | + } |
| 107 | + |
| 108 | + public function testSendThrowsForErrorResponse() |
| 109 | + { |
| 110 | + $client = new MockHttpClient(static fn (string $method, string $url, array $options): ResponseInterface => new JsonMockResponse( |
| 111 | + [ |
| 112 | + 'status' => 422, |
| 113 | + 'violations' => [ |
| 114 | + [ |
| 115 | + 'propertyPath' => '', |
| 116 | + 'message' => 'You must specify either text or html', |
| 117 | + ], |
| 118 | + [ |
| 119 | + 'propertyPath' => 'from', |
| 120 | + 'message' => 'Dieser Wert sollte nicht null sein.', |
| 121 | + ], |
| 122 | + [ |
| 123 | + 'propertyPath' => 'to[1].email', |
| 124 | + 'message' => 'Dieser Wert sollte nicht leer sein.', |
| 125 | + ], |
| 126 | + [ |
| 127 | + 'propertyPath' => 'subject', |
| 128 | + 'message' => 'Dieser Wert sollte nicht leer sein.', |
| 129 | + ], |
| 130 | + ], |
| 131 | + ], [ |
| 132 | + 'http_code' => 422, |
| 133 | + ])); |
| 134 | + $transport = new MailomatApiTransport(self::KEY, $client); |
| 135 | + $transport->setPort(8984); |
| 136 | + |
| 137 | + $mail = new Email(); |
| 138 | + $mail->subject('Hello!') |
| 139 | + ->to(new Address('to@mailomat.swiss', 'To Doe')) |
| 140 | + ->from(new Address('from@mailomat.swiss', 'From Doe')) |
| 141 | + ->text('Hello There!'); |
| 142 | + |
| 143 | + $this->expectException(HttpTransportException::class); |
| 144 | + $this->expectExceptionMessage('Unable to send an email: You must specify either text or html; (from) Dieser Wert sollte nicht null sein.; (to[1].email) Dieser Wert sollte nicht leer sein.; (subject) Dieser Wert sollte nicht leer sein. (code 422)'); |
| 145 | + $transport->send($mail); |
| 146 | + } |
| 147 | +} |
0 commit comments