Skip to content

Commit

Permalink
minor #35189 [Mailer] add tests for http transports (azjezz)
Browse files Browse the repository at this point in the history
This PR was merged into the 4.4 branch.

Discussion
----------

[Mailer] add tests for http transports

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | none
| License       | MIT

Commits
-------

df30a176ac [Mailer] add tests for http transports
  • Loading branch information
fabpot committed Jan 30, 2020
2 parents d1986e5 + 2fd0884 commit afaaca6
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions Tests/Transport/PostmarkApiTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -61,4 +65,59 @@ public function testCustomHeader()

$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);
}
}

0 comments on commit afaaca6

Please sign in to comment.