|
7 | 7 | use Resend\Laravel\Transport\ResendTransportFactory; |
8 | 8 | use Resend\Service\Email as EmailService; |
9 | 9 | use Symfony\Component\Mailer\Exception\TransportException; |
| 10 | +use Symfony\Component\Mailer\Header\MetadataHeader; |
10 | 11 | use Symfony\Component\Mime\Address; |
11 | 12 | use Symfony\Component\Mime\Email as SymfonyEmail; |
12 | 13 |
|
|
61 | 62 | $arg['cc'] === ['cc@example.com'] && |
62 | 63 | $arg['bcc'] === ['bcc@example.com'] && |
63 | 64 | $arg['reply_to'] === ['reply-to@example.com']; |
64 | | - })) |
| 65 | + }), Mockery::type('array')) |
65 | 66 | ->andReturn($apiResponse); |
66 | 67 |
|
67 | 68 | $this->transporter->send($email); |
|
88 | 89 | ->with(Mockery::on(function ($arg) { |
89 | 90 | return $arg['from'] === 'from@example.com' && |
90 | 91 | $arg['to'] === ['"Acme" <to@example.com>', '"Acme Sales" <sales@example.com>']; |
91 | | - })) |
| 92 | + }), Mockery::type('array')) |
92 | 93 | ->andReturn($apiResponse); |
93 | 94 |
|
94 | 95 | $this->transporter->send($email); |
|
114 | 115 | $arg['to'] === ['"Acme" <to@example.com>'] && |
115 | 116 | $arg['subject'] === 'Test Subject' && |
116 | 117 | array_key_exists('X-Entity-Ref-ID', $arg['headers']); |
117 | | - })) |
| 118 | + }), Mockery::type('array')) |
118 | 119 | ->andReturn($apiResponse); |
119 | 120 |
|
120 | 121 | $this->transporter->send($email); |
|
151 | 152 | $arg['attachments'][0]['filename'] === 'lorem-ipsum.txt' && |
152 | 153 | $arg['attachments'][0]['content'] === 'TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQWVuZWFuIG51bmMgYXVndWUsIGNvbnNlY3RldHVyIGlkIG5lcXVlIGVnZXQsIHZhcml1cyBkaWduaXNzaW0gZGlhbS4=' && |
153 | 154 | $arg['attachments'][0]['content_type'] === 'text/plain'; |
154 | | - })) |
| 155 | + }), Mockery::type('array')) |
155 | 156 | ->andReturn($apiResponse); |
156 | 157 |
|
157 | 158 | $this->transporter->send($email); |
|
190 | 191 | $arg['attachments'][0]['content'] === 'TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQWVuZWFuIG51bmMgYXVndWUsIGNvbnNlY3RldHVyIGlkIG5lcXVlIGVnZXQsIHZhcml1cyBkaWduaXNzaW0gZGlhbS4=' && |
191 | 192 | $arg['attachments'][0]['content_id'] === 'lorem-ipsum.txt' && |
192 | 193 | $arg['attachments'][0]['content_type'] === 'text/plain'; |
193 | | - })) |
| 194 | + }), Mockery::type('array')) |
194 | 195 | ->andReturn($apiResponse); |
195 | 196 |
|
196 | 197 | $this->transporter->send($email); |
|
228 | 229 | $arg['attachments'][0]['content_type'] === 'text/calendar' && |
229 | 230 | $arg['attachments'][0]['content'] === $calendarContent && |
230 | 231 | $arg['attachments'][0]['filename'] === 'invite.ics'; |
231 | | - })) |
| 232 | + }), Mockery::type('array')) |
232 | 233 | ->andReturn($apiResponse); |
233 | 234 |
|
234 | 235 | $this->transporter->send($email); |
|
268 | 269 | return $arg['from'] === 'from@example.com' && |
269 | 270 | $arg['to'] === ['"Acme" <to@example.com>'] && |
270 | 271 | $arg['subject'] === 'Test Subject'; |
271 | | - })) |
| 272 | + }), Mockery::type('array')) |
272 | 273 | ->andReturn($apiResponse); |
273 | 274 |
|
274 | 275 | $message = $this->transporter->send($email); |
|
286 | 287 | ->getValue() |
287 | 288 | )->toBe('49a3999c-0ce1-4ea6-ab68-afcd6dc2e794'); |
288 | 289 | }); |
| 290 | + |
| 291 | +it('can send with tags', function () { |
| 292 | + $email = (new SymfonyEmail()) |
| 293 | + ->from('from@example.com') |
| 294 | + ->to(new Address('to@example.com', 'Acme')) |
| 295 | + ->subject('Test Subject') |
| 296 | + ->text('Test plain text body'); |
| 297 | + $email->getHeaders()->add(new MetadataHeader('category', 'confirm_email')); |
| 298 | + $email->getHeaders()->add(new MetadataHeader('customer_id', '123')); |
| 299 | + |
| 300 | + $apiResponse = new Email([ |
| 301 | + 'id' => '49a3999c-0ce1-4ea6-ab68-afcd6dc2e794', |
| 302 | + ]); |
| 303 | + |
| 304 | + $this->client->emails |
| 305 | + ->shouldReceive('send') |
| 306 | + ->once() |
| 307 | + ->with(Mockery::on(function ($arg) { |
| 308 | + return $arg['from'] === 'from@example.com' && |
| 309 | + $arg['to'] === ['"Acme" <to@example.com>'] && |
| 310 | + $arg['subject'] === 'Test Subject' && |
| 311 | + $arg['tags'] === [ |
| 312 | + ['name' => 'category', 'value' => 'confirm_email'], |
| 313 | + ['name' => 'customer_id', 'value' => '123'], |
| 314 | + ] && |
| 315 | + ! array_key_exists('X-Metadata-category', $arg['headers']) && |
| 316 | + ! array_key_exists('X-Metadata-customer_id', $arg['headers']); |
| 317 | + }), Mockery::type('array')) |
| 318 | + ->andReturn($apiResponse); |
| 319 | + |
| 320 | + $this->transporter->send($email); |
| 321 | +}); |
| 322 | + |
| 323 | +it('can send with idempotency key', function () { |
| 324 | + $email = (new SymfonyEmail()) |
| 325 | + ->from('from@example.com') |
| 326 | + ->to(new Address('to@example.com', 'Acme')) |
| 327 | + ->subject('Test Subject') |
| 328 | + ->text('Test plain text body'); |
| 329 | + $email->getHeaders()->addTextHeader('Resend-Idempotency-Key', 'welcome-user/123456789'); |
| 330 | + |
| 331 | + $apiResponse = new Email([ |
| 332 | + 'id' => '49a3999c-0ce1-4ea6-ab68-afcd6dc2e794', |
| 333 | + ]); |
| 334 | + |
| 335 | + $this->client->emails |
| 336 | + ->shouldReceive('send') |
| 337 | + ->once() |
| 338 | + ->with(Mockery::on(function ($arg) { |
| 339 | + return $arg['from'] === 'from@example.com' && |
| 340 | + $arg['to'] === ['"Acme" <to@example.com>'] && |
| 341 | + $arg['subject'] === 'Test Subject' && |
| 342 | + ! array_key_exists('Resend-Idempotency-Key', $arg['headers']); |
| 343 | + }), Mockery::on(function ($options) { |
| 344 | + return $options === ['idempotency_key' => 'welcome-user/123456789']; |
| 345 | + })) |
| 346 | + ->andReturn($apiResponse); |
| 347 | + |
| 348 | + $this->transporter->send($email); |
| 349 | +}); |
0 commit comments