ParameterBagTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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. class ParameterBagTest extends \PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * @covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag::__construct
  16. */
  17. public function testConstructor()
  18. {
  19. $bag = new ParameterBag($parameters = array(
  20. 'foo' => 'foo',
  21. 'bar' => 'bar',
  22. ));
  23. $this->assertEquals($parameters, $bag->all(), '__construct() takes an array of parameters as its first argument');
  24. }
  25. /**
  26. * @covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag::clear
  27. */
  28. public function testClear()
  29. {
  30. $bag = new ParameterBag($parameters = array(
  31. 'foo' => 'foo',
  32. 'bar' => 'bar',
  33. ));
  34. $bag->clear();
  35. $this->assertEquals(array(), $bag->all(), '->clear() removes all parameters');
  36. }
  37. /**
  38. * @covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag::get
  39. * @covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag::set
  40. */
  41. public function testGetSet()
  42. {
  43. $bag = new ParameterBag(array('foo' => 'bar'));
  44. $bag->set('bar', 'foo');
  45. $this->assertEquals('foo', $bag->get('bar'), '->set() sets the value of a new parameter');
  46. $bag->set('foo', 'baz');
  47. $this->assertEquals('baz', $bag->get('foo'), '->set() overrides previously set parameter');
  48. $bag->set('Foo', 'baz1');
  49. $this->assertEquals('baz1', $bag->get('foo'), '->set() converts the key to lowercase');
  50. $this->assertEquals('baz1', $bag->get('FOO'), '->get() converts the key to lowercase');
  51. try {
  52. $bag->get('baba');
  53. $this->fail('->get() throws an \InvalidArgumentException if the key does not exist');
  54. } catch (\Exception $e) {
  55. $this->assertInstanceOf('\InvalidArgumentException', $e, '->get() throws an \InvalidArgumentException if the key does not exist');
  56. $this->assertEquals('The parameter "baba" must be defined.', $e->getMessage(), '->get() throws an \InvalidArgumentException if the key does not exist');
  57. }
  58. }
  59. /**
  60. * @covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag::has
  61. */
  62. public function testHas()
  63. {
  64. $bag = new ParameterBag(array('foo' => 'bar'));
  65. $this->assertTrue($bag->has('foo'), '->has() returns true if a parameter is defined');
  66. $this->assertTrue($bag->has('Foo'), '->has() converts the key to lowercase');
  67. $this->assertFalse($bag->has('bar'), '->has() returns false if a parameter is not defined');
  68. }
  69. /**
  70. * @covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag::resolveValue
  71. */
  72. public function testResolveValue()
  73. {
  74. $bag = new ParameterBag(array());
  75. $this->assertEquals('foo', $bag->resolveValue('foo'), '->resolveValue() returns its argument unmodified if no placeholders are found');
  76. $bag = new ParameterBag(array('foo' => 'bar'));
  77. $this->assertEquals('I\'m a bar', $bag->resolveValue('I\'m a %foo%'), '->resolveValue() replaces placeholders by their values');
  78. $this->assertEquals(array('bar' => 'bar'), $bag->resolveValue(array('%foo%' => '%foo%')), '->resolveValue() replaces placeholders in keys and values of arrays');
  79. $this->assertEquals(array('bar' => array('bar' => array('bar' => 'bar'))), $bag->resolveValue(array('%foo%' => array('%foo%' => array('%foo%' => '%foo%')))), '->resolveValue() replaces placeholders in nested arrays');
  80. $this->assertEquals('I\'m a %foo%', $bag->resolveValue('I\'m a %%foo%%'), '->resolveValue() supports % escaping by doubling it');
  81. $this->assertEquals('I\'m a bar %foo bar', $bag->resolveValue('I\'m a %foo% %%foo %foo%'), '->resolveValue() supports % escaping by doubling it');
  82. $bag = new ParameterBag(array('foo' => true));
  83. $this->assertTrue($bag->resolveValue('%foo%') === true, '->resolveValue() replaces arguments that are just a placeholder by their value without casting them to strings');
  84. $bag = new ParameterBag(array());
  85. try {
  86. $bag->resolveValue('%foobar%', array());
  87. $this->fail('->resolveValue() throws an InvalidArgumentException if a placeholder references a non-existent parameter');
  88. } catch (\Exception $e) {
  89. $this->assertInstanceOf('\InvalidArgumentException', $e, '->resolveValue() throws an InvalidArgumentException if a placeholder references a non-existent parameter');
  90. $this->assertEquals('The parameter "foobar" must be defined.', $e->getMessage(), '->resolveValue() throws an InvalidArgumentException if a placeholder references a non-existent parameter');
  91. }
  92. try {
  93. $bag->resolveValue('foo %foobar% bar', array());
  94. $this->fail('->resolveValue() throws an InvalidArgumentException if a placeholder references a non-existent parameter');
  95. } catch (\Exception $e) {
  96. $this->assertInstanceOf('\InvalidArgumentException', $e, '->resolveValue() throws an InvalidArgumentException if a placeholder references a non-existent parameter');
  97. $this->assertEquals('The parameter "foobar" must be defined.', $e->getMessage(), '->resolveValue() throws an InvalidArgumentException if a placeholder references a non-existent parameter');
  98. }
  99. }
  100. }