forked from symfony/http-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InputBagTest.php
109 lines (88 loc) · 4.05 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
<?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;
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 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' => function ($value) {
return strtoupper($value);
}]);
$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);
}
}