|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 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\Azure\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\Azure\Transport\AzureApiTransport; |
| 18 | +use Symfony\Component\Mailer\Envelope; |
| 19 | +use Symfony\Component\Mailer\Header\MetadataHeader; |
| 20 | +use Symfony\Component\Mailer\Header\TagHeader; |
| 21 | +use Symfony\Component\Mime\Address; |
| 22 | +use Symfony\Component\Mime\Email; |
| 23 | +use Symfony\Contracts\HttpClient\ResponseInterface; |
| 24 | + |
| 25 | +class AzureApiTransportTest extends TestCase |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @dataProvider getTransportData |
| 29 | + */ |
| 30 | + public function testToString(AzureApiTransport $transport, string $expected) |
| 31 | + { |
| 32 | + $this->assertSame($expected, (string) $transport); |
| 33 | + } |
| 34 | + |
| 35 | + public static function getTransportData(): array |
| 36 | + { |
| 37 | + return [ |
| 38 | + [ |
| 39 | + new AzureApiTransport('KEY', 'ACS_RESOURCE_NAME'), |
| 40 | + 'azure+api://ACS_RESOURCE_NAME.communication.azure.com', |
| 41 | + ], |
| 42 | + ]; |
| 43 | + } |
| 44 | + |
| 45 | + public function testCustomHeader() |
| 46 | + { |
| 47 | + $email = new Email(); |
| 48 | + $email->getHeaders()->addTextHeader('foo', 'bar'); |
| 49 | + $envelope = new Envelope( new Address( '[email protected]'), [ new Address( '[email protected]')]); |
| 50 | + |
| 51 | + $transport = new AzureApiTransport('KEY', 'ACS_RESOURCE_NAME'); |
| 52 | + $method = new \ReflectionMethod(AzureApiTransport::class, 'getPayload'); |
| 53 | + $payload = $method->invoke($transport, $email, $envelope); |
| 54 | + |
| 55 | + $this->assertArrayHasKey('headers', $payload); |
| 56 | + $this->assertArrayHasKey('foo', $payload['headers']); |
| 57 | + $this->assertEquals('bar', $payload['headers']['foo']); |
| 58 | + } |
| 59 | + |
| 60 | + public function testSend() |
| 61 | + { |
| 62 | + $client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface { |
| 63 | + $this->assertSame('POST', $method); |
| 64 | + $this->assertSame('https://my-acs-resource.communication.azure.com/emails:send?api-version=2023-03-31', $url); |
| 65 | + |
| 66 | + $body = json_decode($options['body'], true); |
| 67 | + |
| 68 | + $message = $body['content']; |
| 69 | + $this->assertSame('normal', $body['importance']); |
| 70 | + // $this->assertSame('Fabien', $message['from_name']); |
| 71 | + $this-> assertSame( '[email protected]', $body[ 'senderAddress']); |
| 72 | + $this->assertSame('Saif Eddin', $body['recipients']['to'][0]['displayName']); |
| 73 | + $this-> assertSame( '[email protected]', $body[ 'recipients'][ 'to'][ 0][ 'address']); |
| 74 | + $this->assertSame('Hello!', $message['subject']); |
| 75 | + $this->assertSame('Hello There!', $message['plainText']); |
| 76 | + |
| 77 | + return new JsonMockResponse([ |
| 78 | + 'id' => 'foobar', |
| 79 | + ], [ |
| 80 | + 'http_code' => 202, |
| 81 | + ]); |
| 82 | + }); |
| 83 | + |
| 84 | + $transport = new AzureApiTransport('KEY', 'my-acs-resource', true, '2023-03-31', $client); |
| 85 | + |
| 86 | + $mail = new Email(); |
| 87 | + $mail->subject('Hello!') |
| 88 | + -> to( new Address( '[email protected]', 'Saif Eddin')) |
| 89 | + -> from( new Address( '[email protected]', 'Fabien')) |
| 90 | + ->text('Hello There!'); |
| 91 | + |
| 92 | + $message = $transport->send($mail); |
| 93 | + |
| 94 | + $this->assertSame('foobar', $message->getMessageId()); |
| 95 | + } |
| 96 | + |
| 97 | + public function testTagAndMetadataHeaders() |
| 98 | + { |
| 99 | + $email = new Email(); |
| 100 | + $email->getHeaders()->add(new TagHeader('category-one')); |
| 101 | + $email->getHeaders()->add(new MetadataHeader('Color', 'blue')); |
| 102 | + $email->getHeaders()->add(new MetadataHeader('Client-ID', '12345')); |
| 103 | + $envelope = new Envelope( new Address( '[email protected]'), [ new Address( '[email protected]')]); |
| 104 | + |
| 105 | + $transport = new AzureApiTransport('KEY', 'ACS_RESOURCE_NAME'); |
| 106 | + $method = new \ReflectionMethod(AzureApiTransport::class, 'getPayload'); |
| 107 | + $payload = $method->invoke($transport, $email, $envelope); |
| 108 | + |
| 109 | + $this->assertArrayHasKey('headers', $payload); |
| 110 | + $this->assertArrayHasKey('X-Tag', $payload['headers']); |
| 111 | + $this->assertArrayHasKey('X-Metadata-Color', $payload['headers']); |
| 112 | + $this->assertArrayHasKey('X-Metadata-Client-ID', $payload['headers']); |
| 113 | + |
| 114 | + $this->assertCount(3, $payload['headers']); |
| 115 | + |
| 116 | + $this->assertSame('category-one', $payload['headers']['X-Tag']); |
| 117 | + $this->assertSame('blue', $payload['headers']['X-Metadata-Color']); |
| 118 | + $this->assertSame('12345', $payload['headers']['X-Metadata-Client-ID']); |
| 119 | + } |
| 120 | + |
| 121 | + public function testItDoesNotAllowToAddResourceNameWithDot() |
| 122 | + { |
| 123 | + $this->expectException(\Exception::class); |
| 124 | + $this->expectExceptionMessage('Resource name cannot contain or end with a dot'); |
| 125 | + |
| 126 | + new AzureApiTransport('KEY', 'ACS_RESOURCE_NAME.'); |
| 127 | + } |
| 128 | +} |
0 commit comments