|
| 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 | + |
0 commit comments