feature #32916 [Mailer] Add a name to the transports (fabpot)

This PR was merged into the 4.4 branch.

Discussion
----------

[Mailer] Add a name to the transports

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

Having a name for Transports helps identify them (useful for instance in the profiler when one uses several mailers).

Commits
-------

2412dfe71f [Mailer] added a name to the transport
This commit is contained in:
Fabien Potencier 2019-08-04 07:00:35 +02:00
commit 37265de54d
25 changed files with 191 additions and 0 deletions

View File

@ -42,6 +42,11 @@ class MailerTest extends AbstractWebTestCase
$this->onDoSend = $onDoSend;
}
public function getName(): string
{
return 'dummy://local';
}
protected function doSend(SentMessage $message): void
{
$onDoSend = $this->onDoSend;

View File

@ -74,6 +74,7 @@
"symfony/dom-crawler": "<4.3",
"symfony/form": "<4.3",
"symfony/lock": "<4.4",
"symfony/mailer": "<4.4",
"symfony/messenger": "<4.3",
"symfony/property-info": "<3.4",
"symfony/serializer": "<4.2",

View File

@ -43,6 +43,11 @@ class SesApiTransport extends AbstractApiTransport
parent::__construct($client, $dispatcher, $logger);
}
public function getName(): string
{
return sprintf('api://%s@ses?region=%s', $this->accessKey, $this->region);
}
protected function doSendApi(Email $email, SmtpEnvelope $envelope): ResponseInterface
{
$date = gmdate('D, d M Y H:i:s e');

View File

@ -42,6 +42,11 @@ class SesHttpTransport extends AbstractHttpTransport
parent::__construct($client, $dispatcher, $logger);
}
public function getName(): string
{
return sprintf('http://%s@ses?region=%s', $this->accessKey, $this->region);
}
protected function doSendHttp(SentMessage $message): ResponseInterface
{
$date = gmdate('D, d M Y H:i:s e');

View File

@ -36,6 +36,11 @@ class MandrillApiTransport extends AbstractApiTransport
parent::__construct($client, $dispatcher, $logger);
}
public function getName(): string
{
return sprintf('api://mandrill');
}
protected function doSendApi(Email $email, SmtpEnvelope $envelope): ResponseInterface
{
$response = $this->client->request('POST', self::ENDPOINT, [

View File

@ -34,6 +34,11 @@ class MandrillHttpTransport extends AbstractHttpTransport
parent::__construct($client, $dispatcher, $logger);
}
public function getName(): string
{
return sprintf('http://mandrill');
}
protected function doSendHttp(SentMessage $message): ResponseInterface
{
$envelope = $message->getEnvelope();

View File

@ -41,6 +41,11 @@ class MailgunApiTransport extends AbstractApiTransport
parent::__construct($client, $dispatcher, $logger);
}
public function getName(): string
{
return sprintf('api://%s@mailgun?region=%s', $this->domain, $this->region);
}
protected function doSendApi(Email $email, SmtpEnvelope $envelope): ResponseInterface
{
$body = new FormDataPart($this->getPayload($email, $envelope));

View File

@ -40,6 +40,11 @@ class MailgunHttpTransport extends AbstractHttpTransport
parent::__construct($client, $dispatcher, $logger);
}
public function getName(): string
{
return sprintf('http://%s@mailgun?region=%s', $this->domain, $this->region);
}
protected function doSendHttp(SentMessage $message): ResponseInterface
{
$body = new FormDataPart([

View File

@ -36,6 +36,11 @@ class PostmarkApiTransport extends AbstractApiTransport
parent::__construct($client, $dispatcher, $logger);
}
public function getName(): string
{
return sprintf('api://postmark');
}
protected function doSendApi(Email $email, SmtpEnvelope $envelope): ResponseInterface
{
$response = $this->client->request('POST', self::ENDPOINT, [

View File

@ -37,6 +37,11 @@ class SendgridApiTransport extends AbstractApiTransport
parent::__construct($client, $dispatcher, $logger);
}
public function getName(): string
{
return sprintf('api://sendgrid');
}
protected function doSendApi(Email $email, SmtpEnvelope $envelope): ResponseInterface
{
$response = $this->client->request('POST', self::ENDPOINT, [

View File

@ -4,6 +4,7 @@ CHANGELOG
4.4.0
-----
* [BC BREAK] `TransportInterface` has a new `getName()` method
* [BC BREAK] Classes `AbstractApiTransport` and `AbstractHttpTransport` moved under `Transport` sub-namespace.
* [BC BREAK] Transports depend on `Symfony\Contracts\EventDispatcher\EventDispatcherInterface`
instead of `Symfony\Component\EventDispatcher\EventDispatcherInterface`.

View File

@ -69,6 +69,9 @@ abstract class TransportFactoryTestCase extends TestCase
$factory = $this->getFactory();
$this->assertEquals($transport, $factory->create($dsn));
if ('smtp' !== $dsn->getScheme()) {
$this->assertStringMatchesFormat($dsn->getScheme().'://%S'.$dsn->getHost().'%S', $transport->getName());
}
}
/**

View File

@ -29,6 +29,16 @@ class FailoverTransportTest extends TestCase
new FailoverTransport([]);
}
public function testGetName()
{
$t1 = $this->createMock(TransportInterface::class);
$t1->expects($this->once())->method('getName')->willReturn('t1://local');
$t2 = $this->createMock(TransportInterface::class);
$t2->expects($this->once())->method('getName')->willReturn('t2://local');
$t = new FailoverTransport([$t1, $t2]);
$this->assertEquals('t1://local || t2://local', $t->getName());
}
public function testSendFirstWork()
{
$t1 = $this->createMock(TransportInterface::class);

View File

@ -0,0 +1,24 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Mailer\Tests\Transport;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Mailer\Transport\NullTransport;
class NullTransportTest extends TestCase
{
public function testName()
{
$t = new NullTransport();
$this->assertEquals('smtp://null', $t->getName());
}
}

View File

@ -28,6 +28,16 @@ class RoundRobinTransportTest extends TestCase
new RoundRobinTransport([]);
}
public function testGetName()
{
$t1 = $this->createMock(TransportInterface::class);
$t1->expects($this->once())->method('getName')->willReturn('t1://local');
$t2 = $this->createMock(TransportInterface::class);
$t2->expects($this->once())->method('getName')->willReturn('t2://local');
$t = new RoundRobinTransport([$t1, $t2]);
$this->assertEquals('t1://local && t2://local', $t->getName());
}
public function testSendAlternate()
{
$t1 = $this->createMock(TransportInterface::class);

View File

@ -0,0 +1,24 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Mailer\Tests\Transport;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Mailer\Transport\SendmailTransport;
class SendmailTransportTest extends TestCase
{
public function testName()
{
$t = new SendmailTransport();
$this->assertEquals('smtp://sendmail', $t->getName());
}
}

View File

@ -0,0 +1,28 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Mailer\Tests\Transport\Smtp;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Mailer\Transport\Smtp\SmtpTransport;
use Symfony\Component\Mailer\Transport\Smtp\Stream\SocketStream;
class SmtpTransportTest extends TestCase
{
public function testName()
{
$t = new SmtpTransport();
$this->assertEquals('smtp://localhost:25', $t->getName());
$t = new SmtpTransport((new SocketStream())->setHost('127.0.0.1')->setPort(2525));
$this->assertEquals('smtp://127.0.0.1:2525', $t->getName());
}
}

View File

@ -68,6 +68,11 @@ class DummyTransport implements Transport\TransportInterface
{
throw new \BadMethodCallException('This method newer should be called.');
}
public function getName(): string
{
return sprintf('dummy://local');
}
}
class DummyTransportFactory implements Transport\TransportFactoryInterface

View File

@ -39,6 +39,8 @@ abstract class AbstractTransport implements TransportInterface
$this->logger = $logger ?: new NullLogger();
}
abstract public function getName(): string;
/**
* Sets the maximum number of messages to send per second (0 to disable).
*/

View File

@ -28,4 +28,9 @@ class FailoverTransport extends RoundRobinTransport
return $this->currentTransport;
}
protected function getNameSymbol(): string
{
return '||';
}
}

View File

@ -23,4 +23,9 @@ final class NullTransport extends AbstractTransport
protected function doSend(SentMessage $message): void
{
}
public function getName(): string
{
return 'smtp://null';
}
}

View File

@ -56,6 +56,13 @@ class RoundRobinTransport implements TransportInterface
throw new TransportException('All transports failed.');
}
public function getName(): string
{
return implode(' '.$this->getNameSymbol().' ', array_map(function (TransportInterface $transport) {
return $transport->getName();
}, $this->transports));
}
/**
* Rotates the transport list around and returns the first instance.
*/
@ -90,6 +97,11 @@ class RoundRobinTransport implements TransportInterface
return $this->deadTransports->contains($transport);
}
protected function getNameSymbol(): string
{
return '&&';
}
private function moveCursor(int $cursor): int
{
return ++$cursor >= \count($this->transports) ? 0 : $cursor;

View File

@ -73,6 +73,11 @@ class SendmailTransport extends AbstractTransport
return parent::send($message, $envelope);
}
public function getName(): string
{
return $this->transport->getName();
}
protected function doSend(SentMessage $message): void
{
$this->getLogger()->debug(sprintf('Email transport "%s" starting', __CLASS__));

View File

@ -126,6 +126,15 @@ class SmtpTransport extends AbstractTransport
return $message;
}
public function getName(): string
{
if ($this->stream instanceof SocketStream) {
return sprintf('smtp://%s:%d', $this->stream->getHost(), $this->stream->getPort());
}
return sprintf('smtp://sendmail');
}
/**
* Runs a command against the stream, expecting the given response codes.
*

View File

@ -30,4 +30,6 @@ interface TransportInterface
* @throws TransportExceptionInterface
*/
public function send(RawMessage $message, SmtpEnvelope $envelope = null): ?SentMessage;
public function getName(): string;
}