dccfd4f3086871227e0ab14343d8a17094a65b66
[yaffs-website] / vendor / symfony / http-foundation / Tests / ParameterBagTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\HttpFoundation\Tests;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\ParameterBag;
16
17 class ParameterBagTest extends TestCase
18 {
19     public function testConstructor()
20     {
21         $this->testAll();
22     }
23
24     public function testAll()
25     {
26         $bag = new ParameterBag(array('foo' => 'bar'));
27         $this->assertEquals(array('foo' => 'bar'), $bag->all(), '->all() gets all the input');
28     }
29
30     public function testKeys()
31     {
32         $bag = new ParameterBag(array('foo' => 'bar'));
33         $this->assertEquals(array('foo'), $bag->keys());
34     }
35
36     public function testAdd()
37     {
38         $bag = new ParameterBag(array('foo' => 'bar'));
39         $bag->add(array('bar' => 'bas'));
40         $this->assertEquals(array('foo' => 'bar', 'bar' => 'bas'), $bag->all());
41     }
42
43     public function testRemove()
44     {
45         $bag = new ParameterBag(array('foo' => 'bar'));
46         $bag->add(array('bar' => 'bas'));
47         $this->assertEquals(array('foo' => 'bar', 'bar' => 'bas'), $bag->all());
48         $bag->remove('bar');
49         $this->assertEquals(array('foo' => 'bar'), $bag->all());
50     }
51
52     public function testReplace()
53     {
54         $bag = new ParameterBag(array('foo' => 'bar'));
55
56         $bag->replace(array('FOO' => 'BAR'));
57         $this->assertEquals(array('FOO' => 'BAR'), $bag->all(), '->replace() replaces the input with the argument');
58         $this->assertFalse($bag->has('foo'), '->replace() overrides previously set the input');
59     }
60
61     public function testGet()
62     {
63         $bag = new ParameterBag(array('foo' => 'bar', 'null' => null));
64
65         $this->assertEquals('bar', $bag->get('foo'), '->get() gets the value of a parameter');
66         $this->assertEquals('default', $bag->get('unknown', 'default'), '->get() returns second argument as default if a parameter is not defined');
67         $this->assertNull($bag->get('null', 'default'), '->get() returns null if null is set');
68     }
69
70     public function testGetDoesNotUseDeepByDefault()
71     {
72         $bag = new ParameterBag(array('foo' => array('bar' => 'moo')));
73
74         $this->assertNull($bag->get('foo[bar]'));
75     }
76
77     public function testSet()
78     {
79         $bag = new ParameterBag(array());
80
81         $bag->set('foo', 'bar');
82         $this->assertEquals('bar', $bag->get('foo'), '->set() sets the value of parameter');
83
84         $bag->set('foo', 'baz');
85         $this->assertEquals('baz', $bag->get('foo'), '->set() overrides previously set parameter');
86     }
87
88     public function testHas()
89     {
90         $bag = new ParameterBag(array('foo' => 'bar'));
91
92         $this->assertTrue($bag->has('foo'), '->has() returns true if a parameter is defined');
93         $this->assertFalse($bag->has('unknown'), '->has() return false if a parameter is not defined');
94     }
95
96     public function testGetAlpha()
97     {
98         $bag = new ParameterBag(array('word' => 'foo_BAR_012'));
99
100         $this->assertEquals('fooBAR', $bag->getAlpha('word'), '->getAlpha() gets only alphabetic characters');
101         $this->assertEquals('', $bag->getAlpha('unknown'), '->getAlpha() returns empty string if a parameter is not defined');
102     }
103
104     public function testGetAlnum()
105     {
106         $bag = new ParameterBag(array('word' => 'foo_BAR_012'));
107
108         $this->assertEquals('fooBAR012', $bag->getAlnum('word'), '->getAlnum() gets only alphanumeric characters');
109         $this->assertEquals('', $bag->getAlnum('unknown'), '->getAlnum() returns empty string if a parameter is not defined');
110     }
111
112     public function testGetDigits()
113     {
114         $bag = new ParameterBag(array('word' => 'foo_BAR_012'));
115
116         $this->assertEquals('012', $bag->getDigits('word'), '->getDigits() gets only digits as string');
117         $this->assertEquals('', $bag->getDigits('unknown'), '->getDigits() returns empty string if a parameter is not defined');
118     }
119
120     public function testGetInt()
121     {
122         $bag = new ParameterBag(array('digits' => '0123'));
123
124         $this->assertEquals(123, $bag->getInt('digits'), '->getInt() gets a value of parameter as integer');
125         $this->assertEquals(0, $bag->getInt('unknown'), '->getInt() returns zero if a parameter is not defined');
126     }
127
128     public function testFilter()
129     {
130         $bag = new ParameterBag(array(
131             'digits' => '0123ab',
132             'email' => 'example@example.com',
133             'url' => 'http://example.com/foo',
134             'dec' => '256',
135             'hex' => '0x100',
136             'array' => array('bang'),
137             ));
138
139         $this->assertEmpty($bag->filter('nokey'), '->filter() should return empty by default if no key is found');
140
141         $this->assertEquals('0123', $bag->filter('digits', '', FILTER_SANITIZE_NUMBER_INT), '->filter() gets a value of parameter as integer filtering out invalid characters');
142
143         $this->assertEquals('example@example.com', $bag->filter('email', '', FILTER_VALIDATE_EMAIL), '->filter() gets a value of parameter as email');
144
145         $this->assertEquals('http://example.com/foo', $bag->filter('url', '', FILTER_VALIDATE_URL, array('flags' => FILTER_FLAG_PATH_REQUIRED)), '->filter() gets a value of parameter as URL with a path');
146
147         // This test is repeated for code-coverage
148         $this->assertEquals('http://example.com/foo', $bag->filter('url', '', FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED), '->filter() gets a value of parameter as URL with a path');
149
150         $this->assertFalse($bag->filter('dec', '', FILTER_VALIDATE_INT, array(
151             'flags' => FILTER_FLAG_ALLOW_HEX,
152             'options' => array('min_range' => 1, 'max_range' => 0xff),
153         )), '->filter() gets a value of parameter as integer between boundaries');
154
155         $this->assertFalse($bag->filter('hex', '', FILTER_VALIDATE_INT, array(
156             'flags' => FILTER_FLAG_ALLOW_HEX,
157             'options' => array('min_range' => 1, 'max_range' => 0xff),
158         )), '->filter() gets a value of parameter as integer between boundaries');
159
160         $this->assertEquals(array('bang'), $bag->filter('array', ''), '->filter() gets a value of parameter as an array');
161     }
162
163     public function testGetIterator()
164     {
165         $parameters = array('foo' => 'bar', 'hello' => 'world');
166         $bag = new ParameterBag($parameters);
167
168         $i = 0;
169         foreach ($bag as $key => $val) {
170             ++$i;
171             $this->assertEquals($parameters[$key], $val);
172         }
173
174         $this->assertEquals(\count($parameters), $i);
175     }
176
177     public function testCount()
178     {
179         $parameters = array('foo' => 'bar', 'hello' => 'world');
180         $bag = new ParameterBag($parameters);
181
182         $this->assertCount(\count($parameters), $bag);
183     }
184
185     public function testGetBoolean()
186     {
187         $parameters = array('string_true' => 'true', 'string_false' => 'false');
188         $bag = new ParameterBag($parameters);
189
190         $this->assertTrue($bag->getBoolean('string_true'), '->getBoolean() gets the string true as boolean true');
191         $this->assertFalse($bag->getBoolean('string_false'), '->getBoolean() gets the string false as boolean false');
192         $this->assertFalse($bag->getBoolean('unknown'), '->getBoolean() returns false if a parameter is not defined');
193     }
194 }