33namespace Enqueue \Dsn \Tests ;
44
55use Enqueue \Dsn \Dsn ;
6+ use Enqueue \Dsn \InvalidQueryParameterTypeException ;
67use PHPUnit \Framework \TestCase ;
78
89class DsnTest extends TestCase
@@ -73,6 +74,13 @@ public function testShouldParsePath()
7374 $ this ->assertSame ('/thePath ' , $ dsn ->getPath ());
7475 }
7576
77+ public function testShouldUrlDecodedPath ()
78+ {
79+ $ dsn = new Dsn ('amqp+ext://theUser:thePass@theHost:1267/%2f ' );
80+
81+ $ this ->assertSame ('// ' , $ dsn ->getPath ());
82+ }
83+
7684 public function testShouldParseQuery ()
7785 {
7886 $ dsn = new Dsn ('amqp+ext://theUser:thePass@theHost:1267/thePath?foo=fooVal&bar=bar%2fVal ' );
@@ -81,6 +89,95 @@ public function testShouldParseQuery()
8189 $ this ->assertSame (['foo ' => 'fooVal ' , 'bar ' => 'bar/Val ' ], $ dsn ->getQuery ());
8290 }
8391
92+ public function testShouldParseQueryShouldPreservePlusSymbol ()
93+ {
94+ $ dsn = new Dsn ('amqp+ext://theUser:thePass@theHost:1267/thePath?foo=fooVal&bar=bar+Val ' );
95+
96+ $ this ->assertSame ('foo=fooVal&bar=bar+Val ' , $ dsn ->getQueryString ());
97+ $ this ->assertSame (['foo ' => 'fooVal ' , 'bar ' => 'bar+Val ' ], $ dsn ->getQuery ());
98+ }
99+
100+ /**
101+ * @dataProvider provideIntQueryParameters
102+ */
103+ public function testShouldParseQueryParameterAsInt (string $ parameter , int $ expected )
104+ {
105+ $ dsn = new Dsn ('foo:?aName= ' .$ parameter );
106+
107+ $ this ->assertSame ($ expected , $ dsn ->getInt ('aName ' ));
108+ }
109+
110+ public function testShouldReturnDefaultIntIfNotSet ()
111+ {
112+ $ dsn = new Dsn ('foo: ' );
113+
114+ $ this ->assertNull ($ dsn ->getInt ('aName ' ));
115+ $ this ->assertSame (123 , $ dsn ->getInt ('aName ' , 123 ));
116+ }
117+
118+ public function testThrowIfQueryParameterNotInt ()
119+ {
120+ $ dsn = new Dsn ('foo:?aName=notInt ' );
121+
122+ $ this ->expectException (InvalidQueryParameterTypeException::class);
123+ $ this ->expectExceptionMessage ('The query parameter "aName" has invalid type. It must be "integer" ' );
124+ $ dsn ->getInt ('aName ' );
125+ }
126+
127+ /**
128+ * @dataProvider provideFloatQueryParameters
129+ */
130+ public function testShouldParseQueryParameterAsFloat (string $ parameter , float $ expected )
131+ {
132+ $ dsn = new Dsn ('foo:?aName= ' .$ parameter );
133+
134+ $ this ->assertSame ($ expected , $ dsn ->getFloat ('aName ' ));
135+ }
136+
137+ public function testShouldReturnDefaultFloatIfNotSet ()
138+ {
139+ $ dsn = new Dsn ('foo: ' );
140+
141+ $ this ->assertNull ($ dsn ->getFloat ('aName ' ));
142+ $ this ->assertSame (123. , $ dsn ->getFloat ('aName ' , 123. ));
143+ }
144+
145+ public function testThrowIfQueryParameterNotFloat ()
146+ {
147+ $ dsn = new Dsn ('foo:?aName=notFloat ' );
148+
149+ $ this ->expectException (InvalidQueryParameterTypeException::class);
150+ $ this ->expectExceptionMessage ('The query parameter "aName" has invalid type. It must be "float" ' );
151+ $ dsn ->getFloat ('aName ' );
152+ }
153+
154+ /**
155+ * @dataProvider provideBooleanQueryParameters
156+ */
157+ public function testShouldParseQueryParameterAsBoolean (string $ parameter , bool $ expected )
158+ {
159+ $ dsn = new Dsn ('foo:?aName= ' .$ parameter );
160+
161+ $ this ->assertSame ($ expected , $ dsn ->getBool ('aName ' ));
162+ }
163+
164+ public function testShouldReturnDefaultBoolIfNotSet ()
165+ {
166+ $ dsn = new Dsn ('foo: ' );
167+
168+ $ this ->assertNull ($ dsn ->getBool ('aName ' ));
169+ $ this ->assertTrue ($ dsn ->getBool ('aName ' , true ));
170+ }
171+
172+ public function testThrowIfQueryParameterNotBool ()
173+ {
174+ $ dsn = new Dsn ('foo:?aName=notBool ' );
175+
176+ $ this ->expectException (InvalidQueryParameterTypeException::class);
177+ $ this ->expectExceptionMessage ('The query parameter "aName" has invalid type. It must be "bool" ' );
178+ $ dsn ->getBool ('aName ' );
179+ }
180+
84181 public static function provideSchemes ()
85182 {
86183 yield [': ' , '' , '' , []];
@@ -99,4 +196,37 @@ public static function provideSchemes()
99196
100197 yield ['amqp+ext+rabbitmq: ' , 'amqp+ext+rabbitmq ' , 'amqp ' , ['ext ' , 'rabbitmq ' ]];
101198 }
199+
200+ public static function provideIntQueryParameters ()
201+ {
202+ yield ['123 ' , 123 ];
203+
204+ yield ['+123 ' , 123 ];
205+
206+ yield ['-123 ' , -123 ];
207+ }
208+
209+ public static function provideFloatQueryParameters ()
210+ {
211+ yield ['123 ' , 123. ];
212+
213+ yield ['+123 ' , 123. ];
214+
215+ yield ['-123 ' , -123. ];
216+
217+ yield ['0 ' , 0. ];
218+ }
219+
220+ public static function provideBooleanQueryParameters ()
221+ {
222+ yield ['' , false ];
223+
224+ yield ['1 ' , true ];
225+
226+ yield ['0 ' , false ];
227+
228+ yield ['true ' , true ];
229+
230+ yield ['false ' , false ];
231+ }
102232}
0 commit comments