Version 1
[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     /**
78      * @group legacy
79      * @dataProvider getInvalidPaths
80      * @expectedException \InvalidArgumentException
81      */
82     public function testGetDeepWithInvalidPaths($path)
83     {
84         $bag = new ParameterBag(array('foo' => array('bar' => 'moo')));
85
86         $bag->get($path, null, true);
87     }
88
89     public function getInvalidPaths()
90     {
91         return array(
92             array('foo[['),
93             array('foo[d'),
94             array('foo[bar]]'),
95             array('foo[bar]d'),
96         );
97     }
98
99     /**
100      * @group legacy
101      */
102     public function testGetDeep()
103     {
104         $bag = new ParameterBag(array('foo' => array('bar' => array('moo' => 'boo'))));
105
106         $this->assertEquals(array('moo' => 'boo'), $bag->get('foo[bar]', null, true));
107         $this->assertEquals('boo', $bag->get('foo[bar][moo]', null, true));
108         $this->assertEquals('default', $bag->get('foo[bar][foo]', 'default', true));
109         $this->assertEquals('default', $bag->get('bar[moo][foo]', 'default', true));
110     }
111
112     public function testSet()
113     {
114         $bag = new ParameterBag(array());
115
116         $bag->set('foo', 'bar');
117         $this->assertEquals('bar', $bag->get('foo'), '->set() sets the value of parameter');
118
119         $bag->set('foo', 'baz');
120         $this->assertEquals('baz', $bag->get('foo'), '->set() overrides previously set parameter');
121     }
122
123     public function testHas()
124     {
125         $bag = new ParameterBag(array('foo' => 'bar'));
126
127         $this->assertTrue($bag->has('foo'), '->has() returns true if a parameter is defined');
128         $this->assertFalse($bag->has('unknown'), '->has() return false if a parameter is not defined');
129     }
130
131     public function testGetAlpha()
132     {
133         $bag = new ParameterBag(array('word' => 'foo_BAR_012'));
134
135         $this->assertEquals('fooBAR', $bag->getAlpha('word'), '->getAlpha() gets only alphabetic characters');
136         $this->assertEquals('', $bag->getAlpha('unknown'), '->getAlpha() returns empty string if a parameter is not defined');
137     }
138
139     public function testGetAlnum()
140     {
141         $bag = new ParameterBag(array('word' => 'foo_BAR_012'));
142
143         $this->assertEquals('fooBAR012', $bag->getAlnum('word'), '->getAlnum() gets only alphanumeric characters');
144         $this->assertEquals('', $bag->getAlnum('unknown'), '->getAlnum() returns empty string if a parameter is not defined');
145     }
146
147     public function testGetDigits()
148     {
149         $bag = new ParameterBag(array('word' => 'foo_BAR_012'));
150
151         $this->assertEquals('012', $bag->getDigits('word'), '->getDigits() gets only digits as string');
152         $this->assertEquals('', $bag->getDigits('unknown'), '->getDigits() returns empty string if a parameter is not defined');
153     }
154
155     public function testGetInt()
156     {
157         $bag = new ParameterBag(array('digits' => '0123'));
158
159         $this->assertEquals(123, $bag->getInt('digits'), '->getInt() gets a value of parameter as integer');
160         $this->assertEquals(0, $bag->getInt('unknown'), '->getInt() returns zero if a parameter is not defined');
161     }
162
163     public function testFilter()
164     {
165         $bag = new ParameterBag(array(
166             'digits' => '0123ab',
167             'email' => 'example@example.com',
168             'url' => 'http://example.com/foo',
169             'dec' => '256',
170             'hex' => '0x100',
171             'array' => array('bang'),
172             ));
173
174         $this->assertEmpty($bag->filter('nokey'), '->filter() should return empty by default if no key is found');
175
176         $this->assertEquals('0123', $bag->filter('digits', '', FILTER_SANITIZE_NUMBER_INT), '->filter() gets a value of parameter as integer filtering out invalid characters');
177
178         $this->assertEquals('example@example.com', $bag->filter('email', '', FILTER_VALIDATE_EMAIL), '->filter() gets a value of parameter as email');
179
180         $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');
181
182         // This test is repeated for code-coverage
183         $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');
184
185         $this->assertFalse($bag->filter('dec', '', FILTER_VALIDATE_INT, array(
186             'flags' => FILTER_FLAG_ALLOW_HEX,
187             'options' => array('min_range' => 1, 'max_range' => 0xff),
188         )), '->filter() gets a value of parameter as integer between boundaries');
189
190         $this->assertFalse($bag->filter('hex', '', FILTER_VALIDATE_INT, array(
191             'flags' => FILTER_FLAG_ALLOW_HEX,
192             'options' => array('min_range' => 1, 'max_range' => 0xff),
193         )), '->filter() gets a value of parameter as integer between boundaries');
194
195         $this->assertEquals(array('bang'), $bag->filter('array', ''), '->filter() gets a value of parameter as an array');
196     }
197
198     public function testGetIterator()
199     {
200         $parameters = array('foo' => 'bar', 'hello' => 'world');
201         $bag = new ParameterBag($parameters);
202
203         $i = 0;
204         foreach ($bag as $key => $val) {
205             ++$i;
206             $this->assertEquals($parameters[$key], $val);
207         }
208
209         $this->assertEquals(count($parameters), $i);
210     }
211
212     public function testCount()
213     {
214         $parameters = array('foo' => 'bar', 'hello' => 'world');
215         $bag = new ParameterBag($parameters);
216
217         $this->assertEquals(count($parameters), count($bag));
218     }
219
220     public function testGetBoolean()
221     {
222         $parameters = array('string_true' => 'true', 'string_false' => 'false');
223         $bag = new ParameterBag($parameters);
224
225         $this->assertTrue($bag->getBoolean('string_true'), '->getBoolean() gets the string true as boolean true');
226         $this->assertFalse($bag->getBoolean('string_false'), '->getBoolean() gets the string false as boolean false');
227         $this->assertFalse($bag->getBoolean('unknown'), '->getBoolean() returns false if a parameter is not defined');
228     }
229 }