Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 29
SesApiTransportTest
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 4
20
0.00% covered (danger)
0.00%
0 / 29
 testToString
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 getTransportData
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 testSend
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 17
 testSendThrowsForErrorResponse
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 10
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
12namespace Symfony\Component\Mailer\Bridge\Amazon\Tests\Transport;
13
14use PHPUnit\Framework\TestCase;
15use Symfony\Component\HttpClient\MockHttpClient;
16use Symfony\Component\HttpClient\Response\MockResponse;
17use Symfony\Component\Mailer\Bridge\Amazon\Transport\SesApiTransport;
18use Symfony\Component\Mailer\Exception\HttpTransportException;
19use Symfony\Component\Mime\Address;
20use Symfony\Component\Mime\Email;
21use Symfony\Contracts\HttpClient\ResponseInterface;
22
23/**
24 * @group legacy
25 */
26class SesApiTransportTest extends TestCase
27{
28    /**
29     * @dataProvider getTransportData
30     */
31    public function testToString(SesApiTransport $transport, string $expected)
32    {
33        $this->assertSame($expected, (string) $transport);
34    }
35
36    public function getTransportData()
37    {
38        return [
39            [
40                new SesApiTransport('ACCESS_KEY', 'SECRET_KEY'),
41                'ses+api://ACCESS_KEY@email.eu-west-1.amazonaws.com',
42            ],
43            [
44                new SesApiTransport('ACCESS_KEY', 'SECRET_KEY', 'us-east-1'),
45                'ses+api://ACCESS_KEY@email.us-east-1.amazonaws.com',
46            ],
47            [
48                (new SesApiTransport('ACCESS_KEY', 'SECRET_KEY'))->setHost('example.com'),
49                'ses+api://ACCESS_KEY@example.com',
50            ],
51            [
52                (new SesApiTransport('ACCESS_KEY', 'SECRET_KEY'))->setHost('example.com')->setPort(99),
53                'ses+api://ACCESS_KEY@example.com:99',
54            ],
55        ];
56    }
57
58    public function testSend()
59    {
60        $client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface {
61            $this->assertSame('POST', $method);
62            $this->assertSame('https://email.eu-west-1.amazonaws.com:8984/', $url);
63            $this->assertStringContainsStringIgnoringCase('X-Amzn-Authorization: AWS3-HTTPS AWSAccessKeyId=ACCESS_KEY,Algorithm=HmacSHA256,Signature=', $options['headers'][0] ?? $options['request_headers'][0]);
64
65            parse_str($options['body'], $content);
66
67            $this->assertSame('Hello!', $content['Message_Subject_Data']);
68            $this->assertSame('Saif Eddin <saif.gmati@symfony.com>', $content['Destination_ToAddresses_member'][0]);
69            $this->assertSame('Fabien <fabpot@symfony.com>', $content['Source']);
70            $this->assertSame('Hello There!', $content['Message_Body_Text_Data']);
71
72            $xml = '<SendEmailResponse xmlns="https://email.amazonaws.com/doc/2010-03-31/">
73  <SendEmailResult>
74    <MessageId>foobar</MessageId>
75  </SendEmailResult>
76</SendEmailResponse>';
77
78            return new MockResponse($xml, [
79                'http_code' => 200,
80            ]);
81        });
82        $transport = new SesApiTransport('ACCESS_KEY', 'SECRET_KEY', null, $client);
83        $transport->setPort(8984);
84
85        $mail = new Email();
86        $mail->subject('Hello!')
87            ->to(new Address('saif.gmati@symfony.com', 'Saif Eddin'))
88            ->from(new Address('fabpot@symfony.com', 'Fabien'))
89            ->text('Hello There!');
90
91        $message = $transport->send($mail);
92
93        $this->assertSame('foobar', $message->getMessageId());
94    }
95
96    public function testSendThrowsForErrorResponse()
97    {
98        $client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface {
99            $xml = "<SendEmailResponse xmlns=\"https://email.amazonaws.com/doc/2010-03-31/\">
100                <Error>
101                    <Message>i'm a teapot</Message>
102                    <Code>418</Code>
103                </Error>
104            </SendEmailResponse>";
105
106            return new MockResponse($xml, [
107                'http_code' => 418,
108            ]);
109        });
110        $transport = new SesApiTransport('ACCESS_KEY', 'SECRET_KEY', null, $client);
111        $transport->setPort(8984);
112
113        $mail = new Email();
114        $mail->subject('Hello!')
115            ->to(new Address('saif.gmati@symfony.com', 'Saif Eddin'))
116            ->from(new Address('fabpot@symfony.com', 'Fabien'))
117            ->text('Hello There!');
118
119        $this->expectException(HttpTransportException::class);
120        $this->expectExceptionMessage('Unable to send an email: i\'m a teapot (code 418).');
121        $transport->send($mail);
122    }
123}