ParameterBagTest.php 5.6 KB

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