c139f0a1344739dead3d68c7a852b60b3b83e88d
[yaffs-website] / vendor / symfony / dependency-injection / Tests / ParameterBag / ParameterBagTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\DependencyInjection\Tests\ParameterBag;
13
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;
19
20 class ParameterBagTest extends TestCase
21 {
22     public function testConstructor()
23     {
24         $bag = new ParameterBag($parameters = array(
25             'foo' => 'foo',
26             'bar' => 'bar',
27         ));
28         $this->assertEquals($parameters, $bag->all(), '__construct() takes an array of parameters as its first argument');
29     }
30
31     public function testClear()
32     {
33         $bag = new ParameterBag($parameters = array(
34             'foo' => 'foo',
35             'bar' => 'bar',
36         ));
37         $bag->clear();
38         $this->assertEquals(array(), $bag->all(), '->clear() removes all parameters');
39     }
40
41     public function testRemove()
42     {
43         $bag = new ParameterBag(array(
44             'foo' => 'foo',
45             'bar' => 'bar',
46         ));
47         $bag->remove('foo');
48         $this->assertEquals(array('bar' => 'bar'), $bag->all(), '->remove() removes a parameter');
49     }
50
51     public function testGetSet()
52     {
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');
56
57         $bag->set('foo', 'baz');
58         $this->assertEquals('baz', $bag->get('foo'), '->set() overrides previously set parameter');
59
60         try {
61             $bag->get('baba');
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');
66         }
67     }
68
69     /**
70      * @dataProvider provideGetThrowParameterNotFoundExceptionData
71      */
72     public function testGetThrowParameterNotFoundException($parameterKey, $exceptionMessage)
73     {
74         $bag = new ParameterBag(array(
75             'foo' => 'foo',
76             'bar' => 'bar',
77             'baz' => 'baz',
78             'fiz' => array('bar' => array('boo' => 12)),
79         ));
80
81         if (method_exists($this, 'expectException')) {
82             $this->expectException(ParameterNotFoundException::class);
83             $this->expectExceptionMessage($exceptionMessage);
84         } else {
85             $this->setExpectedException(ParameterNotFoundException::class, $exceptionMessage);
86         }
87
88         $bag->get($parameterKey);
89     }
90
91     public function provideGetThrowParameterNotFoundExceptionData()
92     {
93         return array(
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 "".'),
97
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?'),
99         );
100     }
101
102     public function testHas()
103     {
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');
107     }
108
109     /**
110      * @group legacy
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.
115      */
116     public function testMixedCase()
117     {
118         $bag = new ParameterBag(array(
119             'foo' => 'foo',
120             'bar' => 'bar',
121         ));
122
123         $bag->remove('BAR');
124         $this->assertEquals(array('foo' => 'foo'), $bag->all(), '->remove() converts key to lowercase before removing');
125
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');
129
130         $this->assertTrue($bag->has('Foo'), '->has() converts the key to lowercase');
131     }
132
133     public function testResolveValue()
134     {
135         $bag = new ParameterBag(array());
136         $this->assertEquals('foo', $bag->resolveValue('foo'), '->resolveValue() returns its argument unmodified if no placeholders are found');
137
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');
145
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');
150
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 %');
153
154         $bag = new ParameterBag(array('baz' => '%%s?%%s'));
155         $this->assertEquals('%%s?%%s', $bag->resolveValue('%baz%'), '->resolveValue() is not replacing greedily');
156
157         $bag = new ParameterBag(array());
158         try {
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');
163         }
164
165         try {
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');
170         }
171
172         $bag = new ParameterBag(array('foo' => 'a %bar%', 'bar' => array()));
173         try {
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');
178         }
179
180         $bag = new ParameterBag(array('foo' => '%bar%', 'bar' => '%foobar%', 'foobar' => '%foo%'));
181         try {
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');
186         }
187
188         $bag = new ParameterBag(array('foo' => 'a %bar%', 'bar' => 'a %foobar%', 'foobar' => 'a %foo%'));
189         try {
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');
194         }
195
196         $bag = new ParameterBag(array('host' => 'foo.bar', 'port' => 1337));
197         $this->assertEquals('foo.bar:1337', $bag->resolveValue('%host%:%port%'));
198     }
199
200     public function testResolveIndicatesWhyAParameterIsNeeded()
201     {
202         $bag = new ParameterBag(array('foo' => '%bar%'));
203
204         try {
205             $bag->resolve();
206         } catch (ParameterNotFoundException $e) {
207             $this->assertEquals('The parameter "foo" has a dependency on a non-existent parameter "bar".', $e->getMessage());
208         }
209
210         $bag = new ParameterBag(array('foo' => '%bar%'));
211
212         try {
213             $bag->resolve();
214         } catch (ParameterNotFoundException $e) {
215             $this->assertEquals('The parameter "foo" has a dependency on a non-existent parameter "bar".', $e->getMessage());
216         }
217     }
218
219     public function testResolveUnescapesValue()
220     {
221         $bag = new ParameterBag(array(
222             'foo' => array('bar' => array('ding' => 'I\'m a bar %%foo %%bar')),
223             'bar' => 'I\'m a %%foo%%',
224         ));
225
226         $bag->resolve();
227
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');
230     }
231
232     public function testEscapeValue()
233     {
234         $bag = new ParameterBag();
235
236         $bag->add(array(
237             'foo' => $bag->escapeValue(array('bar' => array('ding' => 'I\'m a bar %foo %bar', 'zero' => null))),
238             'bar' => $bag->escapeValue('I\'m a %foo%'),
239         ));
240
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');
243     }
244
245     /**
246      * @dataProvider stringsWithSpacesProvider
247      */
248     public function testResolveStringWithSpacesReturnsString($expected, $test, $description)
249     {
250         $bag = new ParameterBag(array('foo' => 'bar'));
251
252         try {
253             $this->assertEquals($expected, $bag->resolveString($test), $description);
254         } catch (ParameterNotFoundException $e) {
255             $this->fail(sprintf('%s - "%s"', $description, $expected));
256         }
257     }
258
259     public function stringsWithSpacesProvider()
260     {
261         return array(
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.'),
266         );
267     }
268 }