ParameterBagTest.php 8.1 KB

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