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 4b74aa8274cdd..9855a0ded48e3 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/MailerTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/MailerTest.php @@ -42,6 +42,11 @@ public function __construct(EventDispatcherInterface $eventDispatcher, LoggerInt $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 3c4f7f0b0b0af..e273981425fdb 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 e3710f0632cb3..5305540b39860 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 @@ public function __construct(string $accessKey, string $secretKey, string $region 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 43482567ca8e8..e93511fdde8a8 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 @@ public function __construct(string $accessKey, string $secretKey, string $region 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 d4be46d5ab8bf..9b1748904cd68 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 @@ public function __construct(string $key, HttpClientInterface $client = null, Eve 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 10ef9046e6a74..e850ba0cb8230 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 @@ public function __construct(string $key, HttpClientInterface $client = null, Eve 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 0a1872146bf64..d55e498beac0e 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 @@ public function __construct(string $key, string $domain, string $region = null, 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 df98218407ed9..bda648232dd82 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 @@ public function __construct(string $key, string $domain, string $region = null, 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 07a45fb0ccbc8..b2abd43a84da6 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 @@ public function __construct(string $key, HttpClientInterface $client = null, Eve 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 94b657e398ded..0da12690fde3c 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 @@ public function __construct(string $key, HttpClientInterface $client = null, Eve 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 7e7e758dac910..0e25eeb1879a2 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 e9c3c5d0f15f8..0fee7d3b9a744 100644 --- a/src/Symfony/Component/Mailer/Test/TransportFactoryTestCase.php +++ b/src/Symfony/Component/Mailer/Test/TransportFactoryTestCase.php @@ -69,6 +69,9 @@ public function testCreate(Dsn $dsn, TransportInterface $transport): void $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 9243263fdd0ed..5677ecab2c69e 100644 --- a/src/Symfony/Component/Mailer/Tests/Transport/FailoverTransportTest.php +++ b/src/Symfony/Component/Mailer/Tests/Transport/FailoverTransportTest.php @@ -29,6 +29,16 @@ public function testSendNoTransports() 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 0000000000000..7a25994bf8c3a --- /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 4b2316da5d00c..f130f6d3fba37 100644 --- a/src/Symfony/Component/Mailer/Tests/Transport/RoundRobinTransportTest.php +++ b/src/Symfony/Component/Mailer/Tests/Transport/RoundRobinTransportTest.php @@ -28,6 +28,16 @@ public function testSendNoTransports() 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 0000000000000..2f79c0ff52058 --- /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 0000000000000..27494e150648a --- /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 d5a053ed27823..139b644875968 100644 --- a/src/Symfony/Component/Mailer/Tests/TransportTest.php +++ b/src/Symfony/Component/Mailer/Tests/TransportTest.php @@ -68,6 +68,11 @@ public function send(RawMessage $message, SmtpEnvelope $envelope = null): ?SentM { 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 735278320dc55..3dd59c195c302 100644 --- a/src/Symfony/Component/Mailer/Transport/AbstractTransport.php +++ b/src/Symfony/Component/Mailer/Transport/AbstractTransport.php @@ -39,6 +39,8 @@ public function __construct(EventDispatcherInterface $dispatcher = null, LoggerI $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 29ac421692553..8722aa4be0a26 100644 --- a/src/Symfony/Component/Mailer/Transport/FailoverTransport.php +++ b/src/Symfony/Component/Mailer/Transport/FailoverTransport.php @@ -28,4 +28,9 @@ protected function getNextTransport(): ?TransportInterface 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 b1daee3b9d6c5..96df22cc6d38f 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 c5fefd2718114..261c38f46f174 100644 --- a/src/Symfony/Component/Mailer/Transport/RoundRobinTransport.php +++ b/src/Symfony/Component/Mailer/Transport/RoundRobinTransport.php @@ -56,6 +56,13 @@ public function send(RawMessage $message, SmtpEnvelope $envelope = null): ?SentM 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 @@ protected function isTransportDead(TransportInterface $transport): bool 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 4494c55610e8f..db5722175091b 100644 --- a/src/Symfony/Component/Mailer/Transport/SendmailTransport.php +++ b/src/Symfony/Component/Mailer/Transport/SendmailTransport.php @@ -73,6 +73,11 @@ public function send(RawMessage $message, SmtpEnvelope $envelope = null): ?SentM 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 34e4cc9f3e637..f50e670848a1b 100644 --- a/src/Symfony/Component/Mailer/Transport/Smtp/SmtpTransport.php +++ b/src/Symfony/Component/Mailer/Transport/Smtp/SmtpTransport.php @@ -126,6 +126,15 @@ public function send(RawMessage $message, SmtpEnvelope $envelope = null): ?SentM 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 29ab4d3dc5d6a..cc30f65f4787c 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; } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy