23/06/2018 - PHPUNIT, SYMFONY
Bu örnek, AWS SES servisini PHPUnit ile test eder, ancak istediğiniz herhangi bir servise uyarlayabilirsiniz.
parameters:
aws_sdk:
version: 'latest'
region: 'eu-west-1'
credentials:
key: 'AWS_KEY'
secret: 'AWS_SECRET'
services:
_defaults:
...
App\:
...
Aws\Sdk:
arguments:
- '%aws_sdk%'
declare(strict_types=1);
namespace App\Exception;
use RuntimeException;
class AwsException extends RuntimeException
{
}
declare(strict_types=1);
namespace App\Util\Aws;
use Aws\Sdk;
abstract class AwsAbstractUtil
{
protected $sdk;
public function __construct(Sdk $sdk)
{
$this->sdk = $sdk;
}
}
declare(strict_types=1);
namespace App\Util\Aws;
use App\Exception\AwsException;
use Aws\Result;
use Aws\Ses\Exception\SesException;
use Aws\Ses\SesClient;
class AwsSesUtil extends AwsAbstractUtil
{
private const CHARSET = 'UTF-8';
public function sendEmail(
iterable $toAddresses,
string $fromAddress,
string $subject,
string $body
): ?string {
try {
/** @var SesClient $client */
$client = $this->sdk->createSes();
/** @var Result $result */
$result = $client->sendEmail([
'Destination' => [
'ToAddresses' => $toAddresses,
],
'Source' => $fromAddress,
'Message' => [
'Subject' => [
'Charset' => self::CHARSET,
'Data' => $subject,
],
'Body' => [
'Text' => [
'Charset' => self::CHARSET,
'Data' => $body,
],
],
],
]);
return $result->get('MessageId');
} catch (SesException $e) {
throw new AwsException(sprintf('[AWS SES] %s', $e->getAwsErrorMessage()));
}
}
}
declare(strict_types=1);
namespace App\Tests\Util\Aws;
use App\Exception\AwsException;
use App\Util\Aws\AwsSesUtil;
use Aws\CommandInterface;
use Aws\Result;
use Aws\Sdk;
use Aws\Ses\Exception\SesException;
use Aws\Ses\SesClient;
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_MockObject;
class AwsSesUtilTest extends TestCase
{
/** @var Sdk|PHPUnit_Framework_MockObject_MockObject $sdkMock */
private $sdkMock;
/** @var SesClient|PHPUnit_Framework_MockObject_MockObject $sdkMock */
private $clientMock;
/** @var AwsSesUtil */
private $awsSesUtil;
protected function setUp()
{
$this->sdkMock = $this->getMockBuilder(Sdk::class)
->disableOriginalConstructor()
->setMethods(['createSes'])
->getMockForAbstractClass();
$this->clientMock = $this->getMockBuilder(SesClient::class)
->disableOriginalConstructor()
->setMethods(['sendEmail'])
->getMockForAbstractClass();
$this->awsSesUtil = new AwsSesUtil($this->sdkMock);
}
/**
* @test
*/
public function send_email_throws_exception_if_invalid_paramaters_are_given()
{
$to = ['to@email.com'];
$from = 'from@email.com';
$subject = 'Test';
$body = 'Just testing with PHPUnit';
$parameters = [
'Destination' => [
'ToAddresses' => $to,
],
'Source' => $from,
'Message' => [
'Subject' => [
'Charset' => 'UTF-8',
'Data' => $subject,
],
'Body' => [
'Text' => [
'Charset' => 'UTF-8',
'Data' => $body,
],
],
],
];
$this->sdkMock
->expects($this->once())
->method('createSes')
->willReturn($this->clientMock);
$exceptionMessage = 'Mock exception message';
$sesException = new SesException(
$exceptionMessage,
$this->createMock(CommandInterface::class)
);
$this->clientMock
->expects($this->once())
->method('sendEmail')
->with($parameters)
->willThrowException($sesException);
$this->expectException(AwsException::class);
$this->expectExceptionMessage(sprintf('[AWS SES] %s', $sesException->getAwsErrorMessage()));
$this->assertNull($this->awsSesUtil->sendEmail($to, $from, $subject, $body));
}
/**
* @test
*/
public function send_email_returns_message_id_after_success()
{
$messageId = 'MESSAGE_ID';
$to = ['to@email.com'];
$from = 'from@email.com';
$subject = 'Test';
$body = 'Just testing with PHPUnit';
$parameters = [
'Destination' => [
'ToAddresses' => $to,
],
'Source' => $from,
'Message' => [
'Subject' => [
'Charset' => 'UTF-8',
'Data' => $subject,
],
'Body' => [
'Text' => [
'Charset' => 'UTF-8',
'Data' => $body,
],
],
],
];
$this->sdkMock
->expects($this->once())
->method('createSes')
->willReturn($this->clientMock);
/** @var Result|PHPUnit_Framework_MockObject_MockObject $resultMock */
$resultMock = $this->createMock(Result::class);
$this->clientMock
->expects($this->once())
->method('sendEmail')
->with($parameters)
->willReturn($resultMock);
$resultMock
->expects($this->once())
->method('get')
->willReturn($messageId);
$this->assertSame($messageId, $this->awsSesUtil->sendEmail($to, $from, $subject, $body));
}
}
$ vendor/bin/phpunit --filter AwsSesUtilTest tests/Util/Aws/AwsSesUtilTest.php
PHPUnit 7.1.5 by Sebastian Bergmann and contributors.
.. 2 / 2 (100%)
Time: 141 ms, Memory: 4.00MB
OK (2 tests, 8 assertions)