[Mailer] add tests for http transports

This commit is contained in:
azjezz 2020-01-03 13:06:55 +01:00
parent ee5e64413e
commit df30a176ac
7 changed files with 460 additions and 0 deletions

View File

@ -12,7 +12,13 @@
namespace Symfony\Component\Mailer\Bridge\Amazon\Tests\Transport;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\Mailer\Bridge\Amazon\Transport\SesApiTransport;
use Symfony\Component\Mailer\Exception\HttpTransportException;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Contracts\HttpClient\ResponseInterface;
class SesApiTransportTest extends TestCase
{
@ -45,4 +51,70 @@ class SesApiTransportTest extends TestCase
],
];
}
public function testSend()
{
$client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface {
$this->assertSame('POST', $method);
$this->assertSame('https://email.eu-west-1.amazonaws.com:8984/', $url);
$this->assertStringContainsStringIgnoringCase('X-Amzn-Authorization: AWS3-HTTPS AWSAccessKeyId=ACCESS_KEY,Algorithm=HmacSHA256,Signature=', $options['headers'][0] ?? $options['request_headers'][0]);
parse_str($options['body'], $content);
$this->assertSame('Hello!', $content['Message_Subject_Data']);
$this->assertSame('Saif Eddin <saif.gmati@symfony.com>', $content['Destination_ToAddresses_member'][0]);
$this->assertSame('Fabien <fabpot@symfony.com>', $content['Source']);
$this->assertSame('Hello There!', $content['Message_Body_Text_Data']);
$xml = '<SendEmailResponse xmlns="https://email.amazonaws.com/doc/2010-03-31/">
<SendEmailResult>
<MessageId>foobar</MessageId>
</SendEmailResult>
</SendEmailResponse>';
return new MockResponse($xml, [
'http_code' => 200,
]);
});
$transport = new SesApiTransport('ACCESS_KEY', 'SECRET_KEY', null, $client);
$transport->setPort(8984);
$mail = new Email();
$mail->subject('Hello!')
->to(new Address('saif.gmati@symfony.com', 'Saif Eddin'))
->from(new Address('fabpot@symfony.com', 'Fabien'))
->text('Hello There!');
$message = $transport->send($mail);
$this->assertSame('foobar', $message->getMessageId());
}
public function testSendThrowsForErrorResponse()
{
$client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface {
$xml = "<SendEmailResponse xmlns=\"https://email.amazonaws.com/doc/2010-03-31/\">
<Error>
<Message>i'm a teapot</Message>
<Code>418</Code>
</Error>
</SendEmailResponse>";
return new MockResponse($xml, [
'http_code' => 418,
]);
});
$transport = new SesApiTransport('ACCESS_KEY', 'SECRET_KEY', null, $client);
$transport->setPort(8984);
$mail = new Email();
$mail->subject('Hello!')
->to(new Address('saif.gmati@symfony.com', 'Saif Eddin'))
->from(new Address('fabpot@symfony.com', 'Fabien'))
->text('Hello There!');
$this->expectException(HttpTransportException::class);
$this->expectExceptionMessage('Unable to send an email: i\'m a teapot (code 418).');
$transport->send($mail);
}
}

View File

@ -12,7 +12,13 @@
namespace Symfony\Component\Mailer\Bridge\Amazon\Tests\Transport;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\Mailer\Bridge\Amazon\Transport\SesHttpTransport;
use Symfony\Component\Mailer\Exception\HttpTransportException;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Contracts\HttpClient\ResponseInterface;
class SesHttpTransportTest extends TestCase
{
@ -45,4 +51,70 @@ class SesHttpTransportTest extends TestCase
],
];
}
public function testSend()
{
$client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface {
$this->assertSame('POST', $method);
$this->assertSame('https://email.eu-west-1.amazonaws.com:8984/', $url);
$this->assertStringContainsString('AWS3-HTTPS AWSAccessKeyId=ACCESS_KEY,Algorithm=HmacSHA256,Signature=', $options['headers'][0] ?? $options['request_headers'][0]);
parse_str($options['body'], $body);
$content = base64_decode($body['RawMessage_Data']);
$this->assertStringContainsString('Hello!', $content);
$this->assertStringContainsString('Saif Eddin <saif.gmati@symfony.com>', $content);
$this->assertStringContainsString('Fabien <fabpot@symfony.com>', $content);
$this->assertStringContainsString('Hello There!', $content);
$xml = '<SendEmailResponse xmlns="https://email.amazonaws.com/doc/2010-03-31/">
<SendRawEmailResult>
<MessageId>foobar</MessageId>
</SendRawEmailResult>
</SendEmailResponse>';
return new MockResponse($xml, [
'http_code' => 200,
]);
});
$transport = new SesHttpTransport('ACCESS_KEY', 'SECRET_KEY', null, $client);
$transport->setPort(8984);
$mail = new Email();
$mail->subject('Hello!')
->to(new Address('saif.gmati@symfony.com', 'Saif Eddin'))
->from(new Address('fabpot@symfony.com', 'Fabien'))
->text('Hello There!');
$message = $transport->send($mail);
$this->assertSame('foobar', $message->getMessageId());
}
public function testSendThrowsForErrorResponse()
{
$client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface {
$xml = "<SendEmailResponse xmlns=\"https://email.amazonaws.com/doc/2010-03-31/\">
<Error>
<Message>i'm a teapot</Message>
<Code>418</Code>
</Error>
</SendEmailResponse>";
return new MockResponse($xml, [
'http_code' => 418,
]);
});
$transport = new SesHttpTransport('ACCESS_KEY', 'SECRET_KEY', null, $client);
$mail = new Email();
$mail->subject('Hello!')
->to(new Address('saif.gmati@symfony.com', 'Saif Eddin'))
->from(new Address('fabpot@symfony.com', 'Fabien'))
->text('Hello There!');
$this->expectException(HttpTransportException::class);
$this->expectExceptionMessage('Unable to send an email: i\'m a teapot (code 418).');
$transport->send($mail);
}
}

View File

@ -12,10 +12,14 @@
namespace Symfony\Component\Mailer\Bridge\Mailchimp\Tests\Transport;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\Mailer\Bridge\Mailchimp\Transport\MandrillApiTransport;
use Symfony\Component\Mailer\Envelope;
use Symfony\Component\Mailer\Exception\HttpTransportException;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Contracts\HttpClient\ResponseInterface;
class MandrillApiTransportTest extends TestCase
{
@ -61,4 +65,59 @@ class MandrillApiTransportTest extends TestCase
$this->assertCount(1, $payload['message']['headers']);
$this->assertEquals('foo: bar', $payload['message']['headers'][0]);
}
public function testSend()
{
$client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface {
$this->assertSame('POST', $method);
$this->assertSame('https://mandrillapp.com/api/1.0/messages/send.json', $url);
$body = json_decode($options['body'], true);
$message = $body['message'];
$this->assertSame('KEY', $body['key']);
$this->assertSame('Fabien', $message['from_name']);
$this->assertSame('fabpot@symfony.com', $message['from_email']);
$this->assertSame('Saif Eddin', $message['to'][0]['name']);
$this->assertSame('saif.gmati@symfony.com', $message['to'][0]['email']);
$this->assertSame('Hello!', $message['subject']);
$this->assertSame('Hello There!', $message['text']);
return new MockResponse(json_encode([['_id' => 'foobar']]), [
'http_code' => 200,
]);
});
$transport = new MandrillApiTransport('KEY', $client);
$mail = new Email();
$mail->subject('Hello!')
->to(new Address('saif.gmati@symfony.com', 'Saif Eddin'))
->from(new Address('fabpot@symfony.com', 'Fabien'))
->text('Hello There!');
$message = $transport->send($mail);
$this->assertSame('foobar', $message->getMessageId());
}
public function testSendThrowsForErrorResponse()
{
$client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface {
return new MockResponse(json_encode(['status' => 'error', 'message' => 'i\'m a teapot', 'code' => 418]), [
'http_code' => 418,
]);
});
$transport = new MandrillApiTransport('KEY', $client);
$mail = new Email();
$mail->subject('Hello!')
->to(new Address('saif.gmati@symfony.com', 'Saif Eddin'))
->from(new Address('fabpot@symfony.com', 'Fabien'))
->text('Hello There!');
$this->expectException(HttpTransportException::class);
$this->expectExceptionMessage('Unable to send an email: i\'m a teapot (code 418).');
$transport->send($mail);
}
}

View File

@ -12,7 +12,13 @@
namespace Symfony\Component\Mailer\Bridge\Mailchimp\Tests\Transport;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\Mailer\Bridge\Mailchimp\Transport\MandrillHttpTransport;
use Symfony\Component\Mailer\Exception\HttpTransportException;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Contracts\HttpClient\ResponseInterface;
class MandrillHttpTransportTest extends TestCase
{
@ -41,4 +47,60 @@ class MandrillHttpTransportTest extends TestCase
],
];
}
public function testSend()
{
$client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface {
$this->assertSame('POST', $method);
$this->assertSame('https://mandrillapp.com/api/1.0/messages/send-raw.json', $url);
$body = json_decode($options['body'], true);
$message = $body['raw_message'];
$this->assertSame('KEY', $body['key']);
$this->assertSame('Saif Eddin <saif.gmati@symfony.com>', $body['to'][0]);
$this->assertSame('Fabien <fabpot@symfony.com>', $body['from_email']);
$this->assertStringContainsString('Subject: Hello!', $message);
$this->assertStringContainsString('To: Saif Eddin <saif.gmati@symfony.com>', $message);
$this->assertStringContainsString('From: Fabien <fabpot@symfony.com>', $message);
$this->assertStringContainsString('Hello There!', $message);
return new MockResponse(json_encode([['_id' => 'foobar']]), [
'http_code' => 200,
]);
});
$transport = new MandrillHttpTransport('KEY', $client);
$mail = new Email();
$mail->subject('Hello!')
->to(new Address('saif.gmati@symfony.com', 'Saif Eddin'))
->from(new Address('fabpot@symfony.com', 'Fabien'))
->text('Hello There!');
$message = $transport->send($mail);
$this->assertSame('foobar', $message->getMessageId());
}
public function testSendThrowsForErrorResponse()
{
$client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface {
return new MockResponse(json_encode(['status' => 'error', 'message' => 'i\'m a teapot', 'code' => 418]), [
'http_code' => 418,
]);
});
$transport = new MandrillHttpTransport('KEY', $client);
$mail = new Email();
$mail->subject('Hello!')
->to(new Address('saif.gmati@symfony.com', 'Saif Eddin'))
->from(new Address('fabpot@symfony.com', 'Fabien'))
->text('Hello There!');
$this->expectException(HttpTransportException::class);
$this->expectExceptionMessage('Unable to send an email: i\'m a teapot (code 418).');
$transport->send($mail);
}
}

View File

@ -12,10 +12,14 @@
namespace Symfony\Component\Mailer\Bridge\Mailgun\Tests\Transport;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\Mailer\Bridge\Mailgun\Transport\MailgunApiTransport;
use Symfony\Component\Mailer\Envelope;
use Symfony\Component\Mailer\Exception\HttpTransportException;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Contracts\HttpClient\ResponseInterface;
class MailgunApiTransportTest extends TestCase
{
@ -64,4 +68,67 @@ class MailgunApiTransportTest extends TestCase
$this->assertArrayHasKey('h:x-mailgun-variables', $payload);
$this->assertEquals($json, $payload['h:x-mailgun-variables']);
}
public function testSend()
{
$client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface {
$this->assertSame('POST', $method);
$this->assertSame('https://api.us-east-1.mailgun.net:8984/v3/symfony/messages', $url);
$this->assertStringContainsString('Basic YXBpOkFDQ0VTU19LRVk=', $options['headers'][2] ?? $options['request_headers'][1]);
$content = '';
while ($chunk = $options['body']()) {
$content .= $chunk;
}
$this->assertStringContainsString('Hello!', $content);
$this->assertStringContainsString('Saif Eddin <saif.gmati@symfony.com>', $content);
$this->assertStringContainsString('Fabien <fabpot@symfony.com>', $content);
$this->assertStringContainsString('Hello There!', $content);
return new MockResponse(json_encode(['id' => 'foobar']), [
'http_code' => 200,
]);
});
$transport = new MailgunApiTransport('ACCESS_KEY', 'symfony', 'us-east-1', $client);
$transport->setPort(8984);
$mail = new Email();
$mail->subject('Hello!')
->to(new Address('saif.gmati@symfony.com', 'Saif Eddin'))
->from(new Address('fabpot@symfony.com', 'Fabien'))
->text('Hello There!');
$message = $transport->send($mail);
$this->assertSame('foobar', $message->getMessageId());
}
public function testSendThrowsForErrorResponse()
{
$client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface {
$this->assertSame('POST', $method);
$this->assertSame('https://api.mailgun.net:8984/v3/symfony/messages', $url);
$this->assertStringContainsStringIgnoringCase('Authorization: Basic YXBpOkFDQ0VTU19LRVk=', $options['headers'][2] ?? $options['request_headers'][1]);
return new MockResponse(json_encode(['message' => 'i\'m a teapot']), [
'http_code' => 418,
'response_headers' => [
'content-type' => 'application/json',
],
]);
});
$transport = new MailgunApiTransport('ACCESS_KEY', 'symfony', 'us', $client);
$transport->setPort(8984);
$mail = new Email();
$mail->subject('Hello!')
->to(new Address('saif.gmati@symfony.com', 'Saif Eddin'))
->from(new Address('fabpot@symfony.com', 'Fabien'))
->text('Hello There!');
$this->expectException(HttpTransportException::class);
$this->expectExceptionMessage('Unable to send an email: i\'m a teapot (code 418).');
$transport->send($mail);
}
}

View File

@ -12,7 +12,13 @@
namespace Symfony\Component\Mailer\Bridge\Mailgun\Tests\Transport;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\Mailer\Bridge\Mailgun\Transport\MailgunHttpTransport;
use Symfony\Component\Mailer\Exception\HttpTransportException;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Contracts\HttpClient\ResponseInterface;
class MailgunHttpTransportTest extends TestCase
{
@ -45,4 +51,67 @@ class MailgunHttpTransportTest extends TestCase
],
];
}
public function testSend()
{
$client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface {
$this->assertSame('POST', $method);
$this->assertSame('https://api.us-east-1.mailgun.net:8984/v3/symfony/messages.mime', $url);
$this->assertStringContainsString('Basic YXBpOkFDQ0VTU19LRVk=', $options['headers'][2] ?? $options['request_headers'][1]);
$content = '';
while ($chunk = $options['body']()) {
$content .= $chunk;
}
$this->assertStringContainsString('Subject: Hello!', $content);
$this->assertStringContainsString('To: Saif Eddin <saif.gmati@symfony.com>', $content);
$this->assertStringContainsString('From: Fabien <fabpot@symfony.com>', $content);
$this->assertStringContainsString('Hello There!', $content);
return new MockResponse(json_encode(['id' => 'foobar']), [
'http_code' => 200,
]);
});
$transport = new MailgunHttpTransport('ACCESS_KEY', 'symfony', 'us-east-1', $client);
$transport->setPort(8984);
$mail = new Email();
$mail->subject('Hello!')
->to(new Address('saif.gmati@symfony.com', 'Saif Eddin'))
->from(new Address('fabpot@symfony.com', 'Fabien'))
->text('Hello There!');
$message = $transport->send($mail);
$this->assertSame('foobar', $message->getMessageId());
}
public function testSendThrowsForErrorResponse()
{
$client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface {
$this->assertSame('POST', $method);
$this->assertSame('https://api.mailgun.net:8984/v3/symfony/messages.mime', $url);
$this->assertStringContainsString('Basic YXBpOkFDQ0VTU19LRVk=', $options['headers'][2] ?? $options['request_headers'][1]);
return new MockResponse(json_encode(['message' => 'i\'m a teapot']), [
'http_code' => 418,
'response_headers' => [
'content-type' => 'application/json',
],
]);
});
$transport = new MailgunHttpTransport('ACCESS_KEY', 'symfony', 'us', $client);
$transport->setPort(8984);
$mail = new Email();
$mail->subject('Hello!')
->to(new Address('saif.gmati@symfony.com', 'Saif Eddin'))
->from(new Address('fabpot@symfony.com', 'Fabien'))
->text('Hello There!');
$this->expectException(HttpTransportException::class);
$this->expectExceptionMessage('Unable to send an email: i\'m a teapot (code 418).');
$transport->send($mail);
}
}

View File

@ -12,10 +12,14 @@
namespace Symfony\Component\Mailer\Bridge\Postmark\Tests\Transport;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\Mailer\Bridge\Postmark\Transport\PostmarkApiTransport;
use Symfony\Component\Mailer\Envelope;
use Symfony\Component\Mailer\Exception\HttpTransportException;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Contracts\HttpClient\ResponseInterface;
class PostmarkApiTransportTest extends TestCase
{
@ -61,4 +65,59 @@ class PostmarkApiTransportTest extends TestCase
$this->assertEquals(['Name' => 'foo', 'Value' => 'bar'], $payload['Headers'][0]);
}
public function testSend()
{
$client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface {
$this->assertSame('POST', $method);
$this->assertSame('https://api.postmarkapp.com/email', $url);
$this->assertStringContainsStringIgnoringCase('X-Postmark-Server-Token: KEY', $options['headers'][1] ?? $options['request_headers'][1]);
$body = json_decode($options['body'], true);
$this->assertSame('Fabien <fabpot@symfony.com>', $body['From']);
$this->assertSame('Saif Eddin <saif.gmati@symfony.com>', $body['To']);
$this->assertSame('Hello!', $body['Subject']);
$this->assertSame('Hello There!', $body['TextBody']);
return new MockResponse(json_encode(['MessageID' => 'foobar']), [
'http_code' => 200,
]);
});
$transport = new PostmarkApiTransport('KEY', $client);
$mail = new Email();
$mail->subject('Hello!')
->to(new Address('saif.gmati@symfony.com', 'Saif Eddin'))
->from(new Address('fabpot@symfony.com', 'Fabien'))
->text('Hello There!');
$message = $transport->send($mail);
$this->assertSame('foobar', $message->getMessageId());
}
public function testSendThrowsForErrorResponse()
{
$client = new MockHttpClient(static function (string $method, string $url, array $options): ResponseInterface {
return new MockResponse(json_encode(['Message' => 'i\'m a teapot', 'ErrorCode' => 418]), [
'http_code' => 418,
'response_headers' => [
'content-type' => 'application/json',
],
]);
});
$transport = new PostmarkApiTransport('KEY', $client);
$transport->setPort(8984);
$mail = new Email();
$mail->subject('Hello!')
->to(new Address('saif.gmati@symfony.com', 'Saif Eddin'))
->from(new Address('fabpot@symfony.com', 'Fabien'))
->text('Hello There!');
$this->expectException(HttpTransportException::class);
$this->expectExceptionMessage('Unable to send an email: i\'m a teapot (code 418).');
$transport->send($mail);
}
}