2010-04-20 17:02:57 +01:00
< ? php
/*
2010-04-25 16:06:54 +01:00
* This file is part of the Symfony package .
2010-04-20 17:02:57 +01:00
*
2011-03-06 11:40:06 +00:00
* ( c ) Fabien Potencier < fabien @ symfony . com >
2010-04-20 17:02:57 +01:00
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
2012-03-28 14:43:52 +01:00
namespace Symfony\Component\HttpFoundation\Tests ;
2010-04-20 17:02:57 +01:00
2017-02-08 07:24:27 +00:00
use PHPUnit\Framework\TestCase ;
2010-08-20 22:09:55 +01:00
use Symfony\Component\HttpFoundation\ParameterBag ;
2010-04-20 17:02:57 +01:00
2017-02-08 07:24:27 +00:00
class ParameterBagTest extends TestCase
2010-04-20 17:02:57 +01:00
{
2010-05-06 12:25:53 +01:00
public function testConstructor ()
{
$this -> testAll ();
}
public function testAll ()
{
$bag = new ParameterBag ( array ( 'foo' => 'bar' ));
$this -> assertEquals ( array ( 'foo' => 'bar' ), $bag -> all (), '->all() gets all the input' );
}
2012-09-11 15:34:01 +01:00
public function testKeys ()
{
$bag = new ParameterBag ( array ( 'foo' => 'bar' ));
$this -> assertEquals ( array ( 'foo' ), $bag -> keys ());
}
public function testAdd ()
{
$bag = new ParameterBag ( array ( 'foo' => 'bar' ));
$bag -> add ( array ( 'bar' => 'bas' ));
$this -> assertEquals ( array ( 'foo' => 'bar' , 'bar' => 'bas' ), $bag -> all ());
}
public function testRemove ()
{
$bag = new ParameterBag ( array ( 'foo' => 'bar' ));
$bag -> add ( array ( 'bar' => 'bas' ));
$this -> assertEquals ( array ( 'foo' => 'bar' , 'bar' => 'bas' ), $bag -> all ());
$bag -> remove ( 'bar' );
$this -> assertEquals ( array ( 'foo' => 'bar' ), $bag -> all ());
}
2010-05-06 12:25:53 +01:00
public function testReplace ()
{
$bag = new ParameterBag ( array ( 'foo' => 'bar' ));
$bag -> replace ( array ( 'FOO' => 'BAR' ));
$this -> assertEquals ( array ( 'FOO' => 'BAR' ), $bag -> all (), '->replace() replaces the input with the argument' );
$this -> assertFalse ( $bag -> has ( 'foo' ), '->replace() overrides previously set the input' );
}
public function testGet ()
{
$bag = new ParameterBag ( array ( 'foo' => 'bar' , 'null' => null ));
$this -> assertEquals ( 'bar' , $bag -> get ( 'foo' ), '->get() gets the value of a parameter' );
$this -> assertEquals ( 'default' , $bag -> get ( 'unknown' , 'default' ), '->get() returns second argument as default if a parameter is not defined' );
2011-03-01 17:56:35 +00:00
$this -> assertNull ( $bag -> get ( 'null' , 'default' ), '->get() returns null if null is set' );
2010-05-06 12:25:53 +01:00
}
2011-05-10 10:16:25 +01:00
public function testGetDoesNotUseDeepByDefault ()
{
$bag = new ParameterBag ( array ( 'foo' => array ( 'bar' => 'moo' )));
$this -> assertNull ( $bag -> get ( 'foo[bar]' ));
}
2010-05-06 12:25:53 +01:00
public function testSet ()
{
$bag = new ParameterBag ( array ());
$bag -> set ( 'foo' , 'bar' );
$this -> assertEquals ( 'bar' , $bag -> get ( 'foo' ), '->set() sets the value of parameter' );
$bag -> set ( 'foo' , 'baz' );
$this -> assertEquals ( 'baz' , $bag -> get ( 'foo' ), '->set() overrides previously set parameter' );
}
public function testHas ()
{
$bag = new ParameterBag ( array ( 'foo' => 'bar' ));
$this -> assertTrue ( $bag -> has ( 'foo' ), '->has() returns true if a parameter is defined' );
$this -> assertFalse ( $bag -> has ( 'unknown' ), '->has() return false if a parameter is not defined' );
}
public function testGetAlpha ()
{
$bag = new ParameterBag ( array ( 'word' => 'foo_BAR_012' ));
$this -> assertEquals ( 'fooBAR' , $bag -> getAlpha ( 'word' ), '->getAlpha() gets only alphabetic characters' );
$this -> assertEquals ( '' , $bag -> getAlpha ( 'unknown' ), '->getAlpha() returns empty string if a parameter is not defined' );
}
public function testGetAlnum ()
{
$bag = new ParameterBag ( array ( 'word' => 'foo_BAR_012' ));
$this -> assertEquals ( 'fooBAR012' , $bag -> getAlnum ( 'word' ), '->getAlnum() gets only alphanumeric characters' );
$this -> assertEquals ( '' , $bag -> getAlnum ( 'unknown' ), '->getAlnum() returns empty string if a parameter is not defined' );
}
public function testGetDigits ()
{
$bag = new ParameterBag ( array ( 'word' => 'foo_BAR_012' ));
$this -> assertEquals ( '012' , $bag -> getDigits ( 'word' ), '->getDigits() gets only digits as string' );
$this -> assertEquals ( '' , $bag -> getDigits ( 'unknown' ), '->getDigits() returns empty string if a parameter is not defined' );
}
public function testGetInt ()
{
$bag = new ParameterBag ( array ( 'digits' => '0123' ));
$this -> assertEquals ( 123 , $bag -> getInt ( 'digits' ), '->getInt() gets a value of parameter as integer' );
$this -> assertEquals ( 0 , $bag -> getInt ( 'unknown' ), '->getInt() returns zero if a parameter is not defined' );
}
2011-10-29 11:05:45 +01:00
2011-09-27 10:35:51 +01:00
public function testFilter ()
{
$bag = new ParameterBag ( array (
2011-10-29 11:05:45 +01:00
'digits' => '0123ab' ,
'email' => 'example@example.com' ,
2011-09-27 15:29:32 +01:00
'url' => 'http://example.com/foo' ,
2011-09-27 10:35:51 +01:00
'dec' => '256' ,
'hex' => '0x100' ,
2011-09-27 15:29:32 +01:00
'array' => array ( 'bang' ),
2011-09-27 10:35:51 +01:00
));
$this -> assertEmpty ( $bag -> filter ( 'nokey' ), '->filter() should return empty by default if no key is found' );
2011-10-29 11:05:45 +01:00
2015-11-27 10:11:22 +00:00
$this -> assertEquals ( '0123' , $bag -> filter ( 'digits' , '' , FILTER_SANITIZE_NUMBER_INT ), '->filter() gets a value of parameter as integer filtering out invalid characters' );
2011-10-29 11:05:45 +01:00
2015-11-27 10:11:22 +00:00
$this -> assertEquals ( 'example@example.com' , $bag -> filter ( 'email' , '' , FILTER_VALIDATE_EMAIL ), '->filter() gets a value of parameter as email' );
2011-10-29 11:05:45 +01:00
2015-11-27 10:11:22 +00:00
$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' );
2011-10-29 11:05:45 +01:00
2011-09-27 15:29:32 +01:00
// This test is repeated for code-coverage
2015-11-27 10:11:22 +00:00
$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' );
2011-10-29 11:05:45 +01:00
2015-11-27 10:11:22 +00:00
$this -> assertFalse ( $bag -> filter ( 'dec' , '' , FILTER_VALIDATE_INT , array (
2014-10-22 19:27:13 +01:00
'flags' => FILTER_FLAG_ALLOW_HEX ,
2015-03-24 21:20:39 +00:00
'options' => array ( 'min_range' => 1 , 'max_range' => 0xff ),
)), '->filter() gets a value of parameter as integer between boundaries' );
2011-10-29 11:05:45 +01:00
2015-11-27 10:11:22 +00:00
$this -> assertFalse ( $bag -> filter ( 'hex' , '' , FILTER_VALIDATE_INT , array (
2014-10-22 19:27:13 +01:00
'flags' => FILTER_FLAG_ALLOW_HEX ,
2015-03-24 21:20:39 +00:00
'options' => array ( 'min_range' => 1 , 'max_range' => 0xff ),
)), '->filter() gets a value of parameter as integer between boundaries' );
2011-10-29 11:05:45 +01:00
2015-11-27 10:11:22 +00:00
$this -> assertEquals ( array ( 'bang' ), $bag -> filter ( 'array' , '' ), '->filter() gets a value of parameter as an array' );
2011-09-27 10:35:51 +01:00
}
2012-03-06 15:07:49 +00:00
public function testGetIterator ()
{
$parameters = array ( 'foo' => 'bar' , 'hello' => 'world' );
$bag = new ParameterBag ( $parameters );
$i = 0 ;
foreach ( $bag as $key => $val ) {
2015-03-31 00:07:44 +01:00
++ $i ;
2012-03-06 15:07:49 +00:00
$this -> assertEquals ( $parameters [ $key ], $val );
}
$this -> assertEquals ( count ( $parameters ), $i );
}
public function testCount ()
{
$parameters = array ( 'foo' => 'bar' , 'hello' => 'world' );
$bag = new ParameterBag ( $parameters );
$this -> assertEquals ( count ( $parameters ), count ( $bag ));
}
2014-06-10 03:24:55 +01:00
public function testGetBoolean ()
{
$parameters = array ( 'string_true' => 'true' , 'string_false' => 'false' );
$bag = new ParameterBag ( $parameters );
$this -> assertTrue ( $bag -> getBoolean ( 'string_true' ), '->getBoolean() gets the string true as boolean true' );
$this -> assertFalse ( $bag -> getBoolean ( 'string_false' ), '->getBoolean() gets the string false as boolean false' );
$this -> assertFalse ( $bag -> getBoolean ( 'unknown' ), '->getBoolean() returns false if a parameter is not defined' );
}
2011-06-08 18:56:59 +01:00
}