This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/tests/Symfony/Tests/Component/DependencyInjection/ParameterBag/ParameterBagTest.php

165 lines
8.2 KiB
PHP
Raw Normal View History

2010-06-27 17:28:29 +01:00
<?php
/*
* This file is part of the Symfony package.
*
* (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.
*/
namespace Symfony\Tests\Component\DependencyInjection\ParameterBag;
2010-06-27 17:28:29 +01:00
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
use Symfony\Component\DependencyInjection\Exception\ParameterCircularReferenceException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
2010-06-27 17:28:29 +01:00
class ParameterBagTest extends \PHPUnit_Framework_TestCase
{
/**
* @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');
}
/**
* @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');
}
/**
* @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');
$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
}
}
/**
* @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');
}
/**
* @covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag::resolveValue
*/
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');
$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');
$bag = new ParameterBag(array('foo' => true));
$this->assertSame(true, $bag->resolveValue('%foo%'), '->resolveValue() replaces arguments that are just a placeholder by their value without casting them to strings');
$bag = new ParameterBag(array('foo' => null));
$this->assertSame(null, $bag->resolveValue('%foo%'), '->resolveValue() replaces arguments that are just a placeholder by their value without casting them to strings');
$bag = new ParameterBag(array());
try {
$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');
} 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');
}
try {
$bag->resolveValue('foo %foobar% bar');
$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');
}
$bag = new ParameterBag(array('foo' => 'a %bar%', 'bar' => array()));
try {
$bag->resolveValue('%foo%');
$this->fail('->resolveValue() throws a RuntimeException when a parameter embeds another non-string parameter');
} catch (RuntimeException $e) {
$this->assertEquals('A parameter cannot contain a non-string parameter.', $e->getMessage(), '->resolveValue() throws a RuntimeException when a parameter embeds another non-string parameter');
}
$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');
}
}
/**
* @covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag::resolve
*/
public function testResolveIndicatesWhyAParameterIsNeeded()
{
$bag = new ParameterBag(array('foo' => '%bar%'));
try {
$bag->resolve();
} catch (ParameterNotFoundException $e) {
$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();
} catch (ParameterNotFoundException $e) {
$this->assertEquals('The parameter "foo" has a dependency on a non-existent parameter "bar".', $e->getMessage());
}
}
2010-06-27 17:28:29 +01:00
}