@@ -21,66 +21,123 @@ final class DsnTest extends TestCase
21
21
/**
22
22
* @dataProvider fromStringProvider
23
23
*/
24
- public function testFromString (string $ string, Dsn $ expectedDsn )
24
+ public function testFromString (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 = Dsn::fromString ($ dsnString );
27
27
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 ' ));
28
+ $ this ->assertSame ($ scheme , $ dsn ->getScheme ());
29
+ $ this ->assertSame ($ host , $ dsn ->getHost ());
30
+ $ this ->assertSame ($ user , $ dsn -> getUser ());
31
+ $ this ->assertSame ($ password , $ dsn -> getPassword ());
32
+ $ this ->assertSame ($ port , $ dsn -> getPort ());
33
+ $ this ->assertSame ($ path , $ dsn ->getPath ());
34
+ $ this ->assertSame ($ options , $ dsn -> getOptions ( ));
35
35
36
- $ this ->assertSame ($ string , $ actualDsn ->getOriginalDsn ());
36
+ $ this ->assertSame ($ dsnString , $ dsn ->getOriginalDsn ());
37
37
}
38
38
39
39
public function fromStringProvider (): iterable
40
40
{
41
41
yield 'simple dsn ' => [
42
42
'scheme://localhost ' ,
43
- new Dsn ('scheme ' , 'localhost ' , null , null , null , [], null ),
43
+ 'scheme ' ,
44
+ 'localhost ' ,
44
45
];
45
46
46
47
yield 'simple dsn including @ sign, but no user/password/token ' => [
47
48
'scheme://@localhost ' ,
48
- new Dsn ('scheme ' , 'localhost ' , null , null ),
49
+ 'scheme ' ,
50
+ 'localhost ' ,
49
51
];
50
52
51
53
yield 'simple dsn including : sign and @ sign, but no user/password/token ' => [
52
54
'scheme://:@localhost ' ,
53
- new Dsn ('scheme ' , 'localhost ' , null , null ),
55
+ 'scheme ' ,
56
+ 'localhost ' ,
54
57
];
55
58
56
59
yield 'simple dsn including user, : sign and @ sign, but no password ' => [
57
60
'scheme://user1:@localhost ' ,
58
- new Dsn ('scheme ' , 'localhost ' , 'user1 ' , null ),
61
+ 'scheme ' ,
62
+ 'localhost ' ,
63
+ 'user1 ' ,
59
64
];
60
65
61
66
yield 'simple dsn including : sign, password, and @ sign, but no user ' => [
62
67
'scheme://:pass@localhost ' ,
63
- new Dsn ('scheme ' , 'localhost ' , null , 'pass ' ),
68
+ 'scheme ' ,
69
+ 'localhost ' ,
70
+ null ,
71
+ 'pass ' ,
64
72
];
65
73
66
74
yield 'dsn with user and pass ' => [
67
75
'scheme://u$er:pa$s@localhost ' ,
68
- new Dsn ('scheme ' , 'localhost ' , 'u$er ' , 'pa$s ' , null , [], null ),
76
+ 'scheme ' ,
77
+ 'localhost ' ,
78
+ 'u$er ' ,
79
+ 'pa$s ' ,
69
80
];
70
81
71
82
yield 'dsn with user and pass and custom port ' => [
72
83
'scheme://u$er:pa$s@localhost:8000 ' ,
73
- new Dsn ('scheme ' , 'localhost ' , 'u$er ' , 'pa$s ' , '8000 ' , [], null ),
84
+ 'scheme ' ,
85
+ 'localhost ' ,
86
+ 'u$er ' ,
87
+ 'pa$s ' ,
88
+ 8000 ,
74
89
];
75
90
76
91
yield 'dsn with user and pass, custom port and custom path ' => [
77
92
'scheme://u$er:pa$s@localhost:8000/channel ' ,
78
- new Dsn ('scheme ' , 'localhost ' , 'u$er ' , 'pa$s ' , '8000 ' , [], '/channel ' ),
93
+ 'scheme ' ,
94
+ 'localhost ' ,
95
+ 'u$er ' ,
96
+ 'pa$s ' ,
97
+ 8000 ,
98
+ [],
99
+ '/channel ' ,
79
100
];
80
101
81
- yield 'dsn with user and pass, custom port, custom path and custom options ' => [
102
+ yield 'dsn with user and pass, custom port, custom path and custom option ' => [
82
103
'scheme://u$er:pa$s@localhost:8000/channel?from=FROM ' ,
83
- new Dsn ('scheme ' , 'localhost ' , 'u$er ' , 'pa$s ' , '8000 ' , ['from ' => 'FROM ' ], '/channel ' ),
104
+ 'scheme ' ,
105
+ 'localhost ' ,
106
+ 'u$er ' ,
107
+ 'pa$s ' ,
108
+ 8000 ,
109
+ [
110
+ 'from ' => 'FROM ' ,
111
+ ],
112
+ '/channel ' ,
113
+ ];
114
+
115
+ yield 'dsn with user and pass, custom port, custom path and custom options ' => [
116
+ 'scheme://u$er:pa$s@localhost:8000/channel?from=FROM&to=TO ' ,
117
+ 'scheme ' ,
118
+ 'localhost ' ,
119
+ 'u$er ' ,
120
+ 'pa$s ' ,
121
+ 8000 ,
122
+ [
123
+ 'from ' => 'FROM ' ,
124
+ 'to ' => 'TO ' ,
125
+ ],
126
+ '/channel ' ,
127
+ ];
128
+
129
+ yield 'dsn with user and pass, custom port, custom path and custom options and custom options keep the same order ' => [
130
+ 'scheme://u$er:pa$s@localhost:8000/channel?to=TO&from=FROM ' ,
131
+ 'scheme ' ,
132
+ 'localhost ' ,
133
+ 'u$er ' ,
134
+ 'pa$s ' ,
135
+ 8000 ,
136
+ [
137
+ 'to ' => 'TO ' ,
138
+ 'from ' => 'FROM ' ,
139
+ ],
140
+ '/channel ' ,
84
141
];
85
142
}
86
143
@@ -91,6 +148,7 @@ public function testInvalidDsn(string $dsn, string $exceptionMessage)
91
148
{
92
149
$ this ->expectException (InvalidArgumentException::class);
93
150
$ this ->expectExceptionMessage ($ exceptionMessage );
151
+
94
152
Dsn::fromString ($ dsn );
95
153
}
96
154
@@ -112,14 +170,43 @@ public function invalidDsnProvider(): iterable
112
170
];
113
171
}
114
172
115
- public function testGetOption ()
173
+ /**
174
+ * @dataProvider getOptionProvider
175
+ */
176
+ public function testGetOption ($ expected , string $ dsnString , string $ option , ?string $ default = null )
116
177
{
117
- $ options = ['with_value ' => 'some value ' , 'nullable ' => null ];
118
- $ dsn = new Dsn ('scheme ' , 'localhost ' , 'u$er ' , 'pa$s ' , '8000 ' , $ options , '/channel ' );
178
+ $ dsn = Dsn::fromString ($ dsnString );
179
+
180
+ $ this ->assertSame ($ expected , $ dsn ->getOption ($ option , $ default ));
181
+ }
119
182
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 ' ));
183
+ public function getOptionProvider (): iterable
184
+ {
185
+ yield [
186
+ 'foo ' ,
187
+ 'scheme://u$er:pa$s@localhost:8000/channel?with_value=foo ' ,
188
+ 'with_value ' ,
189
+ ];
190
+
191
+ yield [
192
+ 'default-value ' ,
193
+ 'scheme://u$er:pa$s@localhost:8000/channel?empty= ' ,
194
+ 'empty ' ,
195
+ 'default-value '
196
+ ];
197
+
198
+ yield [
199
+ 0 ,
200
+ 'scheme://u$er:pa$s@localhost:8000/channel?zero=0 ' ,
201
+ 'zero ' ,
202
+ ];
203
+
204
+ yield [
205
+ 'default-value ' ,
206
+ 'scheme://u$er:pa$s@localhost:8000/channel?option=value ' ,
207
+ 'non_existent_property ' ,
208
+ 'default-value '
209
+ ];
123
210
}
124
211
125
212
public function testGetRequiredOptionGetsOptionIfSet ()
0 commit comments