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 .
*/
2010-08-20 22:09:55 +01:00
namespace Symfony\Tests\Component\HttpFoundation ;
2010-04-20 17:02:57 +01:00
2010-08-20 22:09:55 +01:00
use Symfony\Component\HttpFoundation\ParameterBag ;
2010-04-20 17:02:57 +01:00
2010-05-03 10:40:23 +01:00
class ParameterBagTest extends \PHPUnit_Framework_TestCase
2010-04-20 17:02:57 +01:00
{
2010-05-06 12:25:53 +01:00
/**
2010-08-20 22:09:55 +01:00
* @ covers Symfony\Component\HttpFoundation\ParameterBag :: __construct
2010-05-06 12:25:53 +01:00
*/
public function testConstructor ()
{
$this -> testAll ();
}
/**
2010-08-20 22:09:55 +01:00
* @ covers Symfony\Component\HttpFoundation\ParameterBag :: all
2010-05-06 12:25:53 +01:00
*/
public function testAll ()
{
$bag = new ParameterBag ( array ( 'foo' => 'bar' ));
$this -> assertEquals ( array ( 'foo' => 'bar' ), $bag -> all (), '->all() gets all the input' );
}
/**
2010-08-20 22:09:55 +01:00
* @ covers Symfony\Component\HttpFoundation\ParameterBag :: replace
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' );
}
/**
2010-08-20 22:09:55 +01:00
* @ covers Symfony\Component\HttpFoundation\ParameterBag :: get
2010-05-06 12:25:53 +01:00
*/
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]' ));
}
2011-04-06 07:17:03 +01:00
/**
* @ dataProvider getInvalidPaths
* @ expectedException \InvalidArgumentException
*/
public function testGetDeepWithInvalidPaths ( $path )
{
$bag = new ParameterBag ( array ( 'foo' => array ( 'bar' => 'moo' )));
2011-05-10 10:16:25 +01:00
$bag -> get ( $path , null , true );
2011-04-06 07:17:03 +01:00
}
public function getInvalidPaths ()
{
return array (
array ( 'foo[[' ),
array ( 'foo[d' ),
array ( 'foo[bar]]' ),
array ( 'foo[bar]d' ),
);
}
public function testGetDeep ()
{
$bag = new ParameterBag ( array ( 'foo' => array ( 'bar' => array ( 'moo' => 'boo' ))));
2011-05-10 10:16:25 +01:00
$this -> assertEquals ( array ( 'moo' => 'boo' ), $bag -> get ( 'foo[bar]' , null , true ));
$this -> assertEquals ( 'boo' , $bag -> get ( 'foo[bar][moo]' , null , true ));
$this -> assertEquals ( 'default' , $bag -> get ( 'foo[bar][foo]' , 'default' , true ));
$this -> assertEquals ( 'default' , $bag -> get ( 'bar[moo][foo]' , 'default' , true ));
2011-04-06 07:17:03 +01:00
}
2010-05-06 12:25:53 +01:00
/**
2010-08-20 22:09:55 +01:00
* @ covers Symfony\Component\HttpFoundation\ParameterBag :: set
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' );
}
/**
2010-08-20 22:09:55 +01:00
* @ covers Symfony\Component\HttpFoundation\ParameterBag :: has
2010-05-06 12:25:53 +01:00
*/
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' );
}
/**
2010-08-20 22:09:55 +01:00
* @ covers Symfony\Component\HttpFoundation\ParameterBag :: getAlpha
2010-05-06 12:25:53 +01:00
*/
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' );
}
/**
2010-08-20 22:09:55 +01:00
* @ covers Symfony\Component\HttpFoundation\ParameterBag :: getAlnum
2010-05-06 12:25:53 +01:00
*/
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' );
}
/**
2010-08-20 22:09:55 +01:00
* @ covers Symfony\Component\HttpFoundation\ParameterBag :: getDigits
2010-05-06 12:25:53 +01:00
*/
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' );
}
/**
2010-08-20 22:09:55 +01:00
* @ covers Symfony\Component\HttpFoundation\ParameterBag :: getInt
2010-05-06 12:25:53 +01:00
*/
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
/**
* @ covers Symfony\Component\HttpFoundation\ParameterBag :: filter
*/
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
2011-09-27 10:35:51 +01:00
$this -> assertEquals ( '0123' , $bag -> filter ( 'digits' , '' , false , FILTER_SANITIZE_NUMBER_INT ), '->filter() gets a value of parameter as integer filtering out invalid characters' );
2011-10-29 11:05:45 +01:00
2011-09-27 10:35:51 +01:00
$this -> assertEquals ( 'example@example.com' , $bag -> filter ( 'email' , '' , false , FILTER_VALIDATE_EMAIL ), '->filter() gets a value of parameter as email' );
2011-10-29 11:05:45 +01:00
2011-09-27 15:29:32 +01:00
$this -> assertEquals ( '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' );
2011-10-29 11:05:45 +01:00
2011-09-27 15:29:32 +01:00
// This test is repeated for code-coverage
$this -> assertEquals ( 'http://example.com/foo' , $bag -> filter ( 'url' , '' , false , 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
2011-09-27 10:35:51 +01:00
$this -> assertFalse ( $bag -> filter ( 'dec' , '' , false , FILTER_VALIDATE_INT , array (
'flags' => FILTER_FLAG_ALLOW_HEX ,
'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
2011-09-27 10:35:51 +01:00
$this -> assertFalse ( $bag -> filter ( 'hex' , '' , false , FILTER_VALIDATE_INT , array (
'flags' => FILTER_FLAG_ALLOW_HEX ,
'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
2011-09-27 15:29:32 +01:00
$this -> assertEquals ( array ( 'bang' ), $bag -> filter ( 'array' , '' , false ), '->filter() gets a value of parameter as an array' );
2011-10-29 11:05:45 +01:00
2011-09-27 10:35:51 +01:00
}
2011-06-08 18:56:59 +01:00
}