4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpFoundation\Tests;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\ParameterBag;
17 class ParameterBagTest extends TestCase
19 public function testConstructor()
24 public function testAll()
26 $bag = new ParameterBag(array('foo' => 'bar'));
27 $this->assertEquals(array('foo' => 'bar'), $bag->all(), '->all() gets all the input');
30 public function testKeys()
32 $bag = new ParameterBag(array('foo' => 'bar'));
33 $this->assertEquals(array('foo'), $bag->keys());
36 public function testAdd()
38 $bag = new ParameterBag(array('foo' => 'bar'));
39 $bag->add(array('bar' => 'bas'));
40 $this->assertEquals(array('foo' => 'bar', 'bar' => 'bas'), $bag->all());
43 public function testRemove()
45 $bag = new ParameterBag(array('foo' => 'bar'));
46 $bag->add(array('bar' => 'bas'));
47 $this->assertEquals(array('foo' => 'bar', 'bar' => 'bas'), $bag->all());
49 $this->assertEquals(array('foo' => 'bar'), $bag->all());
52 public function testReplace()
54 $bag = new ParameterBag(array('foo' => 'bar'));
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');
61 public function testGet()
63 $bag = new ParameterBag(array('foo' => 'bar', 'null' => null));
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');
70 public function testGetDoesNotUseDeepByDefault()
72 $bag = new ParameterBag(array('foo' => array('bar' => 'moo')));
74 $this->assertNull($bag->get('foo[bar]'));
79 * @dataProvider getInvalidPaths
80 * @expectedException \InvalidArgumentException
82 public function testGetDeepWithInvalidPaths($path)
84 $bag = new ParameterBag(array('foo' => array('bar' => 'moo')));
86 $bag->get($path, null, true);
89 public function getInvalidPaths()
102 public function testGetDeep()
104 $bag = new ParameterBag(array('foo' => array('bar' => array('moo' => 'boo'))));
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));
112 public function testSet()
114 $bag = new ParameterBag(array());
116 $bag->set('foo', 'bar');
117 $this->assertEquals('bar', $bag->get('foo'), '->set() sets the value of parameter');
119 $bag->set('foo', 'baz');
120 $this->assertEquals('baz', $bag->get('foo'), '->set() overrides previously set parameter');
123 public function testHas()
125 $bag = new ParameterBag(array('foo' => 'bar'));
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');
131 public function testGetAlpha()
133 $bag = new ParameterBag(array('word' => 'foo_BAR_012'));
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');
139 public function testGetAlnum()
141 $bag = new ParameterBag(array('word' => 'foo_BAR_012'));
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');
147 public function testGetDigits()
149 $bag = new ParameterBag(array('word' => 'foo_BAR_012'));
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');
155 public function testGetInt()
157 $bag = new ParameterBag(array('digits' => '0123'));
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');
163 public function testFilter()
165 $bag = new ParameterBag(array(
166 'digits' => '0123ab',
167 'email' => 'example@example.com',
168 'url' => 'http://example.com/foo',
171 'array' => array('bang'),
174 $this->assertEmpty($bag->filter('nokey'), '->filter() should return empty by default if no key is found');
176 $this->assertEquals('0123', $bag->filter('digits', '', FILTER_SANITIZE_NUMBER_INT), '->filter() gets a value of parameter as integer filtering out invalid characters');
178 $this->assertEquals('example@example.com', $bag->filter('email', '', FILTER_VALIDATE_EMAIL), '->filter() gets a value of parameter as email');
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');
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');
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');
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');
195 $this->assertEquals(array('bang'), $bag->filter('array', ''), '->filter() gets a value of parameter as an array');
198 public function testGetIterator()
200 $parameters = array('foo' => 'bar', 'hello' => 'world');
201 $bag = new ParameterBag($parameters);
204 foreach ($bag as $key => $val) {
206 $this->assertEquals($parameters[$key], $val);
209 $this->assertEquals(count($parameters), $i);
212 public function testCount()
214 $parameters = array('foo' => 'bar', 'hello' => 'world');
215 $bag = new ParameterBag($parameters);
217 $this->assertEquals(count($parameters), count($bag));
220 public function testGetBoolean()
222 $parameters = array('string_true' => 'true', 'string_false' => 'false');
223 $bag = new ParameterBag($parameters);
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');