ParameterBagTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\DependencyInjection\Tests\ParameterBag;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
  13. use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
  14. use Symfony\Component\DependencyInjection\Exception\ParameterCircularReferenceException;
  15. use Symfony\Component\DependencyInjection\Exception\RuntimeException;
  16. class ParameterBagTest extends TestCase
  17. {
  18. public function testConstructor()
  19. {
  20. $bag = new ParameterBag($parameters = array(
  21. 'foo' => 'foo',
  22. 'bar' => 'bar',
  23. ));
  24. $this->assertEquals($parameters, $bag->all(), '__construct() takes an array of parameters as its first argument');
  25. }
  26. public function testClear()
  27. {
  28. $bag = new ParameterBag($parameters = array(
  29. 'foo' => 'foo',
  30. 'bar' => 'bar',
  31. ));
  32. $bag->clear();
  33. $this->assertEquals(array(), $bag->all(), '->clear() removes all parameters');
  34. }
  35. public function testRemove()
  36. {
  37. $bag = new ParameterBag(array(
  38. 'foo' => 'foo',
  39. 'bar' => 'bar',
  40. ));
  41. $bag->remove('foo');
  42. $this->assertEquals(array('bar' => 'bar'), $bag->all(), '->remove() removes a parameter');
  43. $bag->remove('BAR');
  44. $this->assertEquals(array(), $bag->all(), '->remove() converts key to lowercase before removing');
  45. }
  46. public function testGetSet()
  47. {
  48. $bag = new ParameterBag(array('foo' => 'bar'));
  49. $bag->set('bar', 'foo');
  50. $this->assertEquals('foo', $bag->get('bar'), '->set() sets the value of a new parameter');
  51. $bag->set('foo', 'baz');
  52. $this->assertEquals('baz', $bag->get('foo'), '->set() overrides previously set parameter');
  53. $bag->set('Foo', 'baz1');
  54. $this->assertEquals('baz1', $bag->get('foo'), '->set() converts the key to lowercase');
  55. $this->assertEquals('baz1', $bag->get('FOO'), '->get() converts the key to lowercase');
  56. try {
  57. $bag->get('baba');
  58. $this->fail('->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException if the key does not exist');
  59. } catch (\Exception $e) {
  60. $this->assertInstanceOf('Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException', $e, '->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException if the key does not exist');
  61. $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');
  62. }
  63. }
  64. /**
  65. * @dataProvider provideGetThrowParameterNotFoundExceptionData
  66. */
  67. public function testGetThrowParameterNotFoundException($parameterKey, $exceptionMessage)
  68. {
  69. $bag = new ParameterBag(array(
  70. 'foo' => 'foo',
  71. 'bar' => 'bar',
  72. 'baz' => 'baz',
  73. 'fiz' => array('bar' => array('boo' => 12)),
  74. ));
  75. if (method_exists($this, 'expectException')) {
  76. $this->expectException(ParameterNotFoundException::class);
  77. $this->expectExceptionMessage($exceptionMessage);
  78. } else {
  79. $this->setExpectedException(ParameterNotFoundException::class, $exceptionMessage);
  80. }
  81. $bag->get($parameterKey);
  82. }
  83. public function provideGetThrowParameterNotFoundExceptionData()
  84. {
  85. return array(
  86. array('foo1', 'You have requested a non-existent parameter "foo1". Did you mean this: "foo"?'),
  87. array('bag', 'You have requested a non-existent parameter "bag". Did you mean one of these: "bar", "baz"?'),
  88. array('', 'You have requested a non-existent parameter "".'),
  89. 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?'),
  90. );
  91. }
  92. public function testHas()
  93. {
  94. $bag = new ParameterBag(array('foo' => 'bar'));
  95. $this->assertTrue($bag->has('foo'), '->has() returns true if a parameter is defined');
  96. $this->assertTrue($bag->has('Foo'), '->has() converts the key to lowercase');
  97. $this->assertFalse($bag->has('bar'), '->has() returns false if a parameter is not defined');
  98. }
  99. public function testResolveValue()
  100. {
  101. $bag = new ParameterBag(array());
  102. $this->assertEquals('foo', $bag->resolveValue('foo'), '->resolveValue() returns its argument unmodified if no placeholders are found');
  103. $bag = new ParameterBag(array('foo' => 'bar'));
  104. $this->assertEquals('I\'m a bar', $bag->resolveValue('I\'m a %foo%'), '->resolveValue() replaces placeholders by their values');
  105. $this->assertEquals(array('bar' => 'bar'), $bag->resolveValue(array('%foo%' => '%foo%')), '->resolveValue() replaces placeholders in keys and values of arrays');
  106. $this->assertEquals(array('bar' => array('bar' => array('bar' => 'bar'))), $bag->resolveValue(array('%foo%' => array('%foo%' => array('%foo%' => '%foo%')))), '->resolveValue() replaces placeholders in nested arrays');
  107. $this->assertEquals('I\'m a %%foo%%', $bag->resolveValue('I\'m a %%foo%%'), '->resolveValue() supports % escaping by doubling it');
  108. $this->assertEquals('I\'m a bar %%foo bar', $bag->resolveValue('I\'m a %foo% %%foo %foo%'), '->resolveValue() supports % escaping by doubling it');
  109. $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');
  110. $bag = new ParameterBag(array('foo' => true));
  111. $this->assertTrue($bag->resolveValue('%foo%'), '->resolveValue() replaces arguments that are just a placeholder by their value without casting them to strings');
  112. $bag = new ParameterBag(array('foo' => null));
  113. $this->assertNull($bag->resolveValue('%foo%'), '->resolveValue() replaces arguments that are just a placeholder by their value without casting them to strings');
  114. $bag = new ParameterBag(array('foo' => 'bar', 'baz' => '%%%foo% %foo%%% %%foo%% %%%foo%%%'));
  115. $this->assertEquals('%%bar bar%% %%foo%% %%bar%%', $bag->resolveValue('%baz%'), '->resolveValue() replaces params placed besides escaped %');
  116. $bag = new ParameterBag(array('baz' => '%%s?%%s'));
  117. $this->assertEquals('%%s?%%s', $bag->resolveValue('%baz%'), '->resolveValue() is not replacing greedily');
  118. $bag = new ParameterBag(array());
  119. try {
  120. $bag->resolveValue('%foobar%');
  121. $this->fail('->resolveValue() throws an InvalidArgumentException if a placeholder references a non-existent parameter');
  122. } catch (ParameterNotFoundException $e) {
  123. $this->assertEquals('You have requested a non-existent parameter "foobar".', $e->getMessage(), '->resolveValue() throws a ParameterNotFoundException if a placeholder references a non-existent parameter');
  124. }
  125. try {
  126. $bag->resolveValue('foo %foobar% bar');
  127. $this->fail('->resolveValue() throws a ParameterNotFoundException if a placeholder references a non-existent parameter');
  128. } catch (ParameterNotFoundException $e) {
  129. $this->assertEquals('You have requested a non-existent parameter "foobar".', $e->getMessage(), '->resolveValue() throws a ParameterNotFoundException if a placeholder references a non-existent parameter');
  130. }
  131. $bag = new ParameterBag(array('foo' => 'a %bar%', 'bar' => array()));
  132. try {
  133. $bag->resolveValue('%foo%');
  134. $this->fail('->resolveValue() throws a RuntimeException when a parameter embeds another non-string parameter');
  135. } catch (RuntimeException $e) {
  136. $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');
  137. }
  138. $bag = new ParameterBag(array('foo' => '%bar%', 'bar' => '%foobar%', 'foobar' => '%foo%'));
  139. try {
  140. $bag->resolveValue('%foo%');
  141. $this->fail('->resolveValue() throws a ParameterCircularReferenceException when a parameter has a circular reference');
  142. } catch (ParameterCircularReferenceException $e) {
  143. $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');
  144. }
  145. $bag = new ParameterBag(array('foo' => 'a %bar%', 'bar' => 'a %foobar%', 'foobar' => 'a %foo%'));
  146. try {
  147. $bag->resolveValue('%foo%');
  148. $this->fail('->resolveValue() throws a ParameterCircularReferenceException when a parameter has a circular reference');
  149. } catch (ParameterCircularReferenceException $e) {
  150. $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');
  151. }
  152. $bag = new ParameterBag(array('host' => 'foo.bar', 'port' => 1337));
  153. $this->assertEquals('foo.bar:1337', $bag->resolveValue('%host%:%port%'));
  154. }
  155. public function testResolveIndicatesWhyAParameterIsNeeded()
  156. {
  157. $bag = new ParameterBag(array('foo' => '%bar%'));
  158. try {
  159. $bag->resolve();
  160. } catch (ParameterNotFoundException $e) {
  161. $this->assertEquals('The parameter "foo" has a dependency on a non-existent parameter "bar".', $e->getMessage());
  162. }
  163. $bag = new ParameterBag(array('foo' => '%bar%'));
  164. try {
  165. $bag->resolve();
  166. } catch (ParameterNotFoundException $e) {
  167. $this->assertEquals('The parameter "foo" has a dependency on a non-existent parameter "bar".', $e->getMessage());
  168. }
  169. }
  170. public function testResolveUnescapesValue()
  171. {
  172. $bag = new ParameterBag(array(
  173. 'foo' => array('bar' => array('ding' => 'I\'m a bar %%foo %%bar')),
  174. 'bar' => 'I\'m a %%foo%%',
  175. ));
  176. $bag->resolve();
  177. $this->assertEquals('I\'m a %foo%', $bag->get('bar'), '->resolveValue() supports % escaping by doubling it');
  178. $this->assertEquals(array('bar' => array('ding' => 'I\'m a bar %foo %bar')), $bag->get('foo'), '->resolveValue() supports % escaping by doubling it');
  179. }
  180. public function testEscapeValue()
  181. {
  182. $bag = new ParameterBag();
  183. $bag->add(array(
  184. 'foo' => $bag->escapeValue(array('bar' => array('ding' => 'I\'m a bar %foo %bar', 'zero' => null))),
  185. 'bar' => $bag->escapeValue('I\'m a %foo%'),
  186. ));
  187. $this->assertEquals('I\'m a %%foo%%', $bag->get('bar'), '->escapeValue() escapes % by doubling it');
  188. $this->assertEquals(array('bar' => array('ding' => 'I\'m a bar %%foo %%bar', 'zero' => null)), $bag->get('foo'), '->escapeValue() escapes % by doubling it');
  189. }
  190. /**
  191. * @dataProvider stringsWithSpacesProvider
  192. */
  193. public function testResolveStringWithSpacesReturnsString($expected, $test, $description)
  194. {
  195. $bag = new ParameterBag(array('foo' => 'bar'));
  196. try {
  197. $this->assertEquals($expected, $bag->resolveString($test), $description);
  198. } catch (ParameterNotFoundException $e) {
  199. $this->fail(sprintf('%s - "%s"', $description, $expected));
  200. }
  201. }
  202. public function stringsWithSpacesProvider()
  203. {
  204. return array(
  205. array('bar', '%foo%', 'Parameters must be wrapped by %.'),
  206. array('% foo %', '% foo %', 'Parameters should not have spaces.'),
  207. array('{% set my_template = "foo" %}', '{% set my_template = "foo" %}', 'Twig-like strings are not parameters.'),
  208. array('50% is less than 100%', '50% is less than 100%', 'Text between % signs is allowed, if there are spaces.'),
  209. );
  210. }
  211. }