|
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']); |
|
|
|
|
|
|
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
|
|
|
public function testConfigurationSetName() |
|
85
|
|
|
{ |
|
86
|
|
|
$email = (new Email()) |
|
87
|
|
|
->from('[email protected]') |
|
88
|
|
|
->addTo('[email protected]') |
|
89
|
|
|
->text('Test'); |
|
90
|
|
|
|
|
91
|
|
|
$mockHandler = function (CommandInterface $cmd) { |
|
92
|
|
|
$data = $cmd->toArray(); |
|
93
|
|
|
$this->assertArrayHasKey('ConfigurationSetName', $data); |
|
94
|
|
|
$this->assertEquals('MyConfigSet', $data['ConfigurationSetName']); |
|
95
|
|
|
return new \Aws\Result(); |
|
96
|
|
|
}; |
|
97
|
|
|
|
|
98
|
|
|
$transport = new SesSdkTransport( |
|
99
|
|
|
$this->createConfig('ACCESS_KEY', 'SECRET_KEY', 'eu-west-1', [ |
|
100
|
|
|
'ConfigurationSetName' => 'MyConfigSet' |
|
101
|
|
|
]), |
|
102
|
|
|
null, |
|
103
|
|
|
null, |
|
104
|
|
|
$mockHandler); |
|
105
|
|
|
|
|
106
|
|
|
$transport->send($email); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @dataProvider toStringProvider |
|
111
|
|
|
* |
|
112
|
|
|
* @param SesSdkTransport $transport |
|
113
|
|
|
* @param string $expected |
|
114
|
|
|
*/ |
|
115
|
|
|
public function testToString(SesSdkTransport $transport, string $expected) |
|
116
|
|
|
{ |
|
117
|
|
|
$this->assertSame($expected, (string)$transport); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
public function toStringProvider(): iterable |
|
122
|
|
|
{ |
|
123
|
|
|
yield [ |
|
124
|
|
|
new SesSdkTransport($this->createConfig('ACCESS_KEY', 'SECRET_KEY', 'eu-east-1')), |
|
125
|
|
|
'ses+sdk://ACCESS_KEY:SECRET_KEY@eu-east-1' |
|
126
|
|
|
]; |
|
127
|
|
|
|
|
128
|
|
|
yield [ |
|
129
|
|
|
new SesSdkTransport($this->createConfig('ACCESS_KEY', 'SECRET_KEY', 'us-east-1')), |
|
130
|
|
|
'ses+sdk://ACCESS_KEY:SECRET_KEY@us-east-1' |
|
131
|
|
|
]; |
|
132
|
|
|
|
|
133
|
|
|
yield [ |
|
134
|
|
|
new SesSdkTransport(new SesSdkTransportConfig(function () { |
|
135
|
|
|
return new \GuzzleHttp\Promise\RejectedPromise('bad things happened'); |
|
136
|
|
|
}, 'eu-west-1')), |
|
137
|
|
|
'ses+sdk://:@eu-west-1' |
|
138
|
|
|
]; |
|
139
|
|
|
|
|
140
|
|
|
yield [ |
|
141
|
|
|
new SesSdkTransport($this->createConfig('ACCESS_KEY', 'SECRET_KEY', 'us-east-1', [ |
|
142
|
|
|
'ConfigurationSetName' => 'Foobar' |
|
143
|
|
|
])), |
|
144
|
|
|
'ses+sdk://ACCESS_KEY:SECRET_KEY@us-east-1?ConfigurationSetName=Foobar' |
|
145
|
|
|
]; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
private function createConfig($key, $secret, $region, $options = []) |
|
149
|
|
|
{ |
|
150
|
|
|
return new SesSdkTransportConfig( |
|
151
|
|
|
function () use ($key, $secret) { |
|
152
|
|
|
$promise = new Promise(); |
|
153
|
|
|
$promise->resolve(new Credentials($key, $secret)); |
|
154
|
|
|
return $promise; |
|
155
|
|
|
}, |
|
156
|
|
|
$region, |
|
157
|
|
|
$options |
|
158
|
|
|
); |
|
159
|
|
|
} |
|
160
|
|
|
} |