[Mailer] Renamed getName() to toString()

This commit is contained in:
Fabien Potencier 2019-09-02 11:39:31 +02:00
parent bcb91eadf0
commit 5b7bba9ef3
28 changed files with 56 additions and 51 deletions

View File

@ -43,7 +43,7 @@ class MailerTest extends AbstractWebTestCase
$this->onDoSend = $onDoSend;
}
public function getName(): string
public function __toString(): string
{
return 'dummy://local';
}

View File

@ -43,7 +43,7 @@ class SesApiTransport extends AbstractApiTransport
parent::__construct($client, $dispatcher, $logger);
}
public function getName(): string
public function __toString(): string
{
return sprintf('api://%s@ses?region=%s', $this->accessKey, $this->region);
}

View File

@ -42,7 +42,7 @@ class SesHttpTransport extends AbstractHttpTransport
parent::__construct($client, $dispatcher, $logger);
}
public function getName(): string
public function __toString(): string
{
return sprintf('http://%s@ses?region=%s', $this->accessKey, $this->region);
}

View File

@ -36,7 +36,7 @@ class MandrillApiTransport extends AbstractApiTransport
parent::__construct($client, $dispatcher, $logger);
}
public function getName(): string
public function __toString(): string
{
return sprintf('api://mandrill');
}

View File

@ -34,7 +34,7 @@ class MandrillHttpTransport extends AbstractHttpTransport
parent::__construct($client, $dispatcher, $logger);
}
public function getName(): string
public function __toString(): string
{
return sprintf('http://mandrill');
}

View File

@ -41,7 +41,7 @@ class MailgunApiTransport extends AbstractApiTransport
parent::__construct($client, $dispatcher, $logger);
}
public function getName(): string
public function __toString(): string
{
return sprintf('api://%s@mailgun?region=%s', $this->domain, $this->region);
}

View File

@ -40,7 +40,7 @@ class MailgunHttpTransport extends AbstractHttpTransport
parent::__construct($client, $dispatcher, $logger);
}
public function getName(): string
public function __toString(): string
{
return sprintf('http://%s@mailgun?region=%s', $this->domain, $this->region);
}

View File

@ -36,7 +36,7 @@ class PostmarkApiTransport extends AbstractApiTransport
parent::__construct($client, $dispatcher, $logger);
}
public function getName(): string
public function __toString(): string
{
return sprintf('api://postmark');
}

View File

@ -76,4 +76,9 @@ class SendgridTransportFactoryTest extends TransportFactoryTestCase
'The "foo" scheme is not supported for mailer "sendgrid". Supported schemes are: "api", "smtp", "smtps".',
];
}
public function incompleteDsnProvider(): iterable
{
yield [new Dsn('api', 'sendgrid')];
}
}

View File

@ -37,7 +37,7 @@ class SendgridApiTransport extends AbstractApiTransport
parent::__construct($client, $dispatcher, $logger);
}
public function getName(): string
public function __toString(): string
{
return sprintf('api://sendgrid');
}

View File

@ -11,7 +11,7 @@ CHANGELOG
* Added PHPUnit constraints
* Added `MessageDataCollector`
* Added `MessageEvents` and `MessageLoggerListener` to allow collecting sent emails
* [BC BREAK] `TransportInterface` has a new `getName()` method
* [BC BREAK] `TransportInterface` has a new `__toString()` 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

@ -24,14 +24,14 @@ final class MessageEvent extends Event
{
private $message;
private $envelope;
private $transportName;
private $transport;
private $queued;
public function __construct(RawMessage $message, SmtpEnvelope $envelope, string $transportName, bool $queued = false)
public function __construct(RawMessage $message, SmtpEnvelope $envelope, string $transport, bool $queued = false)
{
$this->message = $message;
$this->envelope = $envelope;
$this->transportName = $transportName;
$this->transport = $transport;
$this->queued = $queued;
}
@ -55,9 +55,9 @@ final class MessageEvent extends Event
$this->envelope = $envelope;
}
public function getTransportName(): string
public function getTransport(): string
{
return $this->transportName;
return $this->transport;
}
public function isQueued(): bool

View File

@ -24,7 +24,7 @@ class MessageEvents
public function add(MessageEvent $event): void
{
$this->events[] = $event;
$this->transports[$event->getTransportName()] = true;
$this->transports[$event->getTransport()] = true;
}
public function getTransports(): array
@ -43,7 +43,7 @@ class MessageEvents
$events = [];
foreach ($this->events as $event) {
if ($name === $event->getTransportName()) {
if ($name === $event->getTransport()) {
$events[] = $event;
}
}

View File

@ -54,7 +54,7 @@ class Mailer implements MailerInterface
throw new TransportException('Cannot send message without a valid envelope.', 0, $e);
}
}
$event = new MessageEvent($message, $envelope, $this->transport->getName(), true);
$event = new MessageEvent($message, $envelope, (string) $this->transport, true);
$this->dispatcher->dispatch($event);
}

View File

@ -70,7 +70,7 @@ abstract class TransportFactoryTestCase extends TestCase
$this->assertEquals($transport, $factory->create($dsn));
if ('smtp' !== $dsn->getScheme() && 'smtps' !== $dsn->getScheme()) {
$this->assertStringMatchesFormat($dsn->getScheme().'://%S'.$dsn->getHost().'%S', $transport->getName());
$this->assertStringMatchesFormat($dsn->getScheme().'://%S'.$dsn->getHost().'%S', (string) $transport);
}
}

View File

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

View File

@ -16,9 +16,9 @@ use Symfony\Component\Mailer\Transport\NullTransport;
class NullTransportTest extends TestCase
{
public function testName()
public function testToString()
{
$t = new NullTransport();
$this->assertEquals('smtp://null', $t->getName());
$this->assertEquals('smtp://null', (string) $t);
}
}

View File

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

View File

@ -16,9 +16,9 @@ use Symfony\Component\Mailer\Transport\SendmailTransport;
class SendmailTransportTest extends TestCase
{
public function testName()
public function testToString()
{
$t = new SendmailTransport();
$this->assertEquals('smtp://sendmail', $t->getName());
$this->assertEquals('smtp://sendmail', (string) $t);
}
}

View File

@ -16,28 +16,28 @@ use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
class EsmtpTransportTest extends TestCase
{
public function testName()
public function testToString()
{
$t = new EsmtpTransport();
$this->assertEquals('smtp://localhost', $t->getName());
$this->assertEquals('smtp://localhost', (string) $t);
$t = new EsmtpTransport('example.com');
if (\defined('OPENSSL_VERSION_NUMBER')) {
$this->assertEquals('smtps://example.com', $t->getName());
$this->assertEquals('smtps://example.com', (string) $t);
} else {
$this->assertEquals('smtp://example.com', $t->getName());
$this->assertEquals('smtp://example.com', (string) $t);
}
$t = new EsmtpTransport('example.com', 2525);
$this->assertEquals('smtp://example.com:2525', $t->getName());
$this->assertEquals('smtp://example.com:2525', (string) $t);
$t = new EsmtpTransport('example.com', 0, true);
$this->assertEquals('smtps://example.com', $t->getName());
$this->assertEquals('smtps://example.com', (string) $t);
$t = new EsmtpTransport('example.com', 0, false);
$this->assertEquals('smtp://example.com', $t->getName());
$this->assertEquals('smtp://example.com', (string) $t);
$t = new EsmtpTransport('example.com', 466, true);
$this->assertEquals('smtps://example.com:466', $t->getName());
$this->assertEquals('smtps://example.com:466', (string) $t);
}
}

View File

@ -17,12 +17,12 @@ use Symfony\Component\Mailer\Transport\Smtp\Stream\SocketStream;
class SmtpTransportTest extends TestCase
{
public function testName()
public function testToString()
{
$t = new SmtpTransport();
$this->assertEquals('smtps://localhost', $t->getName());
$this->assertEquals('smtps://localhost', (string) $t);
$t = new SmtpTransport((new SocketStream())->setHost('127.0.0.1')->setPort(2525)->disableTls());
$this->assertEquals('smtp://127.0.0.1:2525', $t->getName());
$this->assertEquals('smtp://127.0.0.1:2525', (string) $t);
}
}

View File

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

View File

@ -67,7 +67,7 @@ abstract class AbstractTransport implements TransportInterface
}
}
$event = new MessageEvent($message, $envelope, $this->getName());
$event = new MessageEvent($message, $envelope, (string) $this);
$this->dispatcher->dispatch($event);
$envelope = $event->getEnvelope();
if (!$envelope->getRecipients()) {

View File

@ -24,7 +24,7 @@ final class NullTransport extends AbstractTransport
{
}
public function getName(): string
public function __toString(): string
{
return 'smtp://null';
}

View File

@ -56,10 +56,10 @@ class RoundRobinTransport implements TransportInterface
throw new TransportException('All transports failed.');
}
public function getName(): string
public function __toString(): string
{
return implode(' '.$this->getNameSymbol().' ', array_map(function (TransportInterface $transport) {
return $transport->getName();
return (string) $transport;
}, $this->transports));
}

View File

@ -73,10 +73,10 @@ class SendmailTransport extends AbstractTransport
return parent::send($message, $envelope);
}
public function getName(): string
public function __toString(): string
{
if ($this->transport) {
return $this->transport->getName();
return (string) $this->transport;
}
return 'smtp://sendmail';

View File

@ -126,7 +126,7 @@ class SmtpTransport extends AbstractTransport
return $message;
}
public function getName(): string
public function __toString(): string
{
if ($this->stream instanceof SocketStream) {
$name = sprintf('smtp%s://%s', ($tls = $this->stream->isTLS()) ? 's' : '', $this->stream->getHost());

View File

@ -31,5 +31,5 @@ interface TransportInterface
*/
public function send(RawMessage $message, SmtpEnvelope $envelope = null): ?SentMessage;
public function getName(): string;
public function __toString(): string;
}