ParameterBagTest.php 13 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\Exception\ParameterCircularReferenceException;
  13. use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
  14. use Symfony\Component\DependencyInjection\Exception\RuntimeException;
  15. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
  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. public function testGetThrowParameterNotFoundException()
  65. {
  66. $bag = new ParameterBag(array(
  67. 'foo' => 'foo',
  68. 'bar' => 'bar',
  69. 'baz' => 'baz',
  70. ));
  71. try {
  72. $bag->get('foo1');
  73. $this->fail('->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException if the key does not exist');
  74. } catch (\Exception $e) {
  75. $this->assertInstanceOf('Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException', $e, '->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException if the key does not exist');
  76. $this->assertEquals('You have requested a non-existent parameter "foo1". Did you mean this: "foo"?', $e->getMessage(), '->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException with some advices');
  77. }
  78. try {
  79. $bag->get('bag');
  80. $this->fail('->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException if the key does not exist');
  81. } catch (\Exception $e) {
  82. $this->assertInstanceOf('Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException', $e, '->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException if the key does not exist');
  83. $this->assertEquals('You have requested a non-existent parameter "bag". Did you mean one of these: "bar", "baz"?', $e->getMessage(), '->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException with some advices');
  84. }
  85. try {
  86. $bag->get('');
  87. $this->fail('->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException if the key does not exist');
  88. } catch (\Exception $e) {
  89. $this->assertInstanceOf('Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException', $e, '->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException if the key does not exist');
  90. $this->assertEquals('You have requested a non-existent parameter "".', $e->getMessage(), '->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException with some advices');
  91. }
  92. }
  93. public function testHas()
  94. {
  95. $bag = new ParameterBag(array('foo' => 'bar'));
  96. $this->assertTrue($bag->has('foo'), '->has() returns true if a parameter is defined');
  97. $this->assertTrue($bag->has('Foo'), '->has() converts the key to lowercase');
  98. $this->assertFalse($bag->has('bar'), '->has() returns false if a parameter is not defined');
  99. }
  100. public function testResolveValue()
  101. {
  102. $bag = new ParameterBag(array());
  103. $this->assertEquals('foo', $bag->resolveValue('foo'), '->resolveValue() returns its argument unmodified if no placeholders are found');
  104. $bag = new ParameterBag(array('foo' => 'bar'));
  105. $this->assertEquals('I\'m a bar', $bag->resolveValue('I\'m a %foo%'), '->resolveValue() replaces placeholders by their values');
  106. $this->assertEquals(array('bar' => 'bar'), $bag->resolveValue(array('%foo%' => '%foo%')), '->resolveValue() replaces placeholders in keys and values of arrays');
  107. $this->assertEquals(array('bar' => array('bar' => array('bar' => 'bar'))), $bag->resolveValue(array('%foo%' => array('%foo%' => array('%foo%' => '%foo%')))), '->resolveValue() replaces placeholders in nested arrays');
  108. $this->assertEquals('I\'m a %%foo%%', $bag->resolveValue('I\'m a %%foo%%'), '->resolveValue() supports % escaping by doubling it');
  109. $this->assertEquals('I\'m a bar %%foo bar', $bag->resolveValue('I\'m a %foo% %%foo %foo%'), '->resolveValue() supports % escaping by doubling it');
  110. $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');
  111. $bag = new ParameterBag(array('foo' => true));
  112. $this->assertTrue($bag->resolveValue('%foo%'), '->resolveValue() replaces arguments that are just a placeholder by their value without casting them to strings');
  113. $bag = new ParameterBag(array('foo' => null));
  114. $this->assertNull($bag->resolveValue('%foo%'), '->resolveValue() replaces arguments that are just a placeholder by their value without casting them to strings');
  115. $bag = new ParameterBag(array('foo' => 'bar', 'baz' => '%%%foo% %foo%%% %%foo%% %%%foo%%%'));
  116. $this->assertEquals('%%bar bar%% %%foo%% %%bar%%', $bag->resolveValue('%baz%'), '->resolveValue() replaces params placed besides escaped %');
  117. $bag = new ParameterBag(array('baz' => '%%s?%%s'));
  118. $this->assertEquals('%%s?%%s', $bag->resolveValue('%baz%'), '->resolveValue() is not replacing greedily');
  119. $bag = new ParameterBag(array());
  120. try {
  121. $bag->resolveValue('%foobar%');
  122. $this->fail('->resolveValue() throws an InvalidArgumentException if a placeholder references a non-existent parameter');
  123. } catch (ParameterNotFoundException $e) {
  124. $this->assertEquals('You have requested a non-existent parameter "foobar".', $e->getMessage(), '->resolveValue() throws a ParameterNotFoundException if a placeholder references a non-existent parameter');
  125. }
  126. try {
  127. $bag->resolveValue('foo %foobar% bar');
  128. $this->fail('->resolveValue() throws a ParameterNotFoundException if a placeholder references a non-existent parameter');
  129. } catch (ParameterNotFoundException $e) {
  130. $this->assertEquals('You have requested a non-existent parameter "foobar".', $e->getMessage(), '->resolveValue() throws a ParameterNotFoundException if a placeholder references a non-existent parameter');
  131. }
  132. $bag = new ParameterBag(array('foo' => 'a %bar%', 'bar' => array()));
  133. try {
  134. $bag->resolveValue('%foo%');
  135. $this->fail('->resolveValue() throws a RuntimeException when a parameter embeds another non-string parameter');
  136. } catch (RuntimeException $e) {
  137. $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');
  138. }
  139. $bag = new ParameterBag(array('foo' => '%bar%', 'bar' => '%foobar%', 'foobar' => '%foo%'));
  140. try {
  141. $bag->resolveValue('%foo%');
  142. $this->fail('->resolveValue() throws a ParameterCircularReferenceException when a parameter has a circular reference');
  143. } catch (ParameterCircularReferenceException $e) {
  144. $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');
  145. }
  146. $bag = new ParameterBag(array('foo' => 'a %bar%', 'bar' => 'a %foobar%', 'foobar' => 'a %foo%'));
  147. try {
  148. $bag->resolveValue('%foo%');
  149. $this->fail('->resolveValue() throws a ParameterCircularReferenceException when a parameter has a circular reference');
  150. } catch (ParameterCircularReferenceException $e) {
  151. $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');
  152. }
  153. $bag = new ParameterBag(array('host' => 'foo.bar', 'port' => 1337));
  154. $this->assertEquals('foo.bar:1337', $bag->resolveValue('%host%:%port%'));
  155. }
  156. public function testResolveIndicatesWhyAParameterIsNeeded()
  157. {
  158. $bag = new ParameterBag(array('foo' => '%bar%'));
  159. try {
  160. $bag->resolve();
  161. } catch (ParameterNotFoundException $e) {
  162. $this->assertEquals('The parameter "foo" has a dependency on a non-existent parameter "bar".', $e->getMessage());
  163. }
  164. $bag = new ParameterBag(array('foo' => '%bar%'));
  165. try {
  166. $bag->resolve();
  167. } catch (ParameterNotFoundException $e) {
  168. $this->assertEquals('The parameter "foo" has a dependency on a non-existent parameter "bar".', $e->getMessage());
  169. }
  170. }
  171. public function testResolveUnescapesValue()
  172. {
  173. $bag = new ParameterBag(array(
  174. 'foo' => array('bar' => array('ding' => 'I\'m a bar %%foo %%bar')),
  175. 'bar' => 'I\'m a %%foo%%',
  176. ));
  177. $bag->resolve();
  178. $this->assertEquals('I\'m a %foo%', $bag->get('bar'), '->resolveValue() supports % escaping by doubling it');
  179. $this->assertEquals(array('bar' => array('ding' => 'I\'m a bar %foo %bar')), $bag->get('foo'), '->resolveValue() supports % escaping by doubling it');
  180. }
  181. public function testEscapeValue()
  182. {
  183. $bag = new ParameterBag();
  184. $bag->add(array(
  185. 'foo' => $bag->escapeValue(array('bar' => array('ding' => 'I\'m a bar %foo %bar', 'zero' => null))),
  186. 'bar' => $bag->escapeValue('I\'m a %foo%'),
  187. ));
  188. $this->assertEquals('I\'m a %%foo%%', $bag->get('bar'), '->escapeValue() escapes % by doubling it');
  189. $this->assertEquals(array('bar' => array('ding' => 'I\'m a bar %%foo %%bar', 'zero' => null)), $bag->get('foo'), '->escapeValue() escapes % by doubling it');
  190. }
  191. /**
  192. * @dataProvider stringsWithSpacesProvider
  193. */
  194. public function testResolveStringWithSpacesReturnsString($expected, $test, $description)
  195. {
  196. $bag = new ParameterBag(array('foo' => 'bar'));
  197. try {
  198. $this->assertEquals($expected, $bag->resolveString($test), $description);
  199. } catch (ParameterNotFoundException $e) {
  200. $this->fail(sprintf('%s - "%s"', $description, $expected));
  201. }
  202. }
  203. public function stringsWithSpacesProvider()
  204. {
  205. return array(
  206. array('bar', '%foo%', 'Parameters must be wrapped by %.'),
  207. array('% foo %', '% foo %', 'Parameters should not have spaces.'),
  208. array('{% set my_template = "foo" %}', '{% set my_template = "foo" %}', 'Twig-like strings are not parameters.'),
  209. array('50% is less than 100%', '50% is less than 100%', 'Text between % signs is allowed, if there are spaces.'),
  210. );
  211. }
  212. }