12
12
namespace Symfony \Component \HttpFoundation \Tests ;
13
13
14
14
use PHPUnit \Framework \TestCase ;
15
+ use Symfony \Bridge \PhpUnit \ExpectDeprecationTrait ;
15
16
use Symfony \Component \HttpFoundation \Exception \BadRequestException ;
16
17
use Symfony \Component \HttpFoundation \ParameterBag ;
17
18
18
19
class ParameterBagTest extends TestCase
19
20
{
21
+ use ExpectDeprecationTrait;
22
+
20
23
public function testConstructor ()
21
24
{
22
25
$ this ->testAll ();
@@ -111,34 +114,154 @@ public function testHas()
111
114
112
115
public function testGetAlpha ()
113
116
{
114
- $ bag = new ParameterBag (['word ' => 'foo_BAR_012 ' ]);
117
+ $ bag = new ParameterBag (['word ' => 'foo_BAR_012 ' , 'bool ' => true , 'integer ' => 123 ]);
118
+
119
+ $ this ->assertSame ('fooBAR ' , $ bag ->getAlpha ('word ' ), '->getAlpha() gets only alphabetic characters ' );
120
+ $ this ->assertSame ('' , $ bag ->getAlpha ('unknown ' ), '->getAlpha() returns empty string if a parameter is not defined ' );
121
+ $ this ->assertSame ('abcDEF ' , $ bag ->getAlpha ('unknown ' , 'abc_DEF_012 ' ), '->getAlpha() returns filtered default if a parameter is not defined ' );
122
+ $ this ->assertSame ('' , $ bag ->getAlpha ('integer ' , 'abc_DEF_012 ' ), '->getAlpha() returns empty string if a parameter is an integer ' );
123
+ $ this ->assertSame ('' , $ bag ->getAlpha ('bool ' , 'abc_DEF_012 ' ), '->getAlpha() returns empty string if a parameter is a boolean ' );
124
+ }
115
125
116
- $ this ->assertEquals ('fooBAR ' , $ bag ->getAlpha ('word ' ), '->getAlpha() gets only alphabetic characters ' );
117
- $ this ->assertEquals ('' , $ bag ->getAlpha ('unknown ' ), '->getAlpha() returns empty string if a parameter is not defined ' );
126
+ public function testGetAlphaExceptionWithArray ()
127
+ {
128
+ $ bag = new ParameterBag (['word ' => ['foo_BAR_012 ' ]]);
129
+
130
+ $ this ->expectException (\UnexpectedValueException::class);
131
+ $ this ->expectExceptionMessage ('Parameter value "word" cannot be converted to "string". ' );
132
+
133
+ $ bag ->getAlpha ('word ' );
118
134
}
119
135
120
136
public function testGetAlnum ()
121
137
{
122
- $ bag = new ParameterBag (['word ' => 'foo_BAR_012 ' ]);
138
+ $ bag = new ParameterBag (['word ' => 'foo_BAR_012 ' , ' bool ' => true , ' integer ' => 123 ]);
123
139
124
- $ this ->assertEquals ('fooBAR012 ' , $ bag ->getAlnum ('word ' ), '->getAlnum() gets only alphanumeric characters ' );
125
- $ this ->assertEquals ('' , $ bag ->getAlnum ('unknown ' ), '->getAlnum() returns empty string if a parameter is not defined ' );
140
+ $ this ->assertSame ('fooBAR012 ' , $ bag ->getAlnum ('word ' ), '->getAlnum() gets only alphanumeric characters ' );
141
+ $ this ->assertSame ('' , $ bag ->getAlnum ('unknown ' ), '->getAlnum() returns empty string if a parameter is not defined ' );
142
+ $ this ->assertSame ('abcDEF012 ' , $ bag ->getAlnum ('unknown ' , 'abc_DEF_012 ' ), '->getAlnum() returns filtered default if a parameter is not defined ' );
143
+ $ this ->assertSame ('123 ' , $ bag ->getAlnum ('integer ' , 'abc_DEF_012 ' ), '->getAlnum() returns the number as string if a parameter is an integer ' );
144
+ $ this ->assertSame ('1 ' , $ bag ->getAlnum ('bool ' , 'abc_DEF_012 ' ), '->getAlnum() returns 1 if a parameter is true ' );
145
+ }
146
+
147
+ public function testGetAlnumExceptionWithArray ()
148
+ {
149
+ $ bag = new ParameterBag (['word ' => ['foo_BAR_012 ' ]]);
150
+
151
+ $ this ->expectException (\UnexpectedValueException::class);
152
+ $ this ->expectExceptionMessage ('Parameter value "word" cannot be converted to "string". ' );
153
+
154
+ $ bag ->getAlnum ('word ' );
126
155
}
127
156
128
157
public function testGetDigits ()
129
158
{
130
- $ bag = new ParameterBag (['word ' => 'foo_BAR_012 ' ]);
159
+ $ bag = new ParameterBag (['word ' => 'foo_BAR_012 ' , ' bool ' => true , ' integer ' => 123 ]);
131
160
132
- $ this ->assertEquals ('012 ' , $ bag ->getDigits ('word ' ), '->getDigits() gets only digits as string ' );
133
- $ this ->assertEquals ('' , $ bag ->getDigits ('unknown ' ), '->getDigits() returns empty string if a parameter is not defined ' );
161
+ $ this ->assertSame ('012 ' , $ bag ->getDigits ('word ' ), '->getDigits() gets only digits as string ' );
162
+ $ this ->assertSame ('' , $ bag ->getDigits ('unknown ' ), '->getDigits() returns empty string if a parameter is not defined ' );
163
+ $ this ->assertSame ('012 ' , $ bag ->getDigits ('unknown ' , 'abc_DEF_012 ' ), '->getDigits() returns filtered default if a parameter is not defined ' );
164
+ $ this ->assertSame ('123 ' , $ bag ->getDigits ('integer ' , 'abc_DEF_012 ' ), '->getDigits() returns the number as string if a parameter is an integer ' );
165
+ $ this ->assertSame ('1 ' , $ bag ->getDigits ('bool ' , 'abc_DEF_012 ' ), '->getDigits() returns 1 if a parameter is true ' );
134
166
}
135
167
168
+ public function testGetDigitsExceptionWithArray ()
169
+ {
170
+ $ bag = new ParameterBag (['word ' => ['foo_BAR_012 ' ]]);
171
+
172
+ $ this ->expectException (\UnexpectedValueException::class);
173
+ $ this ->expectExceptionMessage ('Parameter value "word" cannot be converted to "string". ' );
174
+
175
+ $ bag ->getDigits ('word ' );
176
+ }
177
+
178
+ /**
179
+ * @group legacy
180
+ */
136
181
public function testGetInt ()
137
182
{
138
- $ bag = new ParameterBag (['digits ' => '0123 ' ]);
183
+ $ this ->expectDeprecation ('Since symfony/http-foundation 6.3: Method "getInt" is deprecated and replaced by "getInteger". ' );
184
+
185
+ $ bag = new ParameterBag (['digits ' => '123 ' , 'bool ' => true , 'string ' => 'abc ' ]);
186
+
187
+ $ this ->assertSame (123 , $ bag ->getInt ('digits ' ), '->getInt() gets a value of parameter as integer ' );
188
+ $ this ->assertSame (0 , $ bag ->getInt ('unknown ' ), '->getInt() returns zero if a parameter is not defined ' );
189
+ $ this ->assertSame (10 , $ bag ->getInt ('unknown ' , 10 ), '->getInt() returns the default if a parameter is not defined ' );
190
+ $ this ->assertSame (1 , $ bag ->getInt ('bool ' , 10 ), '->getInt() returns zero if a parameter is invalid ' );
191
+ $ this ->assertSame (0 , $ bag ->getInt ('string ' , 10 ), '->getInt() returns zero if a parameter is invalid ' );
192
+ }
193
+
194
+ public function testGetInteger ()
195
+ {
196
+ $ bag = new ParameterBag (['digits ' => '123 ' , 'bool ' => true ]);
139
197
140
- $ this ->assertEquals (123 , $ bag ->getInt ('digits ' ), '->getInt() gets a value of parameter as integer ' );
141
- $ this ->assertEquals (0 , $ bag ->getInt ('unknown ' ), '->getInt() returns zero if a parameter is not defined ' );
198
+ $ this ->assertSame (123 , $ bag ->getInteger ('digits ' ), '->getInteger() gets a value of parameter as integer ' );
199
+ $ this ->assertSame (0 , $ bag ->getInteger ('unknown ' ), '->getInteger() returns zero if a parameter is not defined ' );
200
+ $ this ->assertSame (10 , $ bag ->getInteger ('unknown ' , 10 ), '->getInteger() returns the default if a parameter is not defined ' );
201
+ $ this ->assertSame (1 , $ bag ->getInteger ('bool ' ), '->getInteger() returns 1 if a parameter is true ' );
202
+ }
203
+
204
+ public function testGetIntegerExceptionWithArray ()
205
+ {
206
+ $ bag = new ParameterBag (['digits ' => ['123 ' ]]);
207
+
208
+ $ this ->expectException (\UnexpectedValueException::class);
209
+ $ this ->expectExceptionMessage ('Parameter value "digits" cannot be converted to "int". ' );
210
+
211
+ $ bag ->getInteger ('digits ' );
212
+ }
213
+
214
+ public function testGetIntegerExceptionWithObject ()
215
+ {
216
+ $ bag = new ParameterBag (['object ' => $ this ]);
217
+
218
+ $ this ->expectException (\UnexpectedValueException::class);
219
+ $ this ->expectExceptionMessage ('Parameter value "object" cannot be filtered. ' );
220
+
221
+ $ bag ->getInteger ('object ' );
222
+ }
223
+
224
+ public function testGetIntegerExceptionWithInvalid ()
225
+ {
226
+ $ bag = new ParameterBag (['word ' => 'foo_BAR_012 ' ]);
227
+
228
+ $ this ->expectException (\UnexpectedValueException::class);
229
+ $ this ->expectExceptionMessage ('Parameter value "word" cannot be converted to "int". ' );
230
+
231
+ $ bag ->getInteger ('word ' );
232
+ }
233
+
234
+ public function testGetString ()
235
+ {
236
+ $ bag = new ParameterBag (['integer ' => 123 , 'bool_true ' => true , 'bool_false ' => false , 'string ' => 'abc ' , 'stringable ' => new InputStringable ('bar ' )]);
237
+
238
+ $ this ->assertSame ('123 ' , $ bag ->getString ('integer ' ), '->getString() gets a value of parameter as string ' );
239
+ $ this ->assertSame ('abc ' , $ bag ->getString ('string ' ), '->getString() gets a value of parameter as string ' );
240
+ $ this ->assertSame ('' , $ bag ->getString ('unknown ' ), '->getString() returns zero if a parameter is not defined ' );
241
+ $ this ->assertSame ('foo ' , $ bag ->getString ('unknown ' , 'foo ' ), '->getString() returns the default if a parameter is not defined ' );
242
+ $ this ->assertSame ('1 ' , $ bag ->getString ('bool_true ' ), '->getString() returns "1" if a parameter is true ' );
243
+ $ this ->assertSame ('' , $ bag ->getString ('bool_false ' , 'foo ' ), '->getString() returns an empty empty string if a parameter is false ' );
244
+ $ this ->assertSame ('bar ' , $ bag ->getString ('stringable ' ), '->getString() gets a value of a stringable paramater as string ' );
245
+ }
246
+
247
+ public function testGetStringExceptionWithArray ()
248
+ {
249
+ $ bag = new ParameterBag (['key ' => ['abc ' ]]);
250
+
251
+ $ this ->expectException (\UnexpectedValueException::class);
252
+ $ this ->expectExceptionMessage ('Parameter value "key" cannot be converted to "string". ' );
253
+
254
+ $ bag ->getString ('key ' );
255
+ }
256
+
257
+ public function testGetStringExceptionWithObject ()
258
+ {
259
+ $ bag = new ParameterBag (['object ' => $ this ]);
260
+
261
+ $ this ->expectException (\UnexpectedValueException::class);
262
+ $ this ->expectExceptionMessage ('Parameter value "object" cannot be converted to "string". ' );
263
+
264
+ $ bag ->getString ('object ' );
142
265
}
143
266
144
267
public function testFilter ()
@@ -219,11 +342,26 @@ public function testCount()
219
342
220
343
public function testGetBoolean ()
221
344
{
222
- $ parameters = ['string_true ' => 'true ' , 'string_false ' => 'false ' ];
345
+ $ parameters = ['string_true ' => 'true ' , 'string_false ' => 'false ' , ' string ' => ' abc ' ];
223
346
$ bag = new ParameterBag ($ parameters );
224
347
225
348
$ this ->assertTrue ($ bag ->getBoolean ('string_true ' ), '->getBoolean() gets the string true as boolean true ' );
226
349
$ this ->assertFalse ($ bag ->getBoolean ('string_false ' ), '->getBoolean() gets the string false as boolean false ' );
227
350
$ this ->assertFalse ($ bag ->getBoolean ('unknown ' ), '->getBoolean() returns false if a parameter is not defined ' );
351
+ $ this ->assertTrue ($ bag ->getBoolean ('unknown ' , true ), '->getBoolean() returns default if a parameter is not defined ' );
352
+ $ this ->assertFalse ($ bag ->getBoolean ('string ' ), '->getBoolean() returns false if a parameter is invalid ' );
353
+ $ this ->assertFalse ($ bag ->getBoolean ('string ' , true ), '->getBoolean() returns false if a parameter is invalid ' );
354
+ }
355
+ }
356
+
357
+ class InputStringable
358
+ {
359
+ public function __construct (private string $ value )
360
+ {
361
+ }
362
+
363
+ public function __toString (): string
364
+ {
365
+ return $ this ->value ;
228
366
}
229
367
}
0 commit comments