-
-
Notifications
You must be signed in to change notification settings - Fork 301
/
Copy pathInputBagTest.php
221 lines (171 loc) · 8.28 KB
/
InputBagTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
use Symfony\Component\HttpFoundation\InputBag;
use Symfony\Component\HttpFoundation\Tests\Fixtures\FooEnum;
class InputBagTest extends TestCase
{
public function testGet()
{
$bag = new InputBag(['foo' => 'bar', 'null' => null, 'int' => 1, 'float' => 1.0, 'bool' => false, 'stringable' => new class implements \Stringable {
public function __toString(): string
{
return 'strval';
}
}]);
$this->assertSame('bar', $bag->get('foo'), '->get() gets the value of a string parameter');
$this->assertSame('default', $bag->get('unknown', 'default'), '->get() returns second argument as default if a parameter is not defined');
$this->assertNull($bag->get('null', 'default'), '->get() returns null if null is set');
$this->assertSame(1, $bag->get('int'), '->get() gets the value of an int parameter');
$this->assertSame(1.0, $bag->get('float'), '->get() gets the value of a float parameter');
$this->assertSame('strval', $bag->get('stringable'), '->get() gets the string value of a \Stringable parameter');
$this->assertFalse($bag->get('bool'), '->get() gets the value of a bool parameter');
}
public function testGetIntError()
{
$bag = new InputBag(['foo' => 'bar']);
$this->expectException(BadRequestException::class);
$this->expectExceptionMessage('Input value "foo" is invalid and flag "FILTER_NULL_ON_FAILURE" was not set.');
$bag->getInt('foo');
}
public function testGetBooleanError()
{
$bag = new InputBag(['foo' => 'bar']);
$this->expectException(BadRequestException::class);
$this->expectExceptionMessage('Input value "foo" is invalid and flag "FILTER_NULL_ON_FAILURE" was not set.');
$bag->getBoolean('foo');
}
public function testGetString()
{
$bag = new InputBag(['integer' => 123, 'bool_true' => true, 'bool_false' => false, 'string' => 'abc', 'stringable' => new class implements \Stringable {
public function __toString(): string
{
return 'strval';
}
}]);
$this->assertSame('123', $bag->getString('integer'), '->getString() gets a value of parameter as string');
$this->assertSame('abc', $bag->getString('string'), '->getString() gets a value of parameter as string');
$this->assertSame('', $bag->getString('unknown'), '->getString() returns zero if a parameter is not defined');
$this->assertSame('foo', $bag->getString('unknown', 'foo'), '->getString() returns the default if a parameter is not defined');
$this->assertSame('1', $bag->getString('bool_true'), '->getString() returns "1" if a parameter is true');
$this->assertSame('', $bag->getString('bool_false', 'foo'), '->getString() returns an empty empty string if a parameter is false');
$this->assertSame('strval', $bag->getString('stringable'), '->getString() gets a value of a stringable parameter as string');
}
public function testGetStringExceptionWithArray()
{
$bag = new InputBag(['key' => ['abc']]);
$this->expectException(BadRequestException::class);
$this->expectExceptionMessage('Input value "key" contains a non-scalar value.');
$bag->getString('key');
}
public function testGetDoesNotUseDeepByDefault()
{
$bag = new InputBag(['foo' => ['bar' => 'moo']]);
$this->assertNull($bag->get('foo[bar]'));
}
public function testFilterArray()
{
$bag = new InputBag([
'foo' => ['12', '8'],
]);
$result = $bag->filter('foo', null, \FILTER_VALIDATE_INT, \FILTER_FORCE_ARRAY);
$this->assertSame([12, 8], $result);
}
public function testFilterCallback()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('A Closure must be passed to "Symfony\Component\HttpFoundation\InputBag::filter()" when FILTER_CALLBACK is used, "string" given.');
$bag = new InputBag(['foo' => 'bar']);
$bag->filter('foo', null, \FILTER_CALLBACK, ['options' => 'strtoupper']);
}
public function testFilterClosure()
{
$bag = new InputBag(['foo' => 'bar']);
$result = $bag->filter('foo', null, \FILTER_CALLBACK, ['options' => strtoupper(...)]);
$this->assertSame('BAR', $result);
}
public function testSetWithNonScalarOrArray()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Expected a scalar, or an array as a 2nd argument to "Symfony\Component\HttpFoundation\InputBag::set()", "Symfony\Component\HttpFoundation\InputBag" given.');
$bag = new InputBag();
$bag->set('foo', new InputBag());
}
public function testGettingANonStringValue()
{
$this->expectException(BadRequestException::class);
$this->expectExceptionMessage('Input value "foo" contains a non-scalar value.');
$bag = new InputBag(['foo' => ['a', 'b']]);
$bag->get('foo');
}
public function testGetWithNonStringDefaultValue()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Expected a scalar value as a 2nd argument to "Symfony\Component\HttpFoundation\InputBag::get()", "array" given.');
$bag = new InputBag(['foo' => 'bar']);
$bag->get('foo', ['a', 'b']);
}
public function testFilterArrayWithoutArrayFlag()
{
$this->expectException(BadRequestException::class);
$this->expectExceptionMessage('Input value "foo" contains an array, but "FILTER_REQUIRE_ARRAY" or "FILTER_FORCE_ARRAY" flags were not set.');
$bag = new InputBag(['foo' => ['bar', 'baz']]);
$bag->filter('foo', \FILTER_VALIDATE_INT);
}
public function testAdd()
{
$bag = new InputBag(['foo' => 'bar']);
$bag->add(['baz' => 'qux']);
$this->assertSame('bar', $bag->get('foo'), '->add() does not remove existing parameters');
$this->assertSame('qux', $bag->get('baz'), '->add() adds new parameters');
}
public function testReplace()
{
$bag = new InputBag(['foo' => 'bar']);
$bag->replace(['baz' => 'qux']);
$this->assertNull($bag->get('foo'), '->replace() removes existing parameters');
$this->assertSame('qux', $bag->get('baz'), '->replace() adds new parameters');
}
public function testGetEnum()
{
$bag = new InputBag(['valid-value' => 1]);
$this->assertSame(FooEnum::Bar, $bag->getEnum('valid-value', FooEnum::class));
}
public function testGetEnumThrowsExceptionWithInvalidValue()
{
$bag = new InputBag(['invalid-value' => 2]);
$this->expectException(BadRequestException::class);
$this->expectExceptionMessage('Parameter "invalid-value" cannot be converted to enum: 2 is not a valid backing value for enum Symfony\Component\HttpFoundation\Tests\Fixtures\FooEnum.');
$this->assertNull($bag->getEnum('invalid-value', FooEnum::class));
}
public function testGetAlnumExceptionWithArray()
{
$bag = new InputBag(['word' => ['foo_BAR_012']]);
$this->expectException(BadRequestException::class);
$this->expectExceptionMessage('Input value "word" contains a non-scalar value.');
$bag->getAlnum('word');
}
public function testGetAlphaExceptionWithArray()
{
$bag = new InputBag(['word' => ['foo_BAR_012']]);
$this->expectException(BadRequestException::class);
$this->expectExceptionMessage('Input value "word" contains a non-scalar value.');
$bag->getAlpha('word');
}
public function testGetDigitsExceptionWithArray()
{
$bag = new InputBag(['word' => ['foo_BAR_012']]);
$this->expectException(BadRequestException::class);
$this->expectExceptionMessage('Input value "word" contains a non-scalar value.');
$bag->getDigits('word');
}
}