[Mailer] Add a more precise exception

This commit is contained in:
Fabien Potencier 2019-09-02 17:31:45 +02:00
parent cf57ca81ee
commit 561f9b7345
2 changed files with 17 additions and 1 deletions

View File

@ -12,6 +12,7 @@
namespace Symfony\Component\Mailer\Tests\Transport;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Mailer\Exception\InvalidArgumentException;
use Symfony\Component\Mailer\Transport\TransportInterface;
use Symfony\Component\Mailer\Transport\Transports;
use Symfony\Component\Mime\Header\Headers;
@ -48,4 +49,19 @@ class TransportsTest extends TestCase
$email = new Message($headers, new TextPart('...'));
$transport->send($email);
}
public function testTransportDoesNotExist()
{
$transport = new Transports([
'foo' => $this->createMock(TransportInterface::class),
'bar' => $this->createMock(TransportInterface::class),
]);
$headers = (new Headers())->addTextHeader('X-Transport', 'foobar');
$email = new Message($headers, new TextPart('...'));
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The "foobar" transport does not exist (available transports: "foo", "bar").');
$transport->send($email);
}
}

View File

@ -56,7 +56,7 @@ class Transports implements TransportInterface
$headers->remove('X-Transport');
if (!isset($this->transports[$transport])) {
throw new InvalidArgumentException(sprintf('The "%s" transport does not exist.', $transport));
throw new InvalidArgumentException(sprintf('The "%s" transport does not exist (available transports: "%s").', $transport, implode('", "', array_keys($this->transports))));
}
return $this->transports[$transport]->send($message, $envelope);