ParameterBagTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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\Tests\Component\DependencyInjection\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::get
  42. * @covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag::set
  43. */
  44. public function testGetSet()
  45. {
  46. $bag = new ParameterBag(array('foo' => 'bar'));
  47. $bag->set('bar', 'foo');
  48. $this->assertEquals('foo', $bag->get('bar'), '->set() sets the value of a new parameter');
  49. $bag->set('foo', 'baz');
  50. $this->assertEquals('baz', $bag->get('foo'), '->set() overrides previously set parameter');
  51. $bag->set('Foo', 'baz1');
  52. $this->assertEquals('baz1', $bag->get('foo'), '->set() converts the key to lowercase');
  53. $this->assertEquals('baz1', $bag->get('FOO'), '->get() converts the key to lowercase');
  54. try {
  55. $bag->get('baba');
  56. $this->fail('->get() throws an \InvalidArgumentException if the key does not exist');
  57. } catch (\Exception $e) {
  58. $this->assertInstanceOf('\InvalidArgumentException', $e, '->get() throws an \InvalidArgumentException if the key does not exist');
  59. $this->assertEquals('You have requested a non-existent parameter "baba".', $e->getMessage(), '->get() throws an \InvalidArgumentException if the key does not exist');
  60. }
  61. }
  62. /**
  63. * @covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag::has
  64. */
  65. public function testHas()
  66. {
  67. $bag = new ParameterBag(array('foo' => 'bar'));
  68. $this->assertTrue($bag->has('foo'), '->has() returns true if a parameter is defined');
  69. $this->assertTrue($bag->has('Foo'), '->has() converts the key to lowercase');
  70. $this->assertFalse($bag->has('bar'), '->has() returns false if a parameter is not defined');
  71. }
  72. /**
  73. * @covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag::resolveValue
  74. */
  75. public function testResolveValue()
  76. {
  77. $bag = new ParameterBag(array());
  78. $this->assertEquals('foo', $bag->resolveValue('foo'), '->resolveValue() returns its argument unmodified if no placeholders are found');
  79. $bag = new ParameterBag(array('foo' => 'bar'));
  80. $this->assertEquals('I\'m a bar', $bag->resolveValue('I\'m a %foo%'), '->resolveValue() replaces placeholders by their values');
  81. $this->assertEquals(array('bar' => 'bar'), $bag->resolveValue(array('%foo%' => '%foo%')), '->resolveValue() replaces placeholders in keys and values of arrays');
  82. $this->assertEquals(array('bar' => array('bar' => array('bar' => 'bar'))), $bag->resolveValue(array('%foo%' => array('%foo%' => array('%foo%' => '%foo%')))), '->resolveValue() replaces placeholders in nested arrays');
  83. $this->assertEquals('I\'m a %%foo%%', $bag->resolveValue('I\'m a %%foo%%'), '->resolveValue() supports % escaping by doubling it');
  84. $this->assertEquals('I\'m a bar %%foo bar', $bag->resolveValue('I\'m a %foo% %%foo %foo%'), '->resolveValue() supports % escaping by doubling it');
  85. $bag = new ParameterBag(array('foo' => true));
  86. $this->assertSame(true, $bag->resolveValue('%foo%'), '->resolveValue() replaces arguments that are just a placeholder by their value without casting them to strings');
  87. $bag = new ParameterBag(array('foo' => null));
  88. $this->assertSame(null, $bag->resolveValue('%foo%'), '->resolveValue() replaces arguments that are just a placeholder by their value without casting them to strings');
  89. $bag = new ParameterBag(array());
  90. try {
  91. $bag->resolveValue('%foobar%');
  92. $this->fail('->resolveValue() throws an InvalidArgumentException if a placeholder references a non-existent parameter');
  93. } catch (ParameterNotFoundException $e) {
  94. $this->assertEquals('You have requested a non-existent parameter "foobar".', $e->getMessage(), '->resolveValue() throws a ParameterNotFoundException if a placeholder references a non-existent parameter');
  95. }
  96. try {
  97. $bag->resolveValue('foo %foobar% bar');
  98. $this->fail('->resolveValue() throws a ParameterNotFoundException if a placeholder references a non-existent parameter');
  99. } catch (ParameterNotFoundException $e) {
  100. $this->assertEquals('You have requested a non-existent parameter "foobar".', $e->getMessage(), '->resolveValue() throws a ParameterNotFoundException if a placeholder references a non-existent parameter');
  101. }
  102. $bag = new ParameterBag(array('foo' => 'a %bar%', 'bar' => array()));
  103. try {
  104. $bag->resolveValue('%foo%');
  105. $this->fail('->resolveValue() throws a RuntimeException when a parameter embeds another non-string parameter');
  106. } catch (RuntimeException $e) {
  107. $this->assertEquals('A parameter cannot contain a non-string parameter.', $e->getMessage(), '->resolveValue() throws a RuntimeException when a parameter embeds another non-string parameter');
  108. }
  109. $bag = new ParameterBag(array('foo' => '%bar%', 'bar' => '%foobar%', 'foobar' => '%foo%'));
  110. try {
  111. $bag->resolveValue('%foo%');
  112. $this->fail('->resolveValue() throws a ParameterCircularReferenceException when a parameter has a circular reference');
  113. } catch (ParameterCircularReferenceException $e) {
  114. $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');
  115. }
  116. $bag = new ParameterBag(array('foo' => 'a %bar%', 'bar' => 'a %foobar%', 'foobar' => 'a %foo%'));
  117. try {
  118. $bag->resolveValue('%foo%');
  119. $this->fail('->resolveValue() throws a ParameterCircularReferenceException when a parameter has a circular reference');
  120. } catch (ParameterCircularReferenceException $e) {
  121. $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');
  122. }
  123. }
  124. /**
  125. * @covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag::resolve
  126. */
  127. public function testResolveIndicatesWhyAParameterIsNeeded()
  128. {
  129. $bag = new ParameterBag(array('foo' => '%bar%'));
  130. try {
  131. $bag->resolve();
  132. } catch (ParameterNotFoundException $e) {
  133. $this->assertEquals('The parameter "foo" has a dependency on a non-existent parameter "bar".', $e->getMessage());
  134. }
  135. $bag = new ParameterBag(array('foo' => '%bar%'));
  136. try {
  137. $bag->resolve();
  138. } catch (ParameterNotFoundException $e) {
  139. $this->assertEquals('The parameter "foo" has a dependency on a non-existent parameter "bar".', $e->getMessage());
  140. }
  141. }
  142. }