bug #30995 [Mailer] allow user/pass on dsn while using failover/roundrobin (scuben)

This PR was merged into the 4.3-dev branch.

Discussion
----------

[Mailer] allow user/pass on dsn while using failover/roundrobin

| Q             | A
| ------------- | ---
| Branch?       | master <!-- see below -->
| Bug fix?      | yes
| New feature?  | no <!-- don't forget to update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- don't forget to 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

this PR provides two things:
1. It is possible now to user `username` and `password` in a failover or round robin transport when using smtp
2. Fixed a type problem with `username` and `password` for the smtp transport as `getUsername()` cannot return `null` because of its signature but if no `username` is provided then the property would have been `null`. Fixed with setting an empty string as default. Same for `password`. (This was discovered by adding a test - yeah!)

Commits
-------

4518ac56a1 allow user/pass on dns while using failover/roundrobin and type fix for username/password
This commit is contained in:
Fabien Potencier 2019-04-08 07:54:46 +02:00
commit 35684187d5
3 changed files with 22 additions and 4 deletions

View File

@ -236,9 +236,11 @@ class TransportTest extends TestCase
public function testFromDsnFailover()
{
$user = 'user';
$pass = 'pass';
$dispatcher = $this->createMock(EventDispatcherInterface::class);
$logger = $this->createMock(LoggerInterface::class);
$transport = Transport::fromDsn('smtp://null || smtp://null || smtp://null', $dispatcher, null, $logger);
$transport = Transport::fromDsn('smtp://example.com || smtp://'.urlencode($user).'@example.com || smtp://'.urlencode($user).':'.urlencode($pass).'@example.com', $dispatcher, null, $logger);
$this->assertInstanceOf(Transport\FailoverTransport::class, $transport);
$p = new \ReflectionProperty(Transport\RoundRobinTransport::class, 'transports');
$p->setAccessible(true);
@ -247,6 +249,12 @@ class TransportTest extends TestCase
foreach ($transports as $transport) {
$this->assertProperties($transport, $dispatcher, $logger);
}
$this->assertSame('', $transports[0]->getUsername());
$this->assertSame('', $transports[0]->getPassword());
$this->assertSame($user, $transports[1]->getUsername());
$this->assertSame('', $transports[1]->getPassword());
$this->assertSame($user, $transports[2]->getUsername());
$this->assertSame($pass, $transports[2]->getPassword());
}
public function testFromDsnRoundRobin()

View File

@ -171,7 +171,17 @@ class Transport
throw new LogicException(sprintf('The "%s" scheme is not supported for mailer "%s".', $parsedDsn['scheme'], $parsedDsn['host']));
default:
if ('smtp' === $parsedDsn['scheme']) {
return new Transport\Smtp\EsmtpTransport($parsedDsn['host'], $parsedDsn['port'] ?? 25, $query['encryption'] ?? null, $query['auth_mode'] ?? null, $dispatcher, $logger);
$transport = new Transport\Smtp\EsmtpTransport($parsedDsn['host'], $parsedDsn['port'] ?? 25, $query['encryption'] ?? null, $query['auth_mode'] ?? null, $dispatcher, $logger);
if ($user) {
$transport->setUsername($user);
}
if ($pass) {
$transport->setPassword($pass);
}
return $transport;
}
throw new LogicException(sprintf('The "%s" mailer is not supported.', $parsedDsn['host']));

View File

@ -29,8 +29,8 @@ use Symfony\Component\Mailer\Transport\Smtp\Stream\SocketStream;
class EsmtpTransport extends SmtpTransport
{
private $authenticators = [];
private $username;
private $password;
private $username = '';
private $password = '';
private $authMode;
public function __construct(string $host = 'localhost', int $port = 25, string $encryption = null, string $authMode = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)