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]'));
77 public function testSet()
79 $bag = new ParameterBag(array());
81 $bag->set('foo', 'bar');
82 $this->assertEquals('bar', $bag->get('foo'), '->set() sets the value of parameter');
84 $bag->set('foo', 'baz');
85 $this->assertEquals('baz', $bag->get('foo'), '->set() overrides previously set parameter');
88 public function testHas()
90 $bag = new ParameterBag(array('foo' => 'bar'));
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');
96 public function testGetAlpha()
98 $bag = new ParameterBag(array('word' => 'foo_BAR_012'));
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');
104 public function testGetAlnum()
106 $bag = new ParameterBag(array('word' => 'foo_BAR_012'));
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');
112 public function testGetDigits()
114 $bag = new ParameterBag(array('word' => 'foo_BAR_012'));
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');
120 public function testGetInt()
122 $bag = new ParameterBag(array('digits' => '0123'));
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');
128 public function testFilter()
130 $bag = new ParameterBag(array(
131 'digits' => '0123ab',
132 'email' => 'example@example.com',
133 'url' => 'http://example.com/foo',
136 'array' => array('bang'),
139 $this->assertEmpty($bag->filter('nokey'), '->filter() should return empty by default if no key is found');
141 $this->assertEquals('0123', $bag->filter('digits', '', FILTER_SANITIZE_NUMBER_INT), '->filter() gets a value of parameter as integer filtering out invalid characters');
143 $this->assertEquals('example@example.com', $bag->filter('email', '', FILTER_VALIDATE_EMAIL), '->filter() gets a value of parameter as email');
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');
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');
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');
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');
160 $this->assertEquals(array('bang'), $bag->filter('array', ''), '->filter() gets a value of parameter as an array');
163 public function testGetIterator()
165 $parameters = array('foo' => 'bar', 'hello' => 'world');
166 $bag = new ParameterBag($parameters);
169 foreach ($bag as $key => $val) {
171 $this->assertEquals($parameters[$key], $val);
174 $this->assertEquals(\count($parameters), $i);
177 public function testCount()
179 $parameters = array('foo' => 'bar', 'hello' => 'world');
180 $bag = new ParameterBag($parameters);
182 $this->assertCount(\count($parameters), $bag);
185 public function testGetBoolean()
187 $parameters = array('string_true' => 'true', 'string_false' => 'false');
188 $bag = new ParameterBag($parameters);
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');