4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Tests\ParameterBag;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
16 use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
17 use Symfony\Component\DependencyInjection\Exception\ParameterCircularReferenceException;
18 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
20 class ParameterBagTest extends TestCase
22 public function testConstructor()
24 $bag = new ParameterBag($parameters = array(
28 $this->assertEquals($parameters, $bag->all(), '__construct() takes an array of parameters as its first argument');
31 public function testClear()
33 $bag = new ParameterBag($parameters = array(
38 $this->assertEquals(array(), $bag->all(), '->clear() removes all parameters');
41 public function testRemove()
43 $bag = new ParameterBag(array(
48 $this->assertEquals(array('bar' => 'bar'), $bag->all(), '->remove() removes a parameter');
51 public function testGetSet()
53 $bag = new ParameterBag(array('foo' => 'bar'));
54 $bag->set('bar', 'foo');
55 $this->assertEquals('foo', $bag->get('bar'), '->set() sets the value of a new parameter');
57 $bag->set('foo', 'baz');
58 $this->assertEquals('baz', $bag->get('foo'), '->set() overrides previously set parameter');
62 $this->fail('->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException if the key does not exist');
63 } catch (\Exception $e) {
64 $this->assertInstanceOf('Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException', $e, '->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException if the key does not exist');
65 $this->assertEquals('You have requested a non-existent parameter "baba".', $e->getMessage(), '->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException if the key does not exist');
70 * @dataProvider provideGetThrowParameterNotFoundExceptionData
72 public function testGetThrowParameterNotFoundException($parameterKey, $exceptionMessage)
74 $bag = new ParameterBag(array(
78 'fiz' => array('bar' => array('boo' => 12)),
81 if (method_exists($this, 'expectException')) {
82 $this->expectException(ParameterNotFoundException::class);
83 $this->expectExceptionMessage($exceptionMessage);
85 $this->setExpectedException(ParameterNotFoundException::class, $exceptionMessage);
88 $bag->get($parameterKey);
91 public function provideGetThrowParameterNotFoundExceptionData()
94 array('foo1', 'You have requested a non-existent parameter "foo1". Did you mean this: "foo"?'),
95 array('bag', 'You have requested a non-existent parameter "bag". Did you mean one of these: "bar", "baz"?'),
96 array('', 'You have requested a non-existent parameter "".'),
98 array('fiz.bar.boo', 'You have requested a non-existent parameter "fiz.bar.boo". You cannot access nested array items, do you want to inject "fiz" instead?'),
102 public function testHas()
104 $bag = new ParameterBag(array('foo' => 'bar'));
105 $this->assertTrue($bag->has('foo'), '->has() returns true if a parameter is defined');
106 $this->assertFalse($bag->has('bar'), '->has() returns false if a parameter is not defined');
111 * @expectedDeprecation Parameter names will be made case sensitive in Symfony 4.0. Using "BAR" instead of "bar" is deprecated since Symfony 3.4.
112 * @expectedDeprecation Parameter names will be made case sensitive in Symfony 4.0. Using "Foo" instead of "foo" is deprecated since Symfony 3.4.
113 * @expectedDeprecation Parameter names will be made case sensitive in Symfony 4.0. Using "FOO" instead of "foo" is deprecated since Symfony 3.4.
114 * @expectedDeprecation Parameter names will be made case sensitive in Symfony 4.0. Using "Foo" instead of "foo" is deprecated since Symfony 3.4.
116 public function testMixedCase()
118 $bag = new ParameterBag(array(
124 $this->assertEquals(array('foo' => 'foo'), $bag->all(), '->remove() converts key to lowercase before removing');
126 $bag->set('Foo', 'baz1');
127 $this->assertEquals('baz1', $bag->get('foo'), '->set() converts the key to lowercase');
128 $this->assertEquals('baz1', $bag->get('FOO'), '->get() converts the key to lowercase');
130 $this->assertTrue($bag->has('Foo'), '->has() converts the key to lowercase');
133 public function testResolveValue()
135 $bag = new ParameterBag(array());
136 $this->assertEquals('foo', $bag->resolveValue('foo'), '->resolveValue() returns its argument unmodified if no placeholders are found');
138 $bag = new ParameterBag(array('foo' => 'bar'));
139 $this->assertEquals('I\'m a bar', $bag->resolveValue('I\'m a %foo%'), '->resolveValue() replaces placeholders by their values');
140 $this->assertEquals(array('bar' => 'bar'), $bag->resolveValue(array('%foo%' => '%foo%')), '->resolveValue() replaces placeholders in keys and values of arrays');
141 $this->assertEquals(array('bar' => array('bar' => array('bar' => 'bar'))), $bag->resolveValue(array('%foo%' => array('%foo%' => array('%foo%' => '%foo%')))), '->resolveValue() replaces placeholders in nested arrays');
142 $this->assertEquals('I\'m a %%foo%%', $bag->resolveValue('I\'m a %%foo%%'), '->resolveValue() supports % escaping by doubling it');
143 $this->assertEquals('I\'m a bar %%foo bar', $bag->resolveValue('I\'m a %foo% %%foo %foo%'), '->resolveValue() supports % escaping by doubling it');
144 $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');
146 $bag = new ParameterBag(array('foo' => true));
147 $this->assertTrue($bag->resolveValue('%foo%'), '->resolveValue() replaces arguments that are just a placeholder by their value without casting them to strings');
148 $bag = new ParameterBag(array('foo' => null));
149 $this->assertNull($bag->resolveValue('%foo%'), '->resolveValue() replaces arguments that are just a placeholder by their value without casting them to strings');
151 $bag = new ParameterBag(array('foo' => 'bar', 'baz' => '%%%foo% %foo%%% %%foo%% %%%foo%%%'));
152 $this->assertEquals('%%bar bar%% %%foo%% %%bar%%', $bag->resolveValue('%baz%'), '->resolveValue() replaces params placed besides escaped %');
154 $bag = new ParameterBag(array('baz' => '%%s?%%s'));
155 $this->assertEquals('%%s?%%s', $bag->resolveValue('%baz%'), '->resolveValue() is not replacing greedily');
157 $bag = new ParameterBag(array());
159 $bag->resolveValue('%foobar%');
160 $this->fail('->resolveValue() throws an InvalidArgumentException if a placeholder references a non-existent parameter');
161 } catch (ParameterNotFoundException $e) {
162 $this->assertEquals('You have requested a non-existent parameter "foobar".', $e->getMessage(), '->resolveValue() throws a ParameterNotFoundException if a placeholder references a non-existent parameter');
166 $bag->resolveValue('foo %foobar% bar');
167 $this->fail('->resolveValue() throws a ParameterNotFoundException if a placeholder references a non-existent parameter');
168 } catch (ParameterNotFoundException $e) {
169 $this->assertEquals('You have requested a non-existent parameter "foobar".', $e->getMessage(), '->resolveValue() throws a ParameterNotFoundException if a placeholder references a non-existent parameter');
172 $bag = new ParameterBag(array('foo' => 'a %bar%', 'bar' => array()));
174 $bag->resolveValue('%foo%');
175 $this->fail('->resolveValue() throws a RuntimeException when a parameter embeds another non-string parameter');
176 } catch (RuntimeException $e) {
177 $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');
180 $bag = new ParameterBag(array('foo' => '%bar%', 'bar' => '%foobar%', 'foobar' => '%foo%'));
182 $bag->resolveValue('%foo%');
183 $this->fail('->resolveValue() throws a ParameterCircularReferenceException when a parameter has a circular reference');
184 } catch (ParameterCircularReferenceException $e) {
185 $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');
188 $bag = new ParameterBag(array('foo' => 'a %bar%', 'bar' => 'a %foobar%', 'foobar' => 'a %foo%'));
190 $bag->resolveValue('%foo%');
191 $this->fail('->resolveValue() throws a ParameterCircularReferenceException when a parameter has a circular reference');
192 } catch (ParameterCircularReferenceException $e) {
193 $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');
196 $bag = new ParameterBag(array('host' => 'foo.bar', 'port' => 1337));
197 $this->assertEquals('foo.bar:1337', $bag->resolveValue('%host%:%port%'));
200 public function testResolveIndicatesWhyAParameterIsNeeded()
202 $bag = new ParameterBag(array('foo' => '%bar%'));
206 } catch (ParameterNotFoundException $e) {
207 $this->assertEquals('The parameter "foo" has a dependency on a non-existent parameter "bar".', $e->getMessage());
210 $bag = new ParameterBag(array('foo' => '%bar%'));
214 } catch (ParameterNotFoundException $e) {
215 $this->assertEquals('The parameter "foo" has a dependency on a non-existent parameter "bar".', $e->getMessage());
219 public function testResolveUnescapesValue()
221 $bag = new ParameterBag(array(
222 'foo' => array('bar' => array('ding' => 'I\'m a bar %%foo %%bar')),
223 'bar' => 'I\'m a %%foo%%',
228 $this->assertEquals('I\'m a %foo%', $bag->get('bar'), '->resolveValue() supports % escaping by doubling it');
229 $this->assertEquals(array('bar' => array('ding' => 'I\'m a bar %foo %bar')), $bag->get('foo'), '->resolveValue() supports % escaping by doubling it');
232 public function testEscapeValue()
234 $bag = new ParameterBag();
237 'foo' => $bag->escapeValue(array('bar' => array('ding' => 'I\'m a bar %foo %bar', 'zero' => null))),
238 'bar' => $bag->escapeValue('I\'m a %foo%'),
241 $this->assertEquals('I\'m a %%foo%%', $bag->get('bar'), '->escapeValue() escapes % by doubling it');
242 $this->assertEquals(array('bar' => array('ding' => 'I\'m a bar %%foo %%bar', 'zero' => null)), $bag->get('foo'), '->escapeValue() escapes % by doubling it');
246 * @dataProvider stringsWithSpacesProvider
248 public function testResolveStringWithSpacesReturnsString($expected, $test, $description)
250 $bag = new ParameterBag(array('foo' => 'bar'));
253 $this->assertEquals($expected, $bag->resolveString($test), $description);
254 } catch (ParameterNotFoundException $e) {
255 $this->fail(sprintf('%s - "%s"', $description, $expected));
259 public function stringsWithSpacesProvider()
262 array('bar', '%foo%', 'Parameters must be wrapped by %.'),
263 array('% foo %', '% foo %', 'Parameters should not have spaces.'),
264 array('{% set my_template = "foo" %}', '{% set my_template = "foo" %}', 'Twig-like strings are not parameters.'),
265 array('50% is less than 100%', '50% is less than 100%', 'Text between % signs is allowed, if there are spaces.'),