ParameterBagTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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\Components\DependencyInjection\ParameterBag;
  10. use Symfony\Components\DependencyInjection\ParameterBag\ParameterBag;
  11. class ParameterBagTest extends \PHPUnit_Framework_TestCase
  12. {
  13. /**
  14. * @covers Symfony\Components\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\Components\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\Components\DependencyInjection\ParameterBag\ParameterBag::get
  38. * @covers Symfony\Components\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\Components\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. }