2010-06-27 17:28:29 +01:00
< ? php
/*
* This file is part of the Symfony package .
2011-01-15 13:29:43 +00:00
*
2011-03-06 11:40:06 +00:00
* ( c ) Fabien Potencier < fabien @ symfony . com >
2010-06-27 17:28:29 +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\DependencyInjection\Tests\ParameterBag ;
2010-06-27 17:28:29 +01:00
2010-08-20 22:09:55 +01:00
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag ;
2011-05-17 15:26:08 +01:00
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException ;
2011-05-25 08:55:31 +01:00
use Symfony\Component\DependencyInjection\Exception\ParameterCircularReferenceException ;
2011-05-25 08:58:43 +01:00
use Symfony\Component\DependencyInjection\Exception\RuntimeException ;
2010-06-27 17:28:29 +01:00
class ParameterBagTest extends \PHPUnit_Framework_TestCase
{
/**
2010-08-20 22:09:55 +01:00
* @ covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag :: __construct
2010-06-27 17:28:29 +01:00
*/
public function testConstructor ()
{
$bag = new ParameterBag ( $parameters = array (
'foo' => 'foo' ,
'bar' => 'bar' ,
));
$this -> assertEquals ( $parameters , $bag -> all (), '__construct() takes an array of parameters as its first argument' );
}
/**
2010-08-20 22:09:55 +01:00
* @ covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag :: clear
2010-06-27 17:28:29 +01:00
*/
public function testClear ()
{
$bag = new ParameterBag ( $parameters = array (
'foo' => 'foo' ,
'bar' => 'bar' ,
));
$bag -> clear ();
$this -> assertEquals ( array (), $bag -> all (), '->clear() removes all parameters' );
}
2012-05-07 17:13:02 +01:00
/**
* @ covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag :: remove
*/
public function testRemove ()
{
$bag = new ParameterBag ( array (
'foo' => 'foo' ,
'bar' => 'bar' ,
));
$bag -> remove ( 'foo' );
$this -> assertEquals ( array ( 'bar' => 'bar' ), $bag -> all (), '->remove() removes a parameter' );
}
2010-06-27 17:28:29 +01:00
/**
2010-08-20 22:09:55 +01:00
* @ covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag :: get
* @ covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag :: set
2010-06-27 17:28:29 +01:00
*/
public function testGetSet ()
{
$bag = new ParameterBag ( array ( 'foo' => 'bar' ));
$bag -> set ( 'bar' , 'foo' );
$this -> assertEquals ( 'foo' , $bag -> get ( 'bar' ), '->set() sets the value of a new parameter' );
$bag -> set ( 'foo' , 'baz' );
$this -> assertEquals ( 'baz' , $bag -> get ( 'foo' ), '->set() overrides previously set parameter' );
$bag -> set ( 'Foo' , 'baz1' );
$this -> assertEquals ( 'baz1' , $bag -> get ( 'foo' ), '->set() converts the key to lowercase' );
$this -> assertEquals ( 'baz1' , $bag -> get ( 'FOO' ), '->get() converts the key to lowercase' );
try {
$bag -> get ( 'baba' );
$this -> fail ( '->get() throws an \InvalidArgumentException if the key does not exist' );
} catch ( \Exception $e ) {
$this -> assertInstanceOf ( '\InvalidArgumentException' , $e , '->get() throws an \InvalidArgumentException if the key does not exist' );
2011-04-26 11:31:40 +01:00
$this -> assertEquals ( 'You have requested a non-existent parameter "baba".' , $e -> getMessage (), '->get() throws an \InvalidArgumentException if the key does not exist' );
2010-06-27 17:28:29 +01:00
}
}
/**
2010-08-20 22:09:55 +01:00
* @ covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag :: has
2010-06-27 17:28:29 +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 -> assertTrue ( $bag -> has ( 'Foo' ), '->has() converts the key to lowercase' );
$this -> assertFalse ( $bag -> has ( 'bar' ), '->has() returns false if a parameter is not defined' );
}
2010-07-15 19:15:49 +01:00
/**
2010-08-20 22:09:55 +01:00
* @ covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag :: resolveValue
2010-07-15 19:15:49 +01:00
*/
public function testResolveValue ()
{
$bag = new ParameterBag ( array ());
$this -> assertEquals ( 'foo' , $bag -> resolveValue ( 'foo' ), '->resolveValue() returns its argument unmodified if no placeholders are found' );
$bag = new ParameterBag ( array ( 'foo' => 'bar' ));
$this -> assertEquals ( 'I\'m a bar' , $bag -> resolveValue ( 'I\'m a %foo%' ), '->resolveValue() replaces placeholders by their values' );
$this -> assertEquals ( array ( 'bar' => 'bar' ), $bag -> resolveValue ( array ( '%foo%' => '%foo%' )), '->resolveValue() replaces placeholders in keys and values of arrays' );
$this -> assertEquals ( array ( 'bar' => array ( 'bar' => array ( 'bar' => 'bar' ))), $bag -> resolveValue ( array ( '%foo%' => array ( '%foo%' => array ( '%foo%' => '%foo%' )))), '->resolveValue() replaces placeholders in nested arrays' );
2011-05-26 07:26:14 +01:00
$this -> assertEquals ( 'I\'m a %%foo%%' , $bag -> resolveValue ( 'I\'m a %%foo%%' ), '->resolveValue() supports % escaping by doubling it' );
$this -> assertEquals ( 'I\'m a bar %%foo bar' , $bag -> resolveValue ( 'I\'m a %foo% %%foo %foo%' ), '->resolveValue() supports % escaping by doubling it' );
2012-02-02 17:24:41 +00:00
$this -> assertEquals ( array ( 'foo' => array ( 'bar' => array ( 'ding' => 'I\'m a bar %%foo %%bar' ))), $bag -> resolveValue ( array ( 'foo' => array ( 'bar' => array ( 'ding' => 'I\'m a bar %%foo %%bar' )))), '->resolveValue() supports % escaping by doubling it' );
2010-07-15 19:15:49 +01:00
$bag = new ParameterBag ( array ( 'foo' => true ));
2011-07-29 10:10:42 +01:00
$this -> assertTrue ( $bag -> resolveValue ( '%foo%' ), '->resolveValue() replaces arguments that are just a placeholder by their value without casting them to strings' );
2011-05-25 08:06:09 +01:00
$bag = new ParameterBag ( array ( 'foo' => null ));
2011-07-29 10:10:42 +01:00
$this -> assertNull ( $bag -> resolveValue ( '%foo%' ), '->resolveValue() replaces arguments that are just a placeholder by their value without casting them to strings' );
2010-07-15 19:15:49 +01:00
2012-03-06 12:33:50 +00:00
$bag = new ParameterBag ( array ( 'foo' => 'bar' , 'baz' => '%%%foo% %foo%%% %%foo%% %%%foo%%%' ));
$this -> assertEquals ( '%%bar bar%% %%foo%% %%bar%%' , $bag -> resolveValue ( '%baz%' ), '->resolveValue() replaces params placed besides escaped %' );
$bag = new ParameterBag ( array ( 'baz' => '%%s?%%s' ));
$this -> assertEquals ( '%%s?%%s' , $bag -> resolveValue ( '%baz%' ), '->resolveValue() is not replacing greedily' );
2010-07-15 19:15:49 +01:00
$bag = new ParameterBag ( array ());
try {
2011-05-25 08:06:09 +01:00
$bag -> resolveValue ( '%foobar%' );
2011-03-01 17:56:35 +00:00
$this -> fail ( '->resolveValue() throws an InvalidArgumentException if a placeholder references a non-existent parameter' );
2011-05-17 15:26:08 +01:00
} catch ( ParameterNotFoundException $e ) {
$this -> assertEquals ( 'You have requested a non-existent parameter "foobar".' , $e -> getMessage (), '->resolveValue() throws a ParameterNotFoundException if a placeholder references a non-existent parameter' );
2010-07-15 19:15:49 +01:00
}
try {
2011-05-25 08:06:09 +01:00
$bag -> resolveValue ( 'foo %foobar% bar' );
2011-05-17 15:26:08 +01:00
$this -> fail ( '->resolveValue() throws a ParameterNotFoundException if a placeholder references a non-existent parameter' );
} catch ( ParameterNotFoundException $e ) {
$this -> assertEquals ( 'You have requested a non-existent parameter "foobar".' , $e -> getMessage (), '->resolveValue() throws a ParameterNotFoundException if a placeholder references a non-existent parameter' );
2011-04-26 11:31:40 +01:00
}
2011-05-25 08:06:09 +01:00
$bag = new ParameterBag ( array ( 'foo' => 'a %bar%' , 'bar' => array ()));
try {
$bag -> resolveValue ( '%foo%' );
2011-05-25 08:58:43 +01:00
$this -> fail ( '->resolveValue() throws a RuntimeException when a parameter embeds another non-string parameter' );
} catch ( RuntimeException $e ) {
2011-07-16 15:10:11 +01:00
$this -> assertEquals ( 'A string value must be composed of strings and/or numbers, but found parameter "bar" of type array inside string value "a %bar%".' , $e -> getMessage (), '->resolveValue() throws a RuntimeException when a parameter embeds another non-string parameter' );
2011-05-25 08:06:09 +01:00
}
2011-05-25 08:55:31 +01:00
$bag = new ParameterBag ( array ( 'foo' => '%bar%' , 'bar' => '%foobar%' , 'foobar' => '%foo%' ));
try {
$bag -> resolveValue ( '%foo%' );
$this -> fail ( '->resolveValue() throws a ParameterCircularReferenceException when a parameter has a circular reference' );
} catch ( ParameterCircularReferenceException $e ) {
$this -> assertEquals ( 'Circular reference detected for parameter "foo" ("foo" > "bar" > "foobar" > "foo").' , $e -> getMessage (), '->resolveValue() throws a ParameterCircularReferenceException when a parameter has a circular reference' );
}
$bag = new ParameterBag ( array ( 'foo' => 'a %bar%' , 'bar' => 'a %foobar%' , 'foobar' => 'a %foo%' ));
try {
$bag -> resolveValue ( '%foo%' );
$this -> fail ( '->resolveValue() throws a ParameterCircularReferenceException when a parameter has a circular reference' );
} catch ( ParameterCircularReferenceException $e ) {
$this -> assertEquals ( 'Circular reference detected for parameter "foo" ("foo" > "bar" > "foobar" > "foo").' , $e -> getMessage (), '->resolveValue() throws a ParameterCircularReferenceException when a parameter has a circular reference' );
}
2011-07-16 15:10:11 +01:00
$bag = new ParameterBag ( array ( 'host' => 'foo.bar' , 'port' => 1337 ));
$this -> assertEquals ( 'foo.bar:1337' , $bag -> resolveValue ( '%host%:%port%' ));
2011-04-26 11:31:40 +01:00
}
/**
* @ covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag :: resolve
*/
public function testResolveIndicatesWhyAParameterIsNeeded ()
{
$bag = new ParameterBag ( array ( 'foo' => '%bar%' ));
try {
$bag -> resolve ();
2011-05-17 15:26:08 +01:00
} catch ( ParameterNotFoundException $e ) {
2011-04-26 11:31:40 +01:00
$this -> assertEquals ( 'The parameter "foo" has a dependency on a non-existent parameter "bar".' , $e -> getMessage ());
}
$bag = new ParameterBag ( array ( 'foo' => '%bar%' ));
try {
$bag -> resolve ();
2011-05-17 15:26:08 +01:00
} catch ( ParameterNotFoundException $e ) {
2011-04-26 11:31:40 +01:00
$this -> assertEquals ( 'The parameter "foo" has a dependency on a non-existent parameter "bar".' , $e -> getMessage ());
2010-07-15 19:15:49 +01:00
}
}
2011-12-27 20:48:49 +00:00
/**
* @ covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag :: resolve
2012-02-02 17:24:41 +00:00
*/
2012-03-06 12:33:50 +00:00
public function testResolveUnescapesValue ()
2012-02-02 17:24:41 +00:00
{
$bag = new ParameterBag ( array (
'foo' => array ( 'bar' => array ( 'ding' => 'I\'m a bar %%foo %%bar' )),
'bar' => 'I\'m a %%foo%%' ,
));
$bag -> resolve ();
$this -> assertEquals ( 'I\'m a %foo%' , $bag -> get ( 'bar' ), '->resolveValue() supports % escaping by doubling it' );
$this -> assertEquals ( array ( 'bar' => array ( 'ding' => 'I\'m a bar %foo %bar' )), $bag -> get ( 'foo' ), '->resolveValue() supports % escaping by doubling it' );
}
2012-05-30 18:50:31 +01:00
/**
* @ covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag :: escapeValue
*/
public function testEscapeValue ()
{
$bag = new ParameterBag ();
$bag -> add ( array (
'foo' => $bag -> escapeValue ( array ( 'bar' => array ( 'ding' => 'I\'m a bar %foo %bar' , 'zero' => null ))),
'bar' => $bag -> escapeValue ( 'I\'m a %foo%' ),
));
$this -> assertEquals ( 'I\'m a %%foo%%' , $bag -> get ( 'bar' ), '->escapeValue() escapes % by doubling it' );
$this -> assertEquals ( array ( 'bar' => array ( 'ding' => 'I\'m a bar %%foo %%bar' , 'zero' => null )), $bag -> get ( 'foo' ), '->escapeValue() escapes % by doubling it' );
}
2012-02-02 17:24:41 +00:00
/**
* @ covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag :: resolve
2011-12-27 20:48:49 +00:00
* @ dataProvider stringsWithSpacesProvider
*/
public function testResolveStringWithSpacesReturnsString ( $expected , $test , $description )
{
$bag = new ParameterBag ( array ( 'foo' => 'bar' ));
try {
$this -> assertEquals ( $expected , $bag -> resolveString ( $test ), $description );
} catch ( ParameterNotFoundException $e ) {
$this -> fail ( sprintf ( '%s - "%s"' , $description , $expected ));
}
}
public function stringsWithSpacesProvider ()
{
return array (
array ( 'bar' , '%foo%' , 'Parameters must be wrapped by %.' ),
array ( '% foo %' , '% foo %' , 'Parameters should not have spaces.' ),
array ( '{% set my_template = "foo" %}' , '{% set my_template = "foo" %}' , 'Twig-like strings are not parameters.' ),
array ( '50% is less than 100%' , '50% is less than 100%' , 'Text between % signs is allowed, if there are spaces.' ),
);
}
2010-06-27 17:28:29 +01:00
}