19
19
final class DsnTest extends TestCase
20
20
{
21
21
/**
22
- * @dataProvider fromStringProvider
22
+ * @dataProvider constructProvider
23
23
*/
24
- public function testFromString (string $ string, Dsn $ expectedDsn )
24
+ public function testConstruct (string $ dsnString , string $ scheme , string $ host , ? string $ user = null , ? string $ password = null , ? int $ port = null , array $ options = [], ? string $ path = null )
25
25
{
26
- $ actualDsn = Dsn::fromString ($ string );
26
+ $ dsn = new Dsn ($ dsnString );
27
+ $ this ->assertSame ($ dsnString , $ dsn ->getOriginalDsn ());
27
28
28
- $ this ->assertSame ($ expectedDsn ->getScheme (), $ actualDsn ->getScheme ());
29
- $ this ->assertSame ($ expectedDsn ->getHost (), $ actualDsn ->getHost ());
30
- $ this ->assertSame ($ expectedDsn ->getPort (), $ actualDsn ->getPort ());
31
- $ this ->assertSame ($ expectedDsn ->getUser (), $ actualDsn ->getUser ());
32
- $ this ->assertSame ($ expectedDsn ->getPassword (), $ actualDsn ->getPassword ());
33
- $ this ->assertSame ($ expectedDsn ->getPath (), $ actualDsn ->getPath ());
34
- $ this ->assertSame ($ expectedDsn ->getOption ('from ' ), $ actualDsn ->getOption ('from ' ));
35
-
36
- $ this ->assertSame ($ string , $ actualDsn ->getOriginalDsn ());
29
+ $ this ->assertSame ($ scheme , $ dsn ->getScheme ());
30
+ $ this ->assertSame ($ host , $ dsn ->getHost ());
31
+ $ this ->assertSame ($ user , $ dsn ->getUser ());
32
+ $ this ->assertSame ($ password , $ dsn ->getPassword ());
33
+ $ this ->assertSame ($ port , $ dsn ->getPort ());
34
+ $ this ->assertSame ($ path , $ dsn ->getPath ());
35
+ $ this ->assertSame ($ options , $ dsn ->getOptions ());
37
36
}
38
37
39
- public function fromStringProvider (): iterable
38
+ public function constructProvider (): iterable
40
39
{
41
40
yield 'simple dsn ' => [
42
41
'scheme://localhost ' ,
43
- new Dsn ('scheme ' , 'localhost ' , null , null , null , [], null ),
42
+ 'scheme ' ,
43
+ 'localhost ' ,
44
44
];
45
45
46
46
yield 'simple dsn including @ sign, but no user/password/token ' => [
47
47
'scheme://@localhost ' ,
48
- new Dsn ('scheme ' , 'localhost ' , null , null ),
48
+ 'scheme ' ,
49
+ 'localhost ' ,
49
50
];
50
51
51
52
yield 'simple dsn including : sign and @ sign, but no user/password/token ' => [
52
53
'scheme://:@localhost ' ,
53
- new Dsn ('scheme ' , 'localhost ' , null , null ),
54
+ 'scheme ' ,
55
+ 'localhost ' ,
54
56
];
55
57
56
58
yield 'simple dsn including user, : sign and @ sign, but no password ' => [
57
59
'scheme://user1:@localhost ' ,
58
- new Dsn ('scheme ' , 'localhost ' , 'user1 ' , null ),
60
+ 'scheme ' ,
61
+ 'localhost ' ,
62
+ 'user1 ' ,
59
63
];
60
64
61
65
yield 'simple dsn including : sign, password, and @ sign, but no user ' => [
62
66
'scheme://:pass@localhost ' ,
63
- new Dsn ('scheme ' , 'localhost ' , null , 'pass ' ),
67
+ 'scheme ' ,
68
+ 'localhost ' ,
69
+ null ,
70
+ 'pass ' ,
64
71
];
65
72
66
73
yield 'dsn with user and pass ' => [
67
74
'scheme://u$er:pa$s@localhost ' ,
68
- new Dsn ('scheme ' , 'localhost ' , 'u$er ' , 'pa$s ' , null , [], null ),
75
+ 'scheme ' ,
76
+ 'localhost ' ,
77
+ 'u$er ' ,
78
+ 'pa$s ' ,
69
79
];
70
80
71
81
yield 'dsn with user and pass and custom port ' => [
72
82
'scheme://u$er:pa$s@localhost:8000 ' ,
73
- new Dsn ('scheme ' , 'localhost ' , 'u$er ' , 'pa$s ' , '8000 ' , [], null ),
83
+ 'scheme ' ,
84
+ 'localhost ' ,
85
+ 'u$er ' ,
86
+ 'pa$s ' ,
87
+ 8000 ,
74
88
];
75
89
76
90
yield 'dsn with user and pass, custom port and custom path ' => [
77
91
'scheme://u$er:pa$s@localhost:8000/channel ' ,
78
- new Dsn ('scheme ' , 'localhost ' , 'u$er ' , 'pa$s ' , '8000 ' , [], '/channel ' ),
92
+ 'scheme ' ,
93
+ 'localhost ' ,
94
+ 'u$er ' ,
95
+ 'pa$s ' ,
96
+ 8000 ,
97
+ [],
98
+ '/channel ' ,
79
99
];
80
100
81
- yield 'dsn with user and pass, custom port, custom path and custom options ' => [
101
+ yield 'dsn with user and pass, custom port, custom path and custom option ' => [
82
102
'scheme://u$er:pa$s@localhost:8000/channel?from=FROM ' ,
83
- new Dsn ('scheme ' , 'localhost ' , 'u$er ' , 'pa$s ' , '8000 ' , ['from ' => 'FROM ' ], '/channel ' ),
103
+ 'scheme ' ,
104
+ 'localhost ' ,
105
+ 'u$er ' ,
106
+ 'pa$s ' ,
107
+ 8000 ,
108
+ [
109
+ 'from ' => 'FROM ' ,
110
+ ],
111
+ '/channel ' ,
112
+ ];
113
+
114
+ yield 'dsn with user and pass, custom port, custom path and custom options ' => [
115
+ 'scheme://u$er:pa$s@localhost:8000/channel?from=FROM&to=TO ' ,
116
+ 'scheme ' ,
117
+ 'localhost ' ,
118
+ 'u$er ' ,
119
+ 'pa$s ' ,
120
+ 8000 ,
121
+ [
122
+ 'from ' => 'FROM ' ,
123
+ 'to ' => 'TO ' ,
124
+ ],
125
+ '/channel ' ,
126
+ ];
127
+
128
+ yield 'dsn with user and pass, custom port, custom path and custom options and custom options keep the same order ' => [
129
+ 'scheme://u$er:pa$s@localhost:8000/channel?to=TO&from=FROM ' ,
130
+ 'scheme ' ,
131
+ 'localhost ' ,
132
+ 'u$er ' ,
133
+ 'pa$s ' ,
134
+ 8000 ,
135
+ [
136
+ 'to ' => 'TO ' ,
137
+ 'from ' => 'FROM ' ,
138
+ ],
139
+ '/channel ' ,
84
140
];
85
141
}
86
142
87
143
/**
88
144
* @dataProvider invalidDsnProvider
89
145
*/
90
- public function testInvalidDsn (string $ dsn , string $ exceptionMessage )
146
+ public function testInvalidDsn (string $ dsnString , string $ exceptionMessage )
91
147
{
92
148
$ this ->expectException (InvalidArgumentException::class);
93
149
$ this ->expectExceptionMessage ($ exceptionMessage );
94
- Dsn::fromString ($ dsn );
150
+
151
+ new Dsn ($ dsnString );
95
152
}
96
153
97
154
public function invalidDsnProvider (): iterable
@@ -112,38 +169,75 @@ public function invalidDsnProvider(): iterable
112
169
];
113
170
}
114
171
115
- public function testGetOption ()
172
+ /**
173
+ * @dataProvider getOptionProvider
174
+ */
175
+ public function testGetOption ($ expected , string $ dsnString , string $ option , ?string $ default = null )
116
176
{
117
- $ options = ['with_value ' => 'some value ' , 'nullable ' => null ];
118
- $ dsn = new Dsn ('scheme ' , 'localhost ' , 'u$er ' , 'pa$s ' , '8000 ' , $ options , '/channel ' );
177
+ $ dsn = new Dsn ($ dsnString );
119
178
120
- $ this ->assertSame ('some value ' , $ dsn ->getOption ('with_value ' ));
121
- $ this ->assertSame ('default ' , $ dsn ->getOption ('nullable ' , 'default ' ));
122
- $ this ->assertSame ('default ' , $ dsn ->getOption ('not_existent_property ' , 'default ' ));
179
+ $ this ->assertSame ($ expected , $ dsn ->getOption ($ option , $ default ));
123
180
}
124
181
125
- public function testGetRequiredOptionGetsOptionIfSet ()
182
+ public function getOptionProvider (): iterable
126
183
{
127
- $ options = ['with_value ' => 'some value ' ];
128
- $ dsn = new Dsn ('scheme ' , 'localhost ' , 'u$er ' , 'pa$s ' , '8000 ' , $ options , '/channel ' );
184
+ yield [
185
+ 'foo ' ,
186
+ 'scheme://localhost?with_value=foo ' ,
187
+ 'with_value ' ,
188
+ ];
189
+
190
+ yield [
191
+ '' ,
192
+ 'scheme://localhost?empty= ' ,
193
+ 'empty ' ,
194
+ ];
129
195
130
- $ this ->assertSame ('some value ' , $ dsn ->getRequiredOption ('with_value ' ));
196
+ yield [
197
+ '0 ' ,
198
+ 'scheme://localhost?zero=0 ' ,
199
+ 'zero ' ,
200
+ ];
201
+
202
+ yield [
203
+ 'default-value ' ,
204
+ 'scheme://localhost?option=value ' ,
205
+ 'non_existent_property ' ,
206
+ 'default-value ' ,
207
+ ];
208
+ }
209
+
210
+ /**
211
+ * @dataProvider getRequiredOptionProvider
212
+ */
213
+ public function testGetRequiredOption (string $ expectedValue , string $ options , string $ option )
214
+ {
215
+ $ dsn = new Dsn (sprintf ('scheme://localhost?%s ' , $ options ));
216
+
217
+ $ this ->assertSame ($ expectedValue , $ dsn ->getRequiredOption ($ option ));
131
218
}
132
219
133
- public function testGetRequiredOptionGetsOptionIfValueIsZero ()
220
+ public function getRequiredOptionProvider (): iterable
134
221
{
135
- $ options = ['timeout ' => 0 ];
136
- $ dsn = new Dsn ('scheme ' , 'localhost ' , 'u$er ' , 'pa$s ' , '8000 ' , $ options , '/channel ' );
222
+ yield [
223
+ 'value ' ,
224
+ 'with_value=value ' ,
225
+ 'with_value ' ,
226
+ ];
137
227
138
- $ this ->assertSame (0 , $ dsn ->getRequiredOption ('timeout ' ));
228
+ yield [
229
+ '0 ' ,
230
+ 'timeout=0 ' ,
231
+ 'timeout ' ,
232
+ ];
139
233
}
140
234
141
235
/**
142
236
* @dataProvider getRequiredOptionThrowsMissingRequiredOptionExceptionProvider
143
237
*/
144
- public function testGetRequiredOptionThrowsMissingRequiredOptionException (string $ expectedExceptionMessage , array $ options , string $ option )
238
+ public function testGetRequiredOptionThrowsMissingRequiredOptionException (string $ expectedExceptionMessage , string $ options , string $ option )
145
239
{
146
- $ dsn = new Dsn ('scheme ' , ' localhost ' , ' u$er ' , ' pa$ s ' , ' 8000 ' , $ options, ' /channel ' );
240
+ $ dsn = new Dsn (sprintf ( 'scheme:// localhost?% s ' , $ options) );
147
241
148
242
$ this ->expectException (MissingRequiredOptionException::class);
149
243
$ this ->expectExceptionMessage ($ expectedExceptionMessage );
@@ -155,20 +249,14 @@ public function getRequiredOptionThrowsMissingRequiredOptionExceptionProvider():
155
249
{
156
250
yield [
157
251
'The option "foo_bar" is required but missing. ' ,
158
- [ 'with_value ' => ' some value '] ,
252
+ 'with_value= value ' ,
159
253
'foo_bar ' ,
160
254
];
161
255
162
256
yield [
163
257
'The option "with_empty_string" is required but missing. ' ,
164
- [ 'with_empty_string ' => '' ] ,
258
+ 'with_empty_string= ' ,
165
259
'with_empty_string ' ,
166
260
];
167
-
168
- yield [
169
- 'The option "with_null" is required but missing. ' ,
170
- ['with_null ' => null ],
171
- 'with_null ' ,
172
- ];
173
261
}
174
262
}
0 commit comments