|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 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\Notifier\Tests\Transport; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\Notifier\Exception\InvalidArgumentException; |
| 16 | +use Symfony\Component\Notifier\Transport\Dsn; |
| 17 | + |
| 18 | +class DsnTest extends TestCase |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @dataProvider fromStringProvider |
| 22 | + */ |
| 23 | + public function testFromString(string $string, Dsn $expectedDsn): void |
| 24 | + { |
| 25 | + $actualDsn = Dsn::fromString($string); |
| 26 | + |
| 27 | + $this->assertSame($expectedDsn->getScheme(), $actualDsn->getScheme()); |
| 28 | + $this->assertSame($expectedDsn->getHost(), $actualDsn->getHost()); |
| 29 | + $this->assertSame($expectedDsn->getPort(), $actualDsn->getPort()); |
| 30 | + $this->assertSame($expectedDsn->getUser(), $actualDsn->getUser()); |
| 31 | + $this->assertSame($expectedDsn->getPassword(), $actualDsn->getPassword()); |
| 32 | + $this->assertSame($expectedDsn->getPath(), $actualDsn->getPath()); |
| 33 | + $this->assertSame($expectedDsn->getOption('from'), $actualDsn->getOption('from')); |
| 34 | + |
| 35 | + $this->assertSame($string, $actualDsn->getOriginalDsn()); |
| 36 | + } |
| 37 | + |
| 38 | + public function fromStringProvider(): iterable |
| 39 | + { |
| 40 | + yield 'simple dsn' => [ |
| 41 | + 'scheme://localhost', |
| 42 | + new Dsn('scheme', 'localhost', null, null, null, [], null), |
| 43 | + ]; |
| 44 | + |
| 45 | + yield 'dsn with user and pass' => [ |
| 46 | + 'scheme://u$er:pa$s@localhost', |
| 47 | + new Dsn('scheme', 'localhost', 'u$er', 'pa$s', null, [], null), |
| 48 | + ]; |
| 49 | + |
| 50 | + yield 'dsn with user and pass and custom port' => [ |
| 51 | + 'scheme://u$er:pa$s@localhost:8000', |
| 52 | + new Dsn('scheme', 'localhost', 'u$er', 'pa$s', '8000', [], null), |
| 53 | + ]; |
| 54 | + |
| 55 | + yield 'dsn with user and pass, custom port and custom path' => [ |
| 56 | + 'scheme://u$er:pa$s@localhost:8000/channel', |
| 57 | + new Dsn('scheme', 'localhost', 'u$er', 'pa$s', '8000', [], '/channel'), |
| 58 | + ]; |
| 59 | + |
| 60 | + yield 'dsn with user and pass, custom port, custom path and custom options' => [ |
| 61 | + 'scheme://u$er:pa$s@localhost:8000/channel?from=FROM', |
| 62 | + new Dsn('scheme', 'localhost', 'u$er', 'pa$s', '8000', ['from' => 'FROM'], '/channel'), |
| 63 | + ]; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @dataProvider invalidDsnProvider |
| 68 | + */ |
| 69 | + public function testInvalidDsn(string $dsn, string $exceptionMessage): void |
| 70 | + { |
| 71 | + $this->expectException(InvalidArgumentException::class); |
| 72 | + $this->expectExceptionMessage($exceptionMessage); |
| 73 | + Dsn::fromString($dsn); |
| 74 | + } |
| 75 | + |
| 76 | + public function invalidDsnProvider(): iterable |
| 77 | + { |
| 78 | + yield [ |
| 79 | + 'some://', |
| 80 | + 'The "some://" notifier DSN is invalid.', |
| 81 | + ]; |
| 82 | + |
| 83 | + yield [ |
| 84 | + '//slack', |
| 85 | + 'The "//slack" notifier DSN must contain a scheme.', |
| 86 | + ]; |
| 87 | + |
| 88 | + yield [ |
| 89 | + 'file:///some/path', |
| 90 | + 'The "file:///some/path" notifier DSN must contain a host (use "default" by default).', |
| 91 | + ]; |
| 92 | + } |
| 93 | + |
| 94 | + public function testGetOption(): void |
| 95 | + { |
| 96 | + $options = ['with_value' => 'some value', 'nullable' => null]; |
| 97 | + $dsn = new Dsn('scheme', 'localhost', 'u$er', 'pa$s', '8000', $options, '/channel'); |
| 98 | + |
| 99 | + $this->assertSame('some value', $dsn->getOption('with_value')); |
| 100 | + $this->assertSame('default', $dsn->getOption('nullable', 'default')); |
| 101 | + $this->assertSame('default', $dsn->getOption('not_existent_property', 'default')); |
| 102 | + } |
| 103 | +} |
0 commit comments