From 2412dfe71fed77b946ad300784d758d380518f42 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 4 Aug 2019 05:52:49 +0200 Subject: [PATCH] [Mailer] added a name to the transport --- .../Tests/Functional/MailerTest.php | 5 ++++ .../Bundle/FrameworkBundle/composer.json | 1 + .../Amazon/Transport/SesApiTransport.php | 5 ++++ .../Amazon/Transport/SesHttpTransport.php | 5 ++++ .../Transport/MandrillApiTransport.php | 5 ++++ .../Transport/MandrillHttpTransport.php | 5 ++++ .../Mailgun/Transport/MailgunApiTransport.php | 5 ++++ .../Transport/MailgunHttpTransport.php | 5 ++++ .../Transport/PostmarkApiTransport.php | 5 ++++ .../Transport/SendgridApiTransport.php | 5 ++++ src/Symfony/Component/Mailer/CHANGELOG.md | 1 + .../Mailer/Test/TransportFactoryTestCase.php | 3 ++ .../Tests/Transport/FailoverTransportTest.php | 10 +++++++ .../Tests/Transport/NullTransportTest.php | 24 ++++++++++++++++ .../Transport/RoundRobinTransportTest.php | 10 +++++++ .../Tests/Transport/SendmailTransportTest.php | 24 ++++++++++++++++ .../Transport/Smtp/SmtpTransportTest.php | 28 +++++++++++++++++++ .../Component/Mailer/Tests/TransportTest.php | 5 ++++ .../Mailer/Transport/AbstractTransport.php | 2 ++ .../Mailer/Transport/FailoverTransport.php | 5 ++++ .../Mailer/Transport/NullTransport.php | 5 ++++ .../Mailer/Transport/RoundRobinTransport.php | 12 ++++++++ .../Mailer/Transport/SendmailTransport.php | 5 ++++ .../Mailer/Transport/Smtp/SmtpTransport.php | 9 ++++++ .../Mailer/Transport/TransportInterface.php | 2 ++ 25 files changed, 191 insertions(+) create mode 100644 src/Symfony/Component/Mailer/Tests/Transport/NullTransportTest.php create mode 100644 src/Symfony/Component/Mailer/Tests/Transport/SendmailTransportTest.php create mode 100644 src/Symfony/Component/Mailer/Tests/Transport/Smtp/SmtpTransportTest.php diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/MailerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/MailerTest.php index 4b74aa8274..9855a0ded4 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/MailerTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/MailerTest.php @@ -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; diff --git a/src/Symfony/Bundle/FrameworkBundle/composer.json b/src/Symfony/Bundle/FrameworkBundle/composer.json index 3c4f7f0b0b..e273981425 100644 --- a/src/Symfony/Bundle/FrameworkBundle/composer.json +++ b/src/Symfony/Bundle/FrameworkBundle/composer.json @@ -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", diff --git a/src/Symfony/Component/Mailer/Bridge/Amazon/Transport/SesApiTransport.php b/src/Symfony/Component/Mailer/Bridge/Amazon/Transport/SesApiTransport.php index e3710f0632..5305540b39 100644 --- a/src/Symfony/Component/Mailer/Bridge/Amazon/Transport/SesApiTransport.php +++ b/src/Symfony/Component/Mailer/Bridge/Amazon/Transport/SesApiTransport.php @@ -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'); diff --git a/src/Symfony/Component/Mailer/Bridge/Amazon/Transport/SesHttpTransport.php b/src/Symfony/Component/Mailer/Bridge/Amazon/Transport/SesHttpTransport.php index 43482567ca..e93511fdde 100644 --- a/src/Symfony/Component/Mailer/Bridge/Amazon/Transport/SesHttpTransport.php +++ b/src/Symfony/Component/Mailer/Bridge/Amazon/Transport/SesHttpTransport.php @@ -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'); diff --git a/src/Symfony/Component/Mailer/Bridge/Mailchimp/Transport/MandrillApiTransport.php b/src/Symfony/Component/Mailer/Bridge/Mailchimp/Transport/MandrillApiTransport.php index d4be46d5ab..9b1748904c 100644 --- a/src/Symfony/Component/Mailer/Bridge/Mailchimp/Transport/MandrillApiTransport.php +++ b/src/Symfony/Component/Mailer/Bridge/Mailchimp/Transport/MandrillApiTransport.php @@ -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, [ diff --git a/src/Symfony/Component/Mailer/Bridge/Mailchimp/Transport/MandrillHttpTransport.php b/src/Symfony/Component/Mailer/Bridge/Mailchimp/Transport/MandrillHttpTransport.php index 10ef9046e6..e850ba0cb8 100644 --- a/src/Symfony/Component/Mailer/Bridge/Mailchimp/Transport/MandrillHttpTransport.php +++ b/src/Symfony/Component/Mailer/Bridge/Mailchimp/Transport/MandrillHttpTransport.php @@ -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(); diff --git a/src/Symfony/Component/Mailer/Bridge/Mailgun/Transport/MailgunApiTransport.php b/src/Symfony/Component/Mailer/Bridge/Mailgun/Transport/MailgunApiTransport.php index 0a1872146b..d55e498bea 100644 --- a/src/Symfony/Component/Mailer/Bridge/Mailgun/Transport/MailgunApiTransport.php +++ b/src/Symfony/Component/Mailer/Bridge/Mailgun/Transport/MailgunApiTransport.php @@ -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)); diff --git a/src/Symfony/Component/Mailer/Bridge/Mailgun/Transport/MailgunHttpTransport.php b/src/Symfony/Component/Mailer/Bridge/Mailgun/Transport/MailgunHttpTransport.php index df98218407..bda648232d 100644 --- a/src/Symfony/Component/Mailer/Bridge/Mailgun/Transport/MailgunHttpTransport.php +++ b/src/Symfony/Component/Mailer/Bridge/Mailgun/Transport/MailgunHttpTransport.php @@ -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([ diff --git a/src/Symfony/Component/Mailer/Bridge/Postmark/Transport/PostmarkApiTransport.php b/src/Symfony/Component/Mailer/Bridge/Postmark/Transport/PostmarkApiTransport.php index 07a45fb0cc..b2abd43a84 100644 --- a/src/Symfony/Component/Mailer/Bridge/Postmark/Transport/PostmarkApiTransport.php +++ b/src/Symfony/Component/Mailer/Bridge/Postmark/Transport/PostmarkApiTransport.php @@ -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, [ diff --git a/src/Symfony/Component/Mailer/Bridge/Sendgrid/Transport/SendgridApiTransport.php b/src/Symfony/Component/Mailer/Bridge/Sendgrid/Transport/SendgridApiTransport.php index 94b657e398..0da12690fd 100644 --- a/src/Symfony/Component/Mailer/Bridge/Sendgrid/Transport/SendgridApiTransport.php +++ b/src/Symfony/Component/Mailer/Bridge/Sendgrid/Transport/SendgridApiTransport.php @@ -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, [ diff --git a/src/Symfony/Component/Mailer/CHANGELOG.md b/src/Symfony/Component/Mailer/CHANGELOG.md index 7e7e758dac..0e25eeb187 100644 --- a/src/Symfony/Component/Mailer/CHANGELOG.md +++ b/src/Symfony/Component/Mailer/CHANGELOG.md @@ -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`. diff --git a/src/Symfony/Component/Mailer/Test/TransportFactoryTestCase.php b/src/Symfony/Component/Mailer/Test/TransportFactoryTestCase.php index e9c3c5d0f1..0fee7d3b9a 100644 --- a/src/Symfony/Component/Mailer/Test/TransportFactoryTestCase.php +++ b/src/Symfony/Component/Mailer/Test/TransportFactoryTestCase.php @@ -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()); + } } /** diff --git a/src/Symfony/Component/Mailer/Tests/Transport/FailoverTransportTest.php b/src/Symfony/Component/Mailer/Tests/Transport/FailoverTransportTest.php index 9243263fdd..5677ecab2c 100644 --- a/src/Symfony/Component/Mailer/Tests/Transport/FailoverTransportTest.php +++ b/src/Symfony/Component/Mailer/Tests/Transport/FailoverTransportTest.php @@ -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); diff --git a/src/Symfony/Component/Mailer/Tests/Transport/NullTransportTest.php b/src/Symfony/Component/Mailer/Tests/Transport/NullTransportTest.php new file mode 100644 index 0000000000..7a25994bf8 --- /dev/null +++ b/src/Symfony/Component/Mailer/Tests/Transport/NullTransportTest.php @@ -0,0 +1,24 @@ + + * + * 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()); + } +} diff --git a/src/Symfony/Component/Mailer/Tests/Transport/RoundRobinTransportTest.php b/src/Symfony/Component/Mailer/Tests/Transport/RoundRobinTransportTest.php index 4b2316da5d..f130f6d3fb 100644 --- a/src/Symfony/Component/Mailer/Tests/Transport/RoundRobinTransportTest.php +++ b/src/Symfony/Component/Mailer/Tests/Transport/RoundRobinTransportTest.php @@ -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); diff --git a/src/Symfony/Component/Mailer/Tests/Transport/SendmailTransportTest.php b/src/Symfony/Component/Mailer/Tests/Transport/SendmailTransportTest.php new file mode 100644 index 0000000000..2f79c0ff52 --- /dev/null +++ b/src/Symfony/Component/Mailer/Tests/Transport/SendmailTransportTest.php @@ -0,0 +1,24 @@ + + * + * 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()); + } +} diff --git a/src/Symfony/Component/Mailer/Tests/Transport/Smtp/SmtpTransportTest.php b/src/Symfony/Component/Mailer/Tests/Transport/Smtp/SmtpTransportTest.php new file mode 100644 index 0000000000..27494e1506 --- /dev/null +++ b/src/Symfony/Component/Mailer/Tests/Transport/Smtp/SmtpTransportTest.php @@ -0,0 +1,28 @@ + + * + * 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()); + } +} diff --git a/src/Symfony/Component/Mailer/Tests/TransportTest.php b/src/Symfony/Component/Mailer/Tests/TransportTest.php index d5a053ed27..139b644875 100644 --- a/src/Symfony/Component/Mailer/Tests/TransportTest.php +++ b/src/Symfony/Component/Mailer/Tests/TransportTest.php @@ -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 diff --git a/src/Symfony/Component/Mailer/Transport/AbstractTransport.php b/src/Symfony/Component/Mailer/Transport/AbstractTransport.php index 735278320d..3dd59c195c 100644 --- a/src/Symfony/Component/Mailer/Transport/AbstractTransport.php +++ b/src/Symfony/Component/Mailer/Transport/AbstractTransport.php @@ -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). */ diff --git a/src/Symfony/Component/Mailer/Transport/FailoverTransport.php b/src/Symfony/Component/Mailer/Transport/FailoverTransport.php index 29ac421692..8722aa4be0 100644 --- a/src/Symfony/Component/Mailer/Transport/FailoverTransport.php +++ b/src/Symfony/Component/Mailer/Transport/FailoverTransport.php @@ -28,4 +28,9 @@ class FailoverTransport extends RoundRobinTransport return $this->currentTransport; } + + protected function getNameSymbol(): string + { + return '||'; + } } diff --git a/src/Symfony/Component/Mailer/Transport/NullTransport.php b/src/Symfony/Component/Mailer/Transport/NullTransport.php index b1daee3b9d..96df22cc6d 100644 --- a/src/Symfony/Component/Mailer/Transport/NullTransport.php +++ b/src/Symfony/Component/Mailer/Transport/NullTransport.php @@ -23,4 +23,9 @@ final class NullTransport extends AbstractTransport protected function doSend(SentMessage $message): void { } + + public function getName(): string + { + return 'smtp://null'; + } } diff --git a/src/Symfony/Component/Mailer/Transport/RoundRobinTransport.php b/src/Symfony/Component/Mailer/Transport/RoundRobinTransport.php index c5fefd2718..261c38f46f 100644 --- a/src/Symfony/Component/Mailer/Transport/RoundRobinTransport.php +++ b/src/Symfony/Component/Mailer/Transport/RoundRobinTransport.php @@ -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; diff --git a/src/Symfony/Component/Mailer/Transport/SendmailTransport.php b/src/Symfony/Component/Mailer/Transport/SendmailTransport.php index 4494c55610..db57221750 100644 --- a/src/Symfony/Component/Mailer/Transport/SendmailTransport.php +++ b/src/Symfony/Component/Mailer/Transport/SendmailTransport.php @@ -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__)); diff --git a/src/Symfony/Component/Mailer/Transport/Smtp/SmtpTransport.php b/src/Symfony/Component/Mailer/Transport/Smtp/SmtpTransport.php index 34e4cc9f3e..f50e670848 100644 --- a/src/Symfony/Component/Mailer/Transport/Smtp/SmtpTransport.php +++ b/src/Symfony/Component/Mailer/Transport/Smtp/SmtpTransport.php @@ -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. * diff --git a/src/Symfony/Component/Mailer/Transport/TransportInterface.php b/src/Symfony/Component/Mailer/Transport/TransportInterface.php index 29ab4d3dc5..cc30f65f47 100644 --- a/src/Symfony/Component/Mailer/Transport/TransportInterface.php +++ b/src/Symfony/Component/Mailer/Transport/TransportInterface.php @@ -30,4 +30,6 @@ interface TransportInterface * @throws TransportExceptionInterface */ public function send(RawMessage $message, SmtpEnvelope $envelope = null): ?SentMessage; + + public function getName(): string; }