Skip to content

Commit b839c91

Browse files
authored
Adding tests for SES (#129)
* Adding tests for SES * Use Mocked responses * Minors * Bugfixes * cs
1 parent 209d197 commit b839c91

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.EXPORT_ALL_VARIABLES:
2+
3+
initialize: start-docker
4+
start-docker:
5+
echo "Noop"
6+
7+
test: initialize
8+
./vendor/bin/simple-phpunit
9+
10+
clean: stop-docker
11+
stop-docker:
12+
echo "Noop"

Tests/Unit/SesClientTest.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AsyncAws\Ses\Tests\Unit;
6+
7+
use AsyncAws\Core\Exception\Http\ClientException;
8+
use AsyncAws\Ses\Input\Destination;
9+
use AsyncAws\Ses\Input\EmailContent;
10+
use AsyncAws\Ses\Input\Message;
11+
use AsyncAws\Ses\Input\SendEmailRequest;
12+
use AsyncAws\Ses\SesClient;
13+
use PHPUnit\Framework\TestCase;
14+
use Symfony\Component\HttpClient\MockHttpClient;
15+
use Symfony\Component\HttpClient\Response\MockResponse;
16+
17+
use Symfony\Contracts\HttpClient\ResponseInterface;
18+
19+
class SesClientTest extends TestCase
20+
{
21+
public function testSendMessage()
22+
{
23+
$httpClient = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface {
24+
$this->assertSame('POST', $method);
25+
$this->assertStringStartsWith('https://email.eu-west-1.amazonaws.com:8984/', $url);
26+
27+
$content = json_decode($options['body'], true);
28+
29+
$this->assertSame('Hello!', $content['Content']['Simple']['Subject']['Data']);
30+
$this->assertSame('[email protected]', $content['Destination']['ToAddresses'][0]);
31+
$this->assertSame('[email protected]', $content['FromEmailAddress']);
32+
$this->assertSame('Hello There!', $content['Content']['Simple']['Body']['Text']['Data']);
33+
34+
$json = '{"MessageId":"foobar"}';
35+
36+
return new MockResponse($json, [
37+
'http_code' => 200,
38+
]);
39+
});
40+
41+
$ses = new SesClient([
42+
'endpoint' => 'https://email.eu-west-1.amazonaws.com:8984',
43+
], null, $httpClient);
44+
45+
$input = new SendEmailRequest([
46+
'FromEmailAddress' => '[email protected]',
47+
]);
48+
$input->setDestination(new Destination([
49+
'ToAddresses' => ['[email protected]'],
50+
]));
51+
$input->setContent(new EmailContent([
52+
'Simple' => new Message([
53+
'Subject' => ['Data' => 'Hello!'],
54+
'Body' => ['Text' => ['Data' => 'Hello There!']],
55+
]),
56+
]));
57+
$result = $ses->sendEmail($input);
58+
59+
self::assertEquals('foobar', $result->getMessageId());
60+
}
61+
62+
public function testSendThrowsForErrorResponse()
63+
{
64+
$httpClient = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface {
65+
$json = '{"message":"Missing final \'@domain\'"}';
66+
67+
return new MockResponse($json, [
68+
'http_code' => 400,
69+
]);
70+
});
71+
$ses = new SesClient([], null, $httpClient);
72+
73+
$input = new SendEmailRequest([
74+
'FromEmailAddress' => 'foo', // no @test.se
75+
]);
76+
$input->setDestination(new Destination([
77+
'ToAddresses' => ['[email protected]'],
78+
]));
79+
$input->setContent(new EmailContent([
80+
'Simple' => new Message([
81+
'Subject' => ['Data' => 'Hello!'],
82+
'Body' => ['Text' => ['Data' => 'Hello There!']],
83+
]),
84+
]));
85+
$result = $ses->sendEmail($input);
86+
87+
$this->expectException(ClientException::class);
88+
$this->expectExceptionMessage('HTTP 400 returned');
89+
$result->resolve();
90+
}
91+
}

0 commit comments

Comments
 (0)