|
2 | 2 |
|
3 | 3 | namespace Enqueue\Dsn\Tests; |
4 | 4 |
|
| 5 | +use Enqueue\Dsn\Dsn; |
5 | 6 | use PHPUnit\Framework\TestCase; |
6 | 7 |
|
7 | 8 | class DsnTest extends TestCase |
8 | 9 | { |
| 10 | + public function testCouldBeConstructedWithDsnAsFirstArgument() |
| 11 | + { |
| 12 | + new Dsn('foo://localhost:1234'); |
| 13 | + } |
| 14 | + |
| 15 | + public function testThrowsIfSchemePartIsMissing() |
| 16 | + { |
| 17 | + $this->expectException(\LogicException::class); |
| 18 | + $this->expectExceptionMessage('The DSN is invalid. It does not have scheme separator ":".'); |
| 19 | + new Dsn('foobar'); |
| 20 | + } |
| 21 | + |
| 22 | + public function testThrowsIfSchemeContainsIllegalSymbols() |
| 23 | + { |
| 24 | + $this->expectException(\LogicException::class); |
| 25 | + $this->expectExceptionMessage('The DSN is invalid. Scheme contains illegal symbols.'); |
| 26 | + new Dsn('foo_&%&^bar://localhost'); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @dataProvider provideSchemes |
| 31 | + */ |
| 32 | + public function testShouldParseSchemeCorrectly(string $dsn, string $expectedScheme, string $expectedSchemeProtocol, array $expectedSchemeExtensions) |
| 33 | + { |
| 34 | + $dsn = new Dsn($dsn); |
| 35 | + |
| 36 | + $this->assertSame($expectedScheme, $dsn->getScheme()); |
| 37 | + $this->assertSame($expectedSchemeProtocol, $dsn->getSchemeProtocol()); |
| 38 | + $this->assertSame($expectedSchemeExtensions, $dsn->getSchemeExtensions()); |
| 39 | + } |
| 40 | + |
| 41 | + public function testShouldParseUser() |
| 42 | + { |
| 43 | + $dsn = new Dsn('amqp+ext://theUser:thePass@theHost:1267/thePath'); |
| 44 | + |
| 45 | + $this->assertSame('theUser', $dsn->getUser()); |
| 46 | + } |
| 47 | + |
| 48 | + public function testShouldParsePassword() |
| 49 | + { |
| 50 | + $dsn = new Dsn('amqp+ext://theUser:thePass@theHost:1267/thePath'); |
| 51 | + |
| 52 | + $this->assertSame('thePass', $dsn->getPassword()); |
| 53 | + } |
| 54 | + |
| 55 | + public function testShouldParseHost() |
| 56 | + { |
| 57 | + $dsn = new Dsn('amqp+ext://theUser:thePass@theHost:1267/thePath'); |
| 58 | + |
| 59 | + $this->assertSame('theHost', $dsn->getHost()); |
| 60 | + } |
| 61 | + |
| 62 | + public function testShouldParsePort() |
| 63 | + { |
| 64 | + $dsn = new Dsn('amqp+ext://theUser:thePass@theHost:1267/thePath'); |
| 65 | + |
| 66 | + $this->assertSame(1267, $dsn->getPort()); |
| 67 | + } |
| 68 | + |
| 69 | + public function testShouldParsePath() |
| 70 | + { |
| 71 | + $dsn = new Dsn('amqp+ext://theUser:thePass@theHost:1267/thePath'); |
| 72 | + |
| 73 | + $this->assertSame('/thePath', $dsn->getPath()); |
| 74 | + } |
| 75 | + |
| 76 | + public function testShouldParseQuery() |
| 77 | + { |
| 78 | + $dsn = new Dsn('amqp+ext://theUser:thePass@theHost:1267/thePath?foo=fooVal&bar=bar%2fVal'); |
| 79 | + |
| 80 | + $this->assertSame('foo=fooVal&bar=bar%2fVal', $dsn->getQueryString()); |
| 81 | + $this->assertSame(['foo' => 'fooVal', 'bar' => 'bar/Val'], $dsn->getQuery()); |
| 82 | + } |
| 83 | + |
| 84 | + public static function provideSchemes() |
| 85 | + { |
| 86 | + yield [':', '', '', []]; |
| 87 | + |
| 88 | + yield ['FOO:', 'foo', 'foo', []]; |
| 89 | + |
| 90 | + yield ['foo:', 'foo', 'foo', []]; |
| 91 | + |
| 92 | + yield ['foo+bar:', 'foo+bar', 'foo', ['bar']]; |
| 93 | + |
| 94 | + yield ['foo+bar+baz:', 'foo+bar+baz', 'foo', ['bar', 'baz']]; |
| 95 | + |
| 96 | + yield ['foo:?bar=barVal', 'foo', 'foo', []]; |
| 97 | + |
| 98 | + yield ['amqp+ext://guest:guest@localhost:5672/%2f', 'amqp+ext', 'amqp', ['ext']]; |
| 99 | + |
| 100 | + yield ['amqp+ext+rabbitmq:', 'amqp+ext+rabbitmq', 'amqp', ['ext', 'rabbitmq']]; |
| 101 | + } |
9 | 102 | } |
0 commit comments