Skip to content

Commit 6aec789

Browse files
author
Drak
committed
Added tests.
1 parent 54454ba commit 6aec789

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/Symfony/Component/HttpFoundation/ParameterBag.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ public function getInt($key, $default = 0, $deep = false)
250250
* @param mixed $default Default = null.
251251
* @param boolean $deep Default = false.
252252
* @param integer $filter FILTER_* constant.
253-
* @param array $options Fitler options.
253+
* @param array $options Filter options - can be an array of options or FILTER_* constant.
254254
*
255255
* @return mixed
256256
*/

tests/Symfony/Tests/Component/HttpFoundation/ParameterBagTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,39 @@ public function testGetInt()
162162
$this->assertEquals(123, $bag->getInt('digits'), '->getInt() gets a value of parameter as integer');
163163
$this->assertEquals(0, $bag->getInt('unknown'), '->getInt() returns zero if a parameter is not defined');
164164
}
165+
166+
/**
167+
* @covers Symfony\Component\HttpFoundation\ParameterBag::filter
168+
*/
169+
public function testFilter()
170+
{
171+
$bag = new ParameterBag(array(
172+
'digits' => '0123ab',
173+
'email' => '[email protected]',
174+
'url' => 'http://example.com/',
175+
'dec' => '256',
176+
'hex' => '0x100',
177+
'array' => 'bang',
178+
));
179+
180+
$this->assertEmpty($bag->filter('nokey'), '->filter() should return empty by default if no key is found');
181+
182+
$this->assertEquals('0123', $bag->filter('digits', '', false, FILTER_SANITIZE_NUMBER_INT), '->filter() gets a value of parameter as integer filtering out invalid characters');
183+
184+
$this->assertEquals('[email protected]', $bag->filter('email', '', false, FILTER_VALIDATE_EMAIL), '->filter() gets a value of parameter as email');
185+
186+
$this->assertNotEquals('http://example.com/foo', $bag->filter('url', '', false, FILTER_VALIDATE_URL, array('flags' => FILTER_FLAG_PATH_REQUIRED)), '->filter() gets a value of parameter as url with a path');
187+
188+
$this->assertFalse($bag->filter('dec', '', false, FILTER_VALIDATE_INT, array(
189+
'flags' => FILTER_FLAG_ALLOW_HEX,
190+
'options' => array('min_range' => 1, 'max_range' => 0xff))
191+
), '->filter() gets a value of parameter as integer between boundaries');
192+
193+
$this->assertFalse($bag->filter('hex', '', false, FILTER_VALIDATE_INT, array(
194+
'flags' => FILTER_FLAG_ALLOW_HEX,
195+
'options' => array('min_range' => 1, 'max_range' => 0xff))
196+
), '->filter() gets a value of parameter as integer between boundaries');
197+
198+
$this->assertNotEquals(array('bang'), $bag->filter('array', '', false), '->filter() gets a value of parameter as an array');
199+
}
165200
}

0 commit comments

Comments
 (0)