ParameterBagTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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\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() retuns null if null is set');
  48. }
  49. /**
  50. * @covers Symfony\Component\HttpFoundation\ParameterBag::set
  51. */
  52. public function testSet()
  53. {
  54. $bag = new ParameterBag(array());
  55. $bag->set('foo', 'bar');
  56. $this->assertEquals('bar', $bag->get('foo'), '->set() sets the value of parameter');
  57. $bag->set('foo', 'baz');
  58. $this->assertEquals('baz', $bag->get('foo'), '->set() overrides previously set parameter');
  59. }
  60. /**
  61. * @covers Symfony\Component\HttpFoundation\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->assertFalse($bag->has('unknown'), '->has() return false if a parameter is not defined');
  68. }
  69. /**
  70. * @covers Symfony\Component\HttpFoundation\ParameterBag::getAlpha
  71. */
  72. public function testGetAlpha()
  73. {
  74. $bag = new ParameterBag(array('word' => 'foo_BAR_012'));
  75. $this->assertEquals('fooBAR', $bag->getAlpha('word'), '->getAlpha() gets only alphabetic characters');
  76. $this->assertEquals('', $bag->getAlpha('unknown'), '->getAlpha() returns empty string if a parameter is not defined');
  77. }
  78. /**
  79. * @covers Symfony\Component\HttpFoundation\ParameterBag::getAlnum
  80. */
  81. public function testGetAlnum()
  82. {
  83. $bag = new ParameterBag(array('word' => 'foo_BAR_012'));
  84. $this->assertEquals('fooBAR012', $bag->getAlnum('word'), '->getAlnum() gets only alphanumeric characters');
  85. $this->assertEquals('', $bag->getAlnum('unknown'), '->getAlnum() returns empty string if a parameter is not defined');
  86. }
  87. /**
  88. * @covers Symfony\Component\HttpFoundation\ParameterBag::getDigits
  89. */
  90. public function testGetDigits()
  91. {
  92. $bag = new ParameterBag(array('word' => 'foo_BAR_012'));
  93. $this->assertEquals('012', $bag->getDigits('word'), '->getDigits() gets only digits as string');
  94. $this->assertEquals('', $bag->getDigits('unknown'), '->getDigits() returns empty string if a parameter is not defined');
  95. }
  96. /**
  97. * @covers Symfony\Component\HttpFoundation\ParameterBag::getInt
  98. */
  99. public function testGetInt()
  100. {
  101. $bag = new ParameterBag(array('digits' => '0123'));
  102. $this->assertEquals(123, $bag->getInt('digits'), '->getInt() gets a value of parameter as integer');
  103. $this->assertEquals(0, $bag->getInt('unknown'), '->getInt() returns zero if a parameter is not defined');
  104. }
  105. }