Security update for Core, with self-updated composer
[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         $bag->remove('BAR');
50         $this->assertEquals(array(), $bag->all(), '->remove() converts key to lowercase before removing');
51     }
52
53     public function testGetSet()
54     {
55         $bag = new ParameterBag(array('foo' => 'bar'));
56         $bag->set('bar', 'foo');
57         $this->assertEquals('foo', $bag->get('bar'), '->set() sets the value of a new parameter');
58
59         $bag->set('foo', 'baz');
60         $this->assertEquals('baz', $bag->get('foo'), '->set() overrides previously set parameter');
61
62         $bag->set('Foo', 'baz1');
63         $this->assertEquals('baz1', $bag->get('foo'), '->set() converts the key to lowercase');
64         $this->assertEquals('baz1', $bag->get('FOO'), '->get() converts the key to lowercase');
65
66         try {
67             $bag->get('baba');
68             $this->fail('->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException if the key does not exist');
69         } catch (\Exception $e) {
70             $this->assertInstanceOf('Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException', $e, '->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException if the key does not exist');
71             $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');
72         }
73     }
74
75     /**
76      * @dataProvider provideGetThrowParameterNotFoundExceptionData
77      */
78     public function testGetThrowParameterNotFoundException($parameterKey, $exceptionMessage)
79     {
80         $bag = new ParameterBag(array(
81             'foo' => 'foo',
82             'bar' => 'bar',
83             'baz' => 'baz',
84             'fiz' => array('bar' => array('boo' => 12)),
85         ));
86
87         if (method_exists($this, 'expectException')) {
88             $this->expectException(ParameterNotFoundException::class);
89             $this->expectExceptionMessage($exceptionMessage);
90         } else {
91             $this->setExpectedException(ParameterNotFoundException::class, $exceptionMessage);
92         }
93
94         $bag->get($parameterKey);
95     }
96
97     public function provideGetThrowParameterNotFoundExceptionData()
98     {
99         return array(
100             array('foo1', 'You have requested a non-existent parameter "foo1". Did you mean this: "foo"?'),
101             array('bag', 'You have requested a non-existent parameter "bag". Did you mean one of these: "bar", "baz"?'),
102             array('', 'You have requested a non-existent parameter "".'),
103
104             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?'),
105         );
106     }
107
108     public function testHas()
109     {
110         $bag = new ParameterBag(array('foo' => 'bar'));
111         $this->assertTrue($bag->has('foo'), '->has() returns true if a parameter is defined');
112         $this->assertTrue($bag->has('Foo'), '->has() converts the key to lowercase');
113         $this->assertFalse($bag->has('bar'), '->has() returns false if a parameter is not defined');
114     }
115
116     public function testResolveValue()
117     {
118         $bag = new ParameterBag(array());
119         $this->assertEquals('foo', $bag->resolveValue('foo'), '->resolveValue() returns its argument unmodified if no placeholders are found');
120
121         $bag = new ParameterBag(array('foo' => 'bar'));
122         $this->assertEquals('I\'m a bar', $bag->resolveValue('I\'m a %foo%'), '->resolveValue() replaces placeholders by their values');
123         $this->assertEquals(array('bar' => 'bar'), $bag->resolveValue(array('%foo%' => '%foo%')), '->resolveValue() replaces placeholders in keys and values of arrays');
124         $this->assertEquals(array('bar' => array('bar' => array('bar' => 'bar'))), $bag->resolveValue(array('%foo%' => array('%foo%' => array('%foo%' => '%foo%')))), '->resolveValue() replaces placeholders in nested arrays');
125         $this->assertEquals('I\'m a %%foo%%', $bag->resolveValue('I\'m a %%foo%%'), '->resolveValue() supports % escaping by doubling it');
126         $this->assertEquals('I\'m a bar %%foo bar', $bag->resolveValue('I\'m a %foo% %%foo %foo%'), '->resolveValue() supports % escaping by doubling it');
127         $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');
128
129         $bag = new ParameterBag(array('foo' => true));
130         $this->assertTrue($bag->resolveValue('%foo%'), '->resolveValue() replaces arguments that are just a placeholder by their value without casting them to strings');
131         $bag = new ParameterBag(array('foo' => null));
132         $this->assertNull($bag->resolveValue('%foo%'), '->resolveValue() replaces arguments that are just a placeholder by their value without casting them to strings');
133
134         $bag = new ParameterBag(array('foo' => 'bar', 'baz' => '%%%foo% %foo%%% %%foo%% %%%foo%%%'));
135         $this->assertEquals('%%bar bar%% %%foo%% %%bar%%', $bag->resolveValue('%baz%'), '->resolveValue() replaces params placed besides escaped %');
136
137         $bag = new ParameterBag(array('baz' => '%%s?%%s'));
138         $this->assertEquals('%%s?%%s', $bag->resolveValue('%baz%'), '->resolveValue() is not replacing greedily');
139
140         $bag = new ParameterBag(array());
141         try {
142             $bag->resolveValue('%foobar%');
143             $this->fail('->resolveValue() throws an InvalidArgumentException if a placeholder references a non-existent parameter');
144         } catch (ParameterNotFoundException $e) {
145             $this->assertEquals('You have requested a non-existent parameter "foobar".', $e->getMessage(), '->resolveValue() throws a ParameterNotFoundException if a placeholder references a non-existent parameter');
146         }
147
148         try {
149             $bag->resolveValue('foo %foobar% bar');
150             $this->fail('->resolveValue() throws a ParameterNotFoundException if a placeholder references a non-existent parameter');
151         } catch (ParameterNotFoundException $e) {
152             $this->assertEquals('You have requested a non-existent parameter "foobar".', $e->getMessage(), '->resolveValue() throws a ParameterNotFoundException if a placeholder references a non-existent parameter');
153         }
154
155         $bag = new ParameterBag(array('foo' => 'a %bar%', 'bar' => array()));
156         try {
157             $bag->resolveValue('%foo%');
158             $this->fail('->resolveValue() throws a RuntimeException when a parameter embeds another non-string parameter');
159         } catch (RuntimeException $e) {
160             $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');
161         }
162
163         $bag = new ParameterBag(array('foo' => '%bar%', 'bar' => '%foobar%', 'foobar' => '%foo%'));
164         try {
165             $bag->resolveValue('%foo%');
166             $this->fail('->resolveValue() throws a ParameterCircularReferenceException when a parameter has a circular reference');
167         } catch (ParameterCircularReferenceException $e) {
168             $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');
169         }
170
171         $bag = new ParameterBag(array('foo' => 'a %bar%', 'bar' => 'a %foobar%', 'foobar' => 'a %foo%'));
172         try {
173             $bag->resolveValue('%foo%');
174             $this->fail('->resolveValue() throws a ParameterCircularReferenceException when a parameter has a circular reference');
175         } catch (ParameterCircularReferenceException $e) {
176             $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');
177         }
178
179         $bag = new ParameterBag(array('host' => 'foo.bar', 'port' => 1337));
180         $this->assertEquals('foo.bar:1337', $bag->resolveValue('%host%:%port%'));
181     }
182
183     public function testResolveIndicatesWhyAParameterIsNeeded()
184     {
185         $bag = new ParameterBag(array('foo' => '%bar%'));
186
187         try {
188             $bag->resolve();
189         } catch (ParameterNotFoundException $e) {
190             $this->assertEquals('The parameter "foo" has a dependency on a non-existent parameter "bar".', $e->getMessage());
191         }
192
193         $bag = new ParameterBag(array('foo' => '%bar%'));
194
195         try {
196             $bag->resolve();
197         } catch (ParameterNotFoundException $e) {
198             $this->assertEquals('The parameter "foo" has a dependency on a non-existent parameter "bar".', $e->getMessage());
199         }
200     }
201
202     public function testResolveUnescapesValue()
203     {
204         $bag = new ParameterBag(array(
205             'foo' => array('bar' => array('ding' => 'I\'m a bar %%foo %%bar')),
206             'bar' => 'I\'m a %%foo%%',
207         ));
208
209         $bag->resolve();
210
211         $this->assertEquals('I\'m a %foo%', $bag->get('bar'), '->resolveValue() supports % escaping by doubling it');
212         $this->assertEquals(array('bar' => array('ding' => 'I\'m a bar %foo %bar')), $bag->get('foo'), '->resolveValue() supports % escaping by doubling it');
213     }
214
215     public function testEscapeValue()
216     {
217         $bag = new ParameterBag();
218
219         $bag->add(array(
220             'foo' => $bag->escapeValue(array('bar' => array('ding' => 'I\'m a bar %foo %bar', 'zero' => null))),
221             'bar' => $bag->escapeValue('I\'m a %foo%'),
222         ));
223
224         $this->assertEquals('I\'m a %%foo%%', $bag->get('bar'), '->escapeValue() escapes % by doubling it');
225         $this->assertEquals(array('bar' => array('ding' => 'I\'m a bar %%foo %%bar', 'zero' => null)), $bag->get('foo'), '->escapeValue() escapes % by doubling it');
226     }
227
228     /**
229      * @dataProvider stringsWithSpacesProvider
230      */
231     public function testResolveStringWithSpacesReturnsString($expected, $test, $description)
232     {
233         $bag = new ParameterBag(array('foo' => 'bar'));
234
235         try {
236             $this->assertEquals($expected, $bag->resolveString($test), $description);
237         } catch (ParameterNotFoundException $e) {
238             $this->fail(sprintf('%s - "%s"', $description, $expected));
239         }
240     }
241
242     public function stringsWithSpacesProvider()
243     {
244         return array(
245             array('bar', '%foo%', 'Parameters must be wrapped by %.'),
246             array('% foo %', '% foo %', 'Parameters should not have spaces.'),
247             array('{% set my_template = "foo" %}', '{% set my_template = "foo" %}', 'Twig-like strings are not parameters.'),
248             array('50% is less than 100%', '50% is less than 100%', 'Text between % signs is allowed, if there are spaces.'),
249         );
250     }
251 }