ParameterBagTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
  12. use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
  13. use Symfony\Component\DependencyInjection\Exception\ParameterCircularReferenceException;
  14. use Symfony\Component\DependencyInjection\Exception\RuntimeException;
  15. class ParameterBagTest extends \PHPUnit_Framework_TestCase
  16. {
  17. /**
  18. * @covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag::__construct
  19. */
  20. public function testConstructor()
  21. {
  22. $bag = new ParameterBag($parameters = array(
  23. 'foo' => 'foo',
  24. 'bar' => 'bar',
  25. ));
  26. $this->assertEquals($parameters, $bag->all(), '__construct() takes an array of parameters as its first argument');
  27. }
  28. /**
  29. * @covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag::clear
  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. * @covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag::remove
  42. */
  43. public function testRemove()
  44. {
  45. $bag = new ParameterBag(array(
  46. 'foo' => 'foo',
  47. 'bar' => 'bar',
  48. ));
  49. $bag->remove('foo');
  50. $this->assertEquals(array('bar' => 'bar'), $bag->all(), '->remove() removes a parameter');
  51. $bag->remove('BAR');
  52. $this->assertEquals(array(), $bag->all(), '->remove() converts key to lowercase before removing');
  53. }
  54. /**
  55. * @covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag::get
  56. * @covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag::set
  57. */
  58. public function testGetSet()
  59. {
  60. $bag = new ParameterBag(array('foo' => 'bar'));
  61. $bag->set('bar', 'foo');
  62. $this->assertEquals('foo', $bag->get('bar'), '->set() sets the value of a new parameter');
  63. $bag->set('foo', 'baz');
  64. $this->assertEquals('baz', $bag->get('foo'), '->set() overrides previously set parameter');
  65. $bag->set('Foo', 'baz1');
  66. $this->assertEquals('baz1', $bag->get('foo'), '->set() converts the key to lowercase');
  67. $this->assertEquals('baz1', $bag->get('FOO'), '->get() converts the key to lowercase');
  68. try {
  69. $bag->get('baba');
  70. $this->fail('->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException if the key does not exist');
  71. } catch (\Exception $e) {
  72. $this->assertInstanceOf('Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException', $e, '->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException if the key does not exist');
  73. $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');
  74. }
  75. }
  76. public function testGetThrowParameterNotFoundException()
  77. {
  78. $bag = new ParameterBag(array(
  79. 'foo' => 'foo',
  80. 'bar' => 'bar',
  81. 'baz' => 'baz',
  82. ));
  83. try {
  84. $bag->get('foo1');
  85. $this->fail('->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException if the key does not exist');
  86. } catch (\Exception $e) {
  87. $this->assertInstanceOf('Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException', $e, '->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException if the key does not exist');
  88. $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');
  89. }
  90. try {
  91. $bag->get('bag');
  92. $this->fail('->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException if the key does not exist');
  93. } catch (\Exception $e) {
  94. $this->assertInstanceOf('Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException', $e, '->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException if the key does not exist');
  95. $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');
  96. }
  97. try {
  98. $bag->get('');
  99. $this->fail('->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException if the key does not exist');
  100. } catch (\Exception $e) {
  101. $this->assertInstanceOf('Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException', $e, '->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException if the key does not exist');
  102. $this->assertEquals('You have requested a non-existent parameter "".', $e->getMessage(), '->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException with some advices');
  103. }
  104. }
  105. /**
  106. * @covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag::has
  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. * @covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag::resolveValue
  117. */
  118. public function testResolveValue()
  119. {
  120. $bag = new ParameterBag(array());
  121. $this->assertEquals('foo', $bag->resolveValue('foo'), '->resolveValue() returns its argument unmodified if no placeholders are found');
  122. $bag = new ParameterBag(array('foo' => 'bar'));
  123. $this->assertEquals('I\'m a bar', $bag->resolveValue('I\'m a %foo%'), '->resolveValue() replaces placeholders by their values');
  124. $this->assertEquals(array('bar' => 'bar'), $bag->resolveValue(array('%foo%' => '%foo%')), '->resolveValue() replaces placeholders in keys and values of arrays');
  125. $this->assertEquals(array('bar' => array('bar' => array('bar' => 'bar'))), $bag->resolveValue(array('%foo%' => array('%foo%' => array('%foo%' => '%foo%')))), '->resolveValue() replaces placeholders in nested arrays');
  126. $this->assertEquals('I\'m a %%foo%%', $bag->resolveValue('I\'m a %%foo%%'), '->resolveValue() supports % escaping by doubling it');
  127. $this->assertEquals('I\'m a bar %%foo bar', $bag->resolveValue('I\'m a %foo% %%foo %foo%'), '->resolveValue() supports % escaping by doubling it');
  128. $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');
  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. $bag = new ParameterBag(array('foo' => 'bar', 'baz' => '%%%foo% %foo%%% %%foo%% %%%foo%%%'));
  134. $this->assertEquals('%%bar bar%% %%foo%% %%bar%%', $bag->resolveValue('%baz%'), '->resolveValue() replaces params placed besides escaped %');
  135. $bag = new ParameterBag(array('baz' => '%%s?%%s'));
  136. $this->assertEquals('%%s?%%s', $bag->resolveValue('%baz%'), '->resolveValue() is not replacing greedily');
  137. $bag = new ParameterBag(array());
  138. try {
  139. $bag->resolveValue('%foobar%');
  140. $this->fail('->resolveValue() throws an InvalidArgumentException if a placeholder references a non-existent parameter');
  141. } catch (ParameterNotFoundException $e) {
  142. $this->assertEquals('You have requested a non-existent parameter "foobar".', $e->getMessage(), '->resolveValue() throws a ParameterNotFoundException if a placeholder references a non-existent parameter');
  143. }
  144. try {
  145. $bag->resolveValue('foo %foobar% bar');
  146. $this->fail('->resolveValue() throws a ParameterNotFoundException if a placeholder references a non-existent parameter');
  147. } catch (ParameterNotFoundException $e) {
  148. $this->assertEquals('You have requested a non-existent parameter "foobar".', $e->getMessage(), '->resolveValue() throws a ParameterNotFoundException if a placeholder references a non-existent parameter');
  149. }
  150. $bag = new ParameterBag(array('foo' => 'a %bar%', 'bar' => array()));
  151. try {
  152. $bag->resolveValue('%foo%');
  153. $this->fail('->resolveValue() throws a RuntimeException when a parameter embeds another non-string parameter');
  154. } catch (RuntimeException $e) {
  155. $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');
  156. }
  157. $bag = new ParameterBag(array('foo' => '%bar%', 'bar' => '%foobar%', 'foobar' => '%foo%'));
  158. try {
  159. $bag->resolveValue('%foo%');
  160. $this->fail('->resolveValue() throws a ParameterCircularReferenceException when a parameter has a circular reference');
  161. } catch (ParameterCircularReferenceException $e) {
  162. $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');
  163. }
  164. $bag = new ParameterBag(array('foo' => 'a %bar%', 'bar' => 'a %foobar%', 'foobar' => 'a %foo%'));
  165. try {
  166. $bag->resolveValue('%foo%');
  167. $this->fail('->resolveValue() throws a ParameterCircularReferenceException when a parameter has a circular reference');
  168. } catch (ParameterCircularReferenceException $e) {
  169. $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');
  170. }
  171. $bag = new ParameterBag(array('host' => 'foo.bar', 'port' => 1337));
  172. $this->assertEquals('foo.bar:1337', $bag->resolveValue('%host%:%port%'));
  173. }
  174. /**
  175. * @covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag::resolve
  176. */
  177. public function testResolveIndicatesWhyAParameterIsNeeded()
  178. {
  179. $bag = new ParameterBag(array('foo' => '%bar%'));
  180. try {
  181. $bag->resolve();
  182. } catch (ParameterNotFoundException $e) {
  183. $this->assertEquals('The parameter "foo" has a dependency on a non-existent parameter "bar".', $e->getMessage());
  184. }
  185. $bag = new ParameterBag(array('foo' => '%bar%'));
  186. try {
  187. $bag->resolve();
  188. } catch (ParameterNotFoundException $e) {
  189. $this->assertEquals('The parameter "foo" has a dependency on a non-existent parameter "bar".', $e->getMessage());
  190. }
  191. }
  192. /**
  193. * @covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag::resolve
  194. */
  195. public function testResolveUnescapesValue()
  196. {
  197. $bag = new ParameterBag(array(
  198. 'foo' => array('bar' => array('ding' => 'I\'m a bar %%foo %%bar')),
  199. 'bar' => 'I\'m a %%foo%%',
  200. ));
  201. $bag->resolve();
  202. $this->assertEquals('I\'m a %foo%', $bag->get('bar'), '->resolveValue() supports % escaping by doubling it');
  203. $this->assertEquals(array('bar' => array('ding' => 'I\'m a bar %foo %bar')), $bag->get('foo'), '->resolveValue() supports % escaping by doubling it');
  204. }
  205. /**
  206. * @covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag::escapeValue
  207. */
  208. public function testEscapeValue()
  209. {
  210. $bag = new ParameterBag();
  211. $bag->add(array(
  212. 'foo' => $bag->escapeValue(array('bar' => array('ding' => 'I\'m a bar %foo %bar', 'zero' => null))),
  213. 'bar' => $bag->escapeValue('I\'m a %foo%'),
  214. ));
  215. $this->assertEquals('I\'m a %%foo%%', $bag->get('bar'), '->escapeValue() escapes % by doubling it');
  216. $this->assertEquals(array('bar' => array('ding' => 'I\'m a bar %%foo %%bar', 'zero' => null)), $bag->get('foo'), '->escapeValue() escapes % by doubling it');
  217. }
  218. /**
  219. * @covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag::resolve
  220. * @dataProvider stringsWithSpacesProvider
  221. */
  222. public function testResolveStringWithSpacesReturnsString($expected, $test, $description)
  223. {
  224. $bag = new ParameterBag(array('foo' => 'bar'));
  225. try {
  226. $this->assertEquals($expected, $bag->resolveString($test), $description);
  227. } catch (ParameterNotFoundException $e) {
  228. $this->fail(sprintf('%s - "%s"', $description, $expected));
  229. }
  230. }
  231. public function stringsWithSpacesProvider()
  232. {
  233. return array(
  234. array('bar', '%foo%', 'Parameters must be wrapped by %.'),
  235. array('% foo %', '% foo %', 'Parameters should not have spaces.'),
  236. array('{% set my_template = "foo" %}', '{% set my_template = "foo" %}', 'Twig-like strings are not parameters.'),
  237. array('50% is less than 100%', '50% is less than 100%', 'Text between % signs is allowed, if there are spaces.'),
  238. );
  239. }
  240. }