Passed
Pull Request — master (#5)
by Byron
02:32
created

SesSdkTransportTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 60
dl 0
loc 103
rs 10
c 1
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A toStringProvider() 0 17 1
A testSesError() 0 20 1
A testSend() 0 35 1
A testToString() 0 3 1
A createConfig() 0 10 1
1
<?php
2
3
/*
4
 * This file is part of the badams/symfony-amazon-sdk-mailer package.
5
 *
6
 * (c) Byron Adams <[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
use Badams\AmazonMailerSdk\SesSdkTransport;
13
use GuzzleHttp\Promise\Promise;
14
use Aws\Credentials\Credentials;
15
use Symfony\Component\Mime\Email;
16
use Aws\MockHandler;
17
use Aws\CommandInterface;
18
use Psr\Http\Message\RequestInterface;
19
use Symfony\Component\Mime\Address;
20
use Badams\AmazonMailerSdk\SesSdkTransportConfig;
21
22
23
class SesSdkTransportTest extends \PHPUnit\Framework\TestCase
24
{
25
    public function testSend()
26
    {
27
        $email = (new Email())
28
            ->from('[email protected]')
29
            ->addTo('[email protected]')
30
            ->addCc('[email protected]')
31
            ->addCc('[email protected]')
32
            ->addBcc('[email protected]')
33
            ->text('This is a test email');
34
35
        $mockHandler = function (CommandInterface $cmd) use ($email) {
36
            $data = $cmd->toArray();
37
            $this->assertEquals($email->getFrom(), [new Address($data['FromEmailAddress'])]);
38
            $this->assertArrayHasKey('Destination', $data);
39
            $this->assertEquals($email->getTo(), [new Address($data['Destination']['ToAddresses'][0])]);
40
            $this->assertEquals($email->getCc(), [
41
                new Address($data['Destination']['CcAddresses'][0]),
42
                new Address($data['Destination']['CcAddresses'][1])
43
            ]);
44
            $this->assertEquals($email->getBcc(), [new Address($data['Destination']['BccAddresses'][0])]);
45
46
            $this->assertArrayHasKey('Content', $data);
47
            $this->assertStringContainsString($email->getTextBody(), $data['Content']['Raw']['Data']);
0 ignored issues
show
Bug introduced by
It seems like $email->getTextBody() can also be of type null and resource; however, parameter $needle of PHPUnit\Framework\Assert...tStringContainsString() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

47
            $this->assertStringContainsString(/** @scrutinizer ignore-type */ $email->getTextBody(), $data['Content']['Raw']['Data']);
Loading history...
48
49
            return new \Aws\Result(['MessageId' => 'MESSAGE_ID_123']);
50
        };
51
52
        $transport = new SesSdkTransport(
53
            $this->createConfig('ACCESS_KEY', 'SECRET_KEY', 'eu-west-1'),
54
            null,
55
            null,
56
            $mockHandler);
57
58
        $sentMessage = $transport->send($email);
59
        $this->assertEquals('MESSAGE_ID_123', $sentMessage->getMessageId());
60
    }
61
62
    public function testSesError()
63
    {
64
        $email = (new Email())
65
            ->from('[email protected]')
66
            ->addTo('[email protected]')
67
            ->text('Test');
68
69
        $mockHandler = function (CommandInterface $cmd) {
70
            throw new \Aws\Ses\Exception\SesException('ERRR', $cmd);
71
        };
72
73
        $transport = new SesSdkTransport(
74
            $this->createConfig('ACCESS_KEY', 'SECRET_KEY', 'eu-west-1'),
75
            null,
76
            null,
77
            $mockHandler);
78
79
        $this->expectException(\Symfony\Component\Mailer\Exception\TransportException::class);
80
        $this->expectExceptionMessage('Unable to send an email: ERRR (code 0).');
81
        $transport->send($email);
82
    }
83
84
    /**
85
     * @dataProvider toStringProvider
86
     *
87
     * @param SesSdkTransport $transport
88
     * @param string $expected
89
     */
90
    public function testToString(SesSdkTransport $transport, string $expected)
91
    {
92
        $this->assertSame($expected, (string)$transport);
93
    }
94
95
96
    public function toStringProvider(): iterable
97
    {
98
        yield [
99
            new SesSdkTransport($this->createConfig('ACCESS_KEY', 'SECRET_KEY', 'eu-east-1')),
100
            'ses+sdk://ACCESS_KEY:SECRET_KEY@eu-east-1'
101
        ];
102
103
        yield [
104
            new SesSdkTransport($this->createConfig('ACCESS_KEY', 'SECRET_KEY', 'us-east-1')),
105
            'ses+sdk://ACCESS_KEY:SECRET_KEY@us-east-1'
106
        ];
107
108
        yield [
109
            new SesSdkTransport(new SesSdkTransportConfig(function () {
110
                return new \GuzzleHttp\Promise\RejectedPromise('bad things happened');
111
            }, 'eu-west-1')),
112
            'ses+sdk://:@eu-west-1'
113
        ];
114
    }
115
116
    private function createConfig($key, $secret, $region, $options = [])
117
    {
118
        return new SesSdkTransportConfig(
119
            function () use ($key, $secret) {
120
                $promise = new Promise();
121
                $promise->resolve(new Credentials($key, $secret));
122
                return $promise;
123
            },
124
            $region,
125
            $options
126
        );
127
    }
128
}