ParameterBagTest.php 6.2 KB

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