|
| 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 | +namespaceSymfony\Component\Mailer\Bridge\Resend\Tests; |
| 13 | + |
| 14 | +usePHPUnit\Framework\TestCase; |
| 15 | +useSymfony\Component\HttpClient\MockHttpClient; |
| 16 | +useSymfony\Component\HttpClient\Response\JsonMockResponse; |
| 17 | +useSymfony\Component\Mailer\Bridge\Resend\Transport\ResendApiTransport; |
| 18 | +useSymfony\Component\Mailer\Envelope; |
| 19 | +useSymfony\Component\Mailer\Exception\HttpTransportException; |
| 20 | +useSymfony\Component\Mailer\Header\MetadataHeader; |
| 21 | +useSymfony\Component\Mailer\Header\TagHeader; |
| 22 | +useSymfony\Component\Mime\Address; |
| 23 | +useSymfony\Component\Mime\Email; |
| 24 | +useSymfony\Component\Mime\Part\DataPart; |
| 25 | +useSymfony\Contracts\HttpClient\ResponseInterface; |
| 26 | + |
| 27 | +class ResendApiTransportTestextends TestCase |
| 28 | +{ |
| 29 | +/** |
| 30 | + * @dataProvider getTransportData |
| 31 | + */ |
| 32 | +publicfunctiontestToString(ResendApiTransport$transport,string$expected) |
| 33 | + { |
| 34 | +$this->assertSame($expected, (string)$transport); |
| 35 | + } |
| 36 | + |
| 37 | +publicstaticfunctiongetTransportData():\Generator |
| 38 | + { |
| 39 | +yield [ |
| 40 | +newResendApiTransport('ACCESS_KEY'), |
| 41 | +'resend+api://api.resend.com', |
| 42 | + ]; |
| 43 | + |
| 44 | +yield [ |
| 45 | + (newResendApiTransport('ACCESS_KEY'))->setHost('example.com'), |
| 46 | +'resend+api://example.com', |
| 47 | + ]; |
| 48 | + |
| 49 | +yield [ |
| 50 | + (newResendApiTransport('ACCESS_KEY'))->setHost('example.com')->setPort(99), |
| 51 | +'resend+api://example.com:99', |
| 52 | + ]; |
| 53 | + } |
| 54 | + |
| 55 | +publicfunctiontestCustomHeader() |
| 56 | + { |
| 57 | +$params = ['param1' =>'foo','param2' =>'bar']; |
| 58 | +$json =json_encode(['custom_header_1' =>'custom_value_1']); |
| 59 | + |
| 60 | +$email =newEmail(); |
| 61 | +$email->getHeaders() |
| 62 | + ->add(newMetadataHeader('custom',$json)) |
| 63 | + ->add(newTagHeader('TagInHeaders')) |
| 64 | + ->addTextHeader('templateId',1) |
| 65 | + ->addParameterizedHeader('params','params',$params) |
| 66 | + ->addTextHeader('foo','bar'); |
| 67 | +$envelope =newEnvelope(newAddress('alice@system.com','Alice'), [newAddress('bob@system.com','Bob')]); |
| 68 | + |
| 69 | +$transport =newResendApiTransport('ACCESS_KEY'); |
| 70 | +$method =new \ReflectionMethod(ResendApiTransport::class,'getPayload'); |
| 71 | +$payload =$method->invoke($transport,$email,$envelope); |
| 72 | + |
| 73 | +$this->assertArrayHasKey('X-Metadata-custom',$payload['headers']); |
| 74 | +$this->assertEquals($json,$payload['headers']['X-Metadata-custom']); |
| 75 | +$this->assertArrayHasKey('tags',$payload); |
| 76 | +$this->assertEquals(['X-Tag' =>'TagInHeaders'],current($payload['tags'])); |
| 77 | +$this->assertArrayHasKey('templateId',$payload['headers']); |
| 78 | +$this->assertEquals('1',$payload['headers']['templateId']); |
| 79 | +$this->assertArrayHasKey('params',$payload['headers']); |
| 80 | +$this->assertEquals('params; param1=foo; param2=bar',$payload['headers']['params']); |
| 81 | +$this->assertArrayHasKey('foo',$payload['headers']); |
| 82 | +$this->assertEquals('bar',$payload['headers']['foo']); |
| 83 | + } |
| 84 | + |
| 85 | +publicfunctiontestSendThrowsForErrorResponse() |
| 86 | + { |
| 87 | +$client =newMockHttpClient(function (string$method,string$url,array$options):ResponseInterface { |
| 88 | +$this->assertSame('POST',$method); |
| 89 | +$this->assertSame('https://api.resend.com:8984/emails',$url); |
| 90 | +$this->assertStringContainsString('Accept: */*',$options['headers'][2] ??$options['request_headers'][1]); |
| 91 | + |
| 92 | +returnnewJsonMockResponse(['message' =>'i\'m a teapot'], [ |
| 93 | +'http_code' =>418, |
| 94 | + ]); |
| 95 | + }); |
| 96 | + |
| 97 | +$transport =newResendApiTransport('ACCESS_KEY',$client); |
| 98 | +$transport->setPort(8984); |
| 99 | + |
| 100 | +$mail =newEmail(); |
| 101 | +$mail->subject('Hello!') |
| 102 | + ->to(newAddress('tony.stark@marvel.com','Tony Stark')) |
| 103 | + ->from(newAddress('fabpot@symfony.com','Fabien')) |
| 104 | + ->text('Hello There!'); |
| 105 | + |
| 106 | +$this->expectException(HttpTransportException::class); |
| 107 | +$this->expectExceptionMessage('Unable to send an email: {"message":"i\'m a teapot"} (code 418).'); |
| 108 | +$transport->send($mail); |
| 109 | + } |
| 110 | + |
| 111 | +publicfunctiontestSend() |
| 112 | + { |
| 113 | +$client =newMockHttpClient(function (string$method,string$url,array$options):ResponseInterface { |
| 114 | +$this->assertSame('POST',$method); |
| 115 | +$this->assertSame('https://api.resend.com:8984/emails',$url); |
| 116 | +$this->assertStringContainsString('Accept: */*',$options['headers'][2] ??$options['request_headers'][1]); |
| 117 | + |
| 118 | +returnnewJsonMockResponse(['id' =>'foobar'], [ |
| 119 | +'http_code' =>200, |
| 120 | + ]); |
| 121 | + }); |
| 122 | + |
| 123 | +$transport =newResendApiTransport('ACCESS_KEY',$client); |
| 124 | +$transport->setPort(8984); |
| 125 | + |
| 126 | +$mail =newEmail(); |
| 127 | +$mail->subject('Hello!') |
| 128 | + ->to(newAddress('tony.stark@marvel.com','Tony Stark')) |
| 129 | + ->from(newAddress('fabpot@symfony.com','Fabien')) |
| 130 | + ->text('Hello here!') |
| 131 | + ->html('Hello there!') |
| 132 | + ->addCc('foo@bar.fr') |
| 133 | + ->addBcc('foo@bar.fr') |
| 134 | + ->addReplyTo('foo@bar.fr') |
| 135 | + ->addPart(newDataPart('body')); |
| 136 | + |
| 137 | +$message =$transport->send($mail); |
| 138 | + |
| 139 | +$this->assertSame('foobar',$message->getMessageId()); |
| 140 | + } |
| 141 | + |
| 142 | +/** |
| 143 | + * IDN (internationalized domain names) like kältetechnik-xyz.de need to be transformed to ACE |
| 144 | + * (ASCII Compatible Encoding) e.g.xn--kltetechnik-xyz-0kb.de, otherwise resend api answers with 400 http code. |
| 145 | + */ |
| 146 | +publicfunctiontestSendForIdnDomains() |
| 147 | + { |
| 148 | +$client =newMockHttpClient(function (string$method,string$url,array$options):ResponseInterface { |
| 149 | +$this->assertSame('POST',$method); |
| 150 | +$this->assertSame('https://api.resend.com:8984/emails',$url); |
| 151 | +$this->assertStringContainsString('Accept: */*',$options['headers'][2] ??$options['request_headers'][1]); |
| 152 | + |
| 153 | +$body =json_decode($options['body'],true); |
| 154 | +// to |
| 155 | +$this->assertSame('kältetechnik@xn--kltetechnik-xyz-0kb.de',$body['to'][0]); |
| 156 | +// sender |
| 157 | +$this->assertStringContainsString('info@xn--kltetechnik-xyz-0kb.de',$body['from']); |
| 158 | +$this->assertStringContainsString('Kältetechnik Xyz',$body['from']); |
| 159 | + |
| 160 | +returnnewJsonMockResponse(['id' =>'foobar'], [ |
| 161 | +'http_code' =>200, |
| 162 | + ]); |
| 163 | + }); |
| 164 | + |
| 165 | +$transport =newResendApiTransport('ACCESS_KEY',$client); |
| 166 | +$transport->setPort(8984); |
| 167 | + |
| 168 | +$mail =newEmail(); |
| 169 | +$mail->subject('Hello!') |
| 170 | + ->to(newAddress('kältetechnik@kältetechnik-xyz.de','Kältetechnik Xyz')) |
| 171 | + ->from(newAddress('info@kältetechnik-xyz.de','Kältetechnik Xyz')) |
| 172 | + ->text('Hello here!') |
| 173 | + ->html('Hello there!'); |
| 174 | + |
| 175 | +$message =$transport->send($mail); |
| 176 | + |
| 177 | +$this->assertSame('foobar',$message->getMessageId()); |
| 178 | + } |
| 179 | +} |