Skip to content

Commit af24fa2

Browse files
committed
[Mailer] Add a more precise exception
1 parent bc942d0 commit af24fa2

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

Tests/Transport/TransportsTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Mailer\Tests\Transport;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Mailer\Exception\InvalidArgumentException;
1516
use Symfony\Component\Mailer\Transport\TransportInterface;
1617
use Symfony\Component\Mailer\Transport\Transports;
1718
use Symfony\Component\Mime\Header\Headers;
@@ -48,4 +49,19 @@ public function testOverrideTransport()
4849
$email = new Message($headers, new TextPart('...'));
4950
$transport->send($email);
5051
}
52+
53+
public function testTransportDoesNotExist()
54+
{
55+
$transport = new Transports([
56+
'foo' => $this->createMock(TransportInterface::class),
57+
'bar' => $this->createMock(TransportInterface::class),
58+
]);
59+
60+
$headers = (new Headers())->addTextHeader('X-Transport', 'foobar');
61+
$email = new Message($headers, new TextPart('...'));
62+
63+
$this->expectException(InvalidArgumentException::class);
64+
$this->expectExceptionMessage('The "foobar" transport does not exist (available transports: "foo", "bar").');
65+
$transport->send($email);
66+
}
5167
}

Transport/Transports.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function send(RawMessage $message, SmtpEnvelope $envelope = null): ?SentM
5656
$headers->remove('X-Transport');
5757

5858
if (!isset($this->transports[$transport])) {
59-
throw new InvalidArgumentException(sprintf('The "%s" transport does not exist.', $transport));
59+
throw new InvalidArgumentException(sprintf('The "%s" transport does not exist (available transports: "%s").', $transport, implode('", "', array_keys($this->transports))));
6060
}
6161

6262
return $this->transports[$transport]->send($message, $envelope);

0 commit comments

Comments
 (0)