@@ -25,20 +25,25 @@ public function __construct(string $connectionString)
2525 };
2626 }
2727
28- public function testPortInHost (): void
28+ public function provideValidConnectionStrings (): array
2929 {
30- $ config = $ this ->createConfigFromString ("host=localhost:5432 user=user database=test " );
31-
32- self ::assertSame ("localhost " , $ config ->getHost ());
33- self ::assertSame (5432 , $ config ->getPort ());
34- self ::assertSame ("user " , $ config ->getUser ());
35- self ::assertSame ("" , $ config ->getPassword ());
36- self ::assertSame ("test " , $ config ->getDatabase ());
30+ return [
31+ 'basic ' => ["host=localhost port=5432 user=user pass=test db=test " ],
32+ 'alternative ' => ["host=localhost;port=5432;user=user;password=test;db=test " ],
33+ 'port-in-host ' => ["host=localhost:5432 user=user pass=test db=test " ],
34+ 'whitespace ' => [" host=localhost port=5432 user=user pass=test db=test " ],
35+ 'whitespace-after-semicolon ' => ["host=localhost; port=5432; user=user; password=test; db=test; " ],
36+ 'quotes ' => ['host="localhost" port=5432 user="user" pass="test" db="test" ' ],
37+ 'alternative-with-whitespace ' => ["host=localhost; port=5432; user=user; password=test; db=test; " ],
38+ ];
3739 }
3840
39- public function testBasicSyntax (): void
41+ /**
42+ * @dataProvider provideValidConnectionStrings
43+ */
44+ public function testValidStrings (string $ connectionString ): void
4045 {
41- $ config = $ this ->createConfigFromString (" host=localhost port=5432 user=user pass=test db=test " );
46+ $ config = $ this ->createConfigFromString ($ connectionString );
4247
4348 self ::assertSame ("localhost " , $ config ->getHost ());
4449 self ::assertSame (5432 , $ config ->getPort ());
@@ -47,21 +52,37 @@ public function testBasicSyntax(): void
4752 self ::assertSame ("test " , $ config ->getDatabase ());
4853 }
4954
50- public function testAlternativeSyntax (): void
55+ public function testQuoteInQuotedValue (): void
5156 {
52- $ config = $ this ->createConfigFromString ("host=localhost;port=3306;user=user;password=test;db=test " );
57+ $ config = $ this ->createConfigFromString (
58+ <<<'CS'
59+ host="local\"host:3306" database='test'
60+ CS
61+ );
5362
54- self ::assertSame (" localhost " , $ config ->getHost ());
63+ self ::assertSame (' local"host ' , $ config ->getHost ());
5564 self ::assertSame (3306 , $ config ->getPort ());
56- self ::assertSame ("user " , $ config ->getUser ());
57- self ::assertSame ("test " , $ config ->getPassword ());
58- self ::assertSame ("test " , $ config ->getDatabase ());
65+ self ::assertSame ('test ' , $ config ->getDatabase ());
66+ }
67+
68+ public function provideInvalidConnectionStrings (): array
69+ {
70+ return [
71+ 'missing-value ' => ['host= ' ],
72+ 'empty-value ' => ['host="" ' ],
73+ 'leading-characters ' => ['test host=localhost ' ],
74+ 'trailing-characters ' => ['host=localhost test ' ],
75+ 'invalid-whitespace ' => ['host= localhost ' ],
76+ 'duplicate-key ' => ['host=localhost port=5432 port=5433 ' ]
77+ ];
5978 }
6079
61- public function testInvalidString (): void
80+ /**
81+ * @dataProvider provideInvalidConnectionStrings
82+ */
83+ public function testInvalidStrings (string $ connectionString ): void
6284 {
6385 $ this ->expectException (\ValueError::class);
64- $ this ->expectExceptionMessage ("Empty key name in connection string " );
65- $ this ->createConfigFromString ("invalid =connection string " );
86+ $ this ->createConfigFromString ($ connectionString );
6687 }
6788}
0 commit comments