Skip to content

Commit

Permalink
Adding tests for SES (#129)
Browse files Browse the repository at this point in the history
* Adding tests for SES

* Use Mocked responses

* Minors

* Bugfixes

* cs
  • Loading branch information
Nyholm authored Feb 22, 2020
1 parent 209d197 commit b839c91
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.EXPORT_ALL_VARIABLES:

initialize: start-docker
start-docker:
echo "Noop"

test: initialize
./vendor/bin/simple-phpunit

clean: stop-docker
stop-docker:
echo "Noop"
91 changes: 91 additions & 0 deletions Tests/Unit/SesClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

declare(strict_types=1);

namespace AsyncAws\Ses\Tests\Unit;

use AsyncAws\Core\Exception\Http\ClientException;
use AsyncAws\Ses\Input\Destination;
use AsyncAws\Ses\Input\EmailContent;
use AsyncAws\Ses\Input\Message;
use AsyncAws\Ses\Input\SendEmailRequest;
use AsyncAws\Ses\SesClient;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;

use Symfony\Contracts\HttpClient\ResponseInterface;

class SesClientTest extends TestCase
{
public function testSendMessage()
{
$httpClient = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface {
$this->assertSame('POST', $method);
$this->assertStringStartsWith('https://email.eu-west-1.amazonaws.com:8984/', $url);

$content = json_decode($options['body'], true);

$this->assertSame('Hello!', $content['Content']['Simple']['Subject']['Data']);
$this->assertSame('[email protected]', $content['Destination']['ToAddresses'][0]);
$this->assertSame('[email protected]', $content['FromEmailAddress']);
$this->assertSame('Hello There!', $content['Content']['Simple']['Body']['Text']['Data']);

$json = '{"MessageId":"foobar"}';

return new MockResponse($json, [
'http_code' => 200,
]);
});

$ses = new SesClient([
'endpoint' => 'https://email.eu-west-1.amazonaws.com:8984',
], null, $httpClient);

$input = new SendEmailRequest([
'FromEmailAddress' => '[email protected]',
]);
$input->setDestination(new Destination([
'ToAddresses' => ['[email protected]'],
]));
$input->setContent(new EmailContent([
'Simple' => new Message([
'Subject' => ['Data' => 'Hello!'],
'Body' => ['Text' => ['Data' => 'Hello There!']],
]),
]));
$result = $ses->sendEmail($input);

self::assertEquals('foobar', $result->getMessageId());
}

public function testSendThrowsForErrorResponse()
{
$httpClient = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface {
$json = '{"message":"Missing final \'@domain\'"}';

return new MockResponse($json, [
'http_code' => 400,
]);
});
$ses = new SesClient([], null, $httpClient);

$input = new SendEmailRequest([
'FromEmailAddress' => 'foo', // no @test.se
]);
$input->setDestination(new Destination([
'ToAddresses' => ['[email protected]'],
]));
$input->setContent(new EmailContent([
'Simple' => new Message([
'Subject' => ['Data' => 'Hello!'],
'Body' => ['Text' => ['Data' => 'Hello There!']],
]),
]));
$result = $ses->sendEmail($input);

$this->expectException(ClientException::class);
$this->expectExceptionMessage('HTTP 400 returned');
$result->resolve();
}
}

0 comments on commit b839c91

Please sign in to comment.