Skip to content

Commit 7acb44a

Browse files
committed
[Messenger] show scheme in exception message when no transport found matching DSN
Improves the exception message thrown when no transport supports a given messenger DSN by showing hinting the scheme that was used - if it can be parsed.
1 parent 6c44684 commit 7acb44a

File tree

2 files changed

+105
-1
lines changed

2 files changed

+105
-1
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Messenger\Tests\Transport;
13+
14+
use Generator;
15+
use PHPUnit\Framework\TestCase;
16+
use Symfony\Component\Messenger\Exception\InvalidArgumentException;
17+
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
18+
use Symfony\Component\Messenger\Transport\TransportFactory;
19+
use Symfony\Component\Messenger\Transport\TransportFactoryInterface;
20+
use Symfony\Component\Messenger\Transport\TransportInterface;
21+
22+
class TransportFactoryTest extends TestCase
23+
{
24+
/**
25+
* @dataProvider provideThrowsExceptionOnUnsupportedTransport
26+
*/
27+
public function testThrowsExceptionOnUnsupportedTransport(array $transportSupport, string $dsn, ?string $expectedMessage)
28+
{
29+
if ($expectedMessage !== null) {
30+
$this->expectException(InvalidArgumentException::class);
31+
$this->expectExceptionMessage($expectedMessage);
32+
}
33+
$serializer = $this->createMock(SerializerInterface::class);
34+
$factories = [];
35+
foreach ($transportSupport as $supported) {
36+
$factory = $this->createMock(TransportFactoryInterface::class);
37+
$factory->method('supports', $dsn, [])->willReturn($supported);
38+
$factories[] = $factory;
39+
}
40+
41+
$factory = new TransportFactory($factories);
42+
$transport = $factory->createTransport($dsn, [], $serializer);
43+
44+
if ($expectedMessage !== null) {
45+
return;
46+
}
47+
48+
self::assertInstanceOf(TransportInterface::class, $transport);
49+
}
50+
51+
public function provideThrowsExceptionOnUnsupportedTransport(): Generator
52+
{
53+
yield 'transport supports dsn' => [
54+
[true],
55+
'foobar://barfoo',
56+
null,
57+
];
58+
yield 'show scheme when no transport supports' => [
59+
[false],
60+
'foobar://barfoo',
61+
'No transport supports the given Messenger DSN (with scheme "foobar")',
62+
];
63+
yield 'empty dsn' => [
64+
[false],
65+
'',
66+
'No transport supports the given Messenger DSN.',
67+
];
68+
yield 'dsn with no scheme' => [
69+
[false],
70+
'barfoo@bar',
71+
'No transport supports the given Messenger DSN.',
72+
];
73+
yield 'dsn with empty scheme ' => [
74+
[false],
75+
'://barfoo@bar',
76+
'No transport supports the given Messenger DSN.',
77+
];
78+
yield 'https dsn' => [
79+
[false],
80+
'https://sqs.foobar.amazonaws.com',
81+
'No transport supports the given Messenger DSN (with scheme "https")',
82+
];
83+
yield 'with package suggestion amqp://' => [
84+
[false],
85+
'amqp://barfoo@bar',
86+
'No transport supports the given Messenger DSN (with scheme "amqp"). Run "composer require symfony/amqp-messenger" to install AMQP transport.',
87+
];
88+
}
89+
}
90+

src/Symfony/Component/Messenger/Transport/TransportFactory.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ public function createTransport(#[\SensitiveParameter] string $dsn, array $optio
5353
$packageSuggestion = ' Run "composer require symfony/beanstalkd-messenger" to install Beanstalkd transport.';
5454
}
5555

56-
throw new InvalidArgumentException('No transport supports the given Messenger DSN.'.$packageSuggestion);
56+
$scheme = $this->parseScheme($dsn);
57+
if ($scheme) {
58+
throw new InvalidArgumentException(sprintf('No transport supports the given Messenger DSN (with scheme "%s").%s', $scheme, $packageSuggestion));
59+
}
60+
61+
throw new InvalidArgumentException(sprintf('No transport supports the given Messenger DSN.%s',$packageSuggestion));
5762
}
5863

5964
public function supports(#[\SensitiveParameter] string $dsn, array $options): bool
@@ -66,4 +71,13 @@ public function supports(#[\SensitiveParameter] string $dsn, array $options): bo
6671

6772
return false;
6873
}
74+
75+
private function parseScheme(string $dsn): ?string
76+
{
77+
if (!preg_match('{^([^:]+)://}', $dsn, $matches)) {
78+
return null;
79+
}
80+
81+
return $matches[1];
82+
}
6983
}

0 commit comments

Comments
 (0)