-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adding tests for SES * Use Mocked responses * Minors * Bugfixes * cs
- Loading branch information
Showing
2 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |