ParameterBagTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. * @covers Symfony\Component\HttpFoundation\ParameterBag::getDeep
  58. */
  59. public function testGetDeepWithInvalidPaths($path)
  60. {
  61. $bag = new ParameterBag(array('foo' => array('bar' => 'moo')));
  62. $bag->get($path, null, true);
  63. }
  64. public function getInvalidPaths()
  65. {
  66. return array(
  67. array('foo[['),
  68. array('foo[d'),
  69. array('foo[bar]]'),
  70. array('foo[bar]d'),
  71. );
  72. }
  73. /**
  74. * @covers Symfony\Component\HttpFoundation\ParameterBag::getDeep
  75. */
  76. public function testGetDeep()
  77. {
  78. $bag = new ParameterBag(array('foo' => array('bar' => array('moo' => 'boo'))));
  79. $this->assertEquals(array('moo' => 'boo'), $bag->get('foo[bar]', null, true));
  80. $this->assertEquals('boo', $bag->get('foo[bar][moo]', null, true));
  81. $this->assertEquals('default', $bag->get('foo[bar][foo]', 'default', true));
  82. $this->assertEquals('default', $bag->get('bar[moo][foo]', 'default', true));
  83. }
  84. /**
  85. * @covers Symfony\Component\HttpFoundation\ParameterBag::set
  86. */
  87. public function testSet()
  88. {
  89. $bag = new ParameterBag(array());
  90. $bag->set('foo', 'bar');
  91. $this->assertEquals('bar', $bag->get('foo'), '->set() sets the value of parameter');
  92. $bag->set('foo', 'baz');
  93. $this->assertEquals('baz', $bag->get('foo'), '->set() overrides previously set parameter');
  94. }
  95. /**
  96. * @covers Symfony\Component\HttpFoundation\ParameterBag::has
  97. */
  98. public function testHas()
  99. {
  100. $bag = new ParameterBag(array('foo' => 'bar'));
  101. $this->assertTrue($bag->has('foo'), '->has() returns true if a parameter is defined');
  102. $this->assertFalse($bag->has('unknown'), '->has() return false if a parameter is not defined');
  103. }
  104. /**
  105. * @covers Symfony\Component\HttpFoundation\ParameterBag::getAlpha
  106. */
  107. public function testGetAlpha()
  108. {
  109. $bag = new ParameterBag(array('word' => 'foo_BAR_012'));
  110. $this->assertEquals('fooBAR', $bag->getAlpha('word'), '->getAlpha() gets only alphabetic characters');
  111. $this->assertEquals('', $bag->getAlpha('unknown'), '->getAlpha() returns empty string if a parameter is not defined');
  112. }
  113. /**
  114. * @covers Symfony\Component\HttpFoundation\ParameterBag::getAlnum
  115. */
  116. public function testGetAlnum()
  117. {
  118. $bag = new ParameterBag(array('word' => 'foo_BAR_012'));
  119. $this->assertEquals('fooBAR012', $bag->getAlnum('word'), '->getAlnum() gets only alphanumeric characters');
  120. $this->assertEquals('', $bag->getAlnum('unknown'), '->getAlnum() returns empty string if a parameter is not defined');
  121. }
  122. /**
  123. * @covers Symfony\Component\HttpFoundation\ParameterBag::getDigits
  124. */
  125. public function testGetDigits()
  126. {
  127. $bag = new ParameterBag(array('word' => 'foo_BAR_012'));
  128. $this->assertEquals('012', $bag->getDigits('word'), '->getDigits() gets only digits as string');
  129. $this->assertEquals('', $bag->getDigits('unknown'), '->getDigits() returns empty string if a parameter is not defined');
  130. }
  131. /**
  132. * @covers Symfony\Component\HttpFoundation\ParameterBag::getInt
  133. */
  134. public function testGetInt()
  135. {
  136. $bag = new ParameterBag(array('digits' => '0123'));
  137. $this->assertEquals(123, $bag->getInt('digits'), '->getInt() gets a value of parameter as integer');
  138. $this->assertEquals(0, $bag->getInt('unknown'), '->getInt() returns zero if a parameter is not defined');
  139. }
  140. }