ParameterBagTest.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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\HttpFoundation;
  11. use Symfony\Component\HttpFoundation\ParameterBag;
  12. class ParameterBagTest extends \PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * @covers Symfony\Component\HttpFoundation\ParameterBag::__construct
  16. */
  17. public function testConstructor()
  18. {
  19. $this->testAll();
  20. }
  21. /**
  22. * @covers Symfony\Component\HttpFoundation\ParameterBag::all
  23. */
  24. public function testAll()
  25. {
  26. $bag = new ParameterBag(array('foo' => 'bar'));
  27. $this->assertEquals(array('foo' => 'bar'), $bag->all(), '->all() gets all the input');
  28. }
  29. /**
  30. * @covers Symfony\Component\HttpFoundation\ParameterBag::replace
  31. */
  32. public function testReplace()
  33. {
  34. $bag = new ParameterBag(array('foo' => 'bar'));
  35. $bag->replace(array('FOO' => 'BAR'));
  36. $this->assertEquals(array('FOO' => 'BAR'), $bag->all(), '->replace() replaces the input with the argument');
  37. $this->assertFalse($bag->has('foo'), '->replace() overrides previously set the input');
  38. }
  39. /**
  40. * @covers Symfony\Component\HttpFoundation\ParameterBag::get
  41. */
  42. public function testGet()
  43. {
  44. $bag = new ParameterBag(array('foo' => 'bar', 'null' => null));
  45. $this->assertEquals('bar', $bag->get('foo'), '->get() gets the value of a parameter');
  46. $this->assertEquals('default', $bag->get('unknown', 'default'), '->get() returns second argument as default if a parameter is not defined');
  47. $this->assertNull($bag->get('null', 'default'), '->get() returns null if null is set');
  48. }
  49. public function testGetDoesNotUseDeepByDefault()
  50. {
  51. $bag = new ParameterBag(array('foo' => array('bar' => 'moo')));
  52. $this->assertNull($bag->get('foo[bar]'));
  53. }
  54. /**
  55. * @dataProvider getInvalidPaths
  56. * @expectedException \InvalidArgumentException
  57. */
  58. public function testGetDeepWithInvalidPaths($path)
  59. {
  60. $bag = new ParameterBag(array('foo' => array('bar' => 'moo')));
  61. $bag->get($path, null, true);
  62. }
  63. public function getInvalidPaths()
  64. {
  65. return array(
  66. array('foo[['),
  67. array('foo[d'),
  68. array('foo[bar]]'),
  69. array('foo[bar]d'),
  70. );
  71. }
  72. public function testGetDeep()
  73. {
  74. $bag = new ParameterBag(array('foo' => array('bar' => array('moo' => 'boo'))));
  75. $this->assertEquals(array('moo' => 'boo'), $bag->get('foo[bar]', null, true));
  76. $this->assertEquals('boo', $bag->get('foo[bar][moo]', null, true));
  77. $this->assertEquals('default', $bag->get('foo[bar][foo]', 'default', true));
  78. $this->assertEquals('default', $bag->get('bar[moo][foo]', 'default', true));
  79. }
  80. /**
  81. * @covers Symfony\Component\HttpFoundation\ParameterBag::set
  82. */
  83. public function testSet()
  84. {
  85. $bag = new ParameterBag(array());
  86. $bag->set('foo', 'bar');
  87. $this->assertEquals('bar', $bag->get('foo'), '->set() sets the value of parameter');
  88. $bag->set('foo', 'baz');
  89. $this->assertEquals('baz', $bag->get('foo'), '->set() overrides previously set parameter');
  90. }
  91. /**
  92. * @covers Symfony\Component\HttpFoundation\ParameterBag::has
  93. */
  94. public function testHas()
  95. {
  96. $bag = new ParameterBag(array('foo' => 'bar'));
  97. $this->assertTrue($bag->has('foo'), '->has() returns true if a parameter is defined');
  98. $this->assertFalse($bag->has('unknown'), '->has() return false if a parameter is not defined');
  99. }
  100. /**
  101. * @covers Symfony\Component\HttpFoundation\ParameterBag::getAlpha
  102. */
  103. public function testGetAlpha()
  104. {
  105. $bag = new ParameterBag(array('word' => 'foo_BAR_012'));
  106. $this->assertEquals('fooBAR', $bag->getAlpha('word'), '->getAlpha() gets only alphabetic characters');
  107. $this->assertEquals('', $bag->getAlpha('unknown'), '->getAlpha() returns empty string if a parameter is not defined');
  108. }
  109. /**
  110. * @covers Symfony\Component\HttpFoundation\ParameterBag::getAlnum
  111. */
  112. public function testGetAlnum()
  113. {
  114. $bag = new ParameterBag(array('word' => 'foo_BAR_012'));
  115. $this->assertEquals('fooBAR012', $bag->getAlnum('word'), '->getAlnum() gets only alphanumeric characters');
  116. $this->assertEquals('', $bag->getAlnum('unknown'), '->getAlnum() returns empty string if a parameter is not defined');
  117. }
  118. /**
  119. * @covers Symfony\Component\HttpFoundation\ParameterBag::getDigits
  120. */
  121. public function testGetDigits()
  122. {
  123. $bag = new ParameterBag(array('word' => 'foo_BAR_012'));
  124. $this->assertEquals('012', $bag->getDigits('word'), '->getDigits() gets only digits as string');
  125. $this->assertEquals('', $bag->getDigits('unknown'), '->getDigits() returns empty string if a parameter is not defined');
  126. }
  127. /**
  128. * @covers Symfony\Component\HttpFoundation\ParameterBag::getInt
  129. */
  130. public function testGetInt()
  131. {
  132. $bag = new ParameterBag(array('digits' => '0123'));
  133. $this->assertEquals(123, $bag->getInt('digits'), '->getInt() gets a value of parameter as integer');
  134. $this->assertEquals(0, $bag->getInt('unknown'), '->getInt() returns zero if a parameter is not defined');
  135. }
  136. }