ParameterBagTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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\Component\HttpFoundation\Tests;
  11. use Symfony\Component\HttpFoundation\ParameterBag;
  12. class ParameterBagTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function testConstructor()
  15. {
  16. $this->testAll();
  17. }
  18. public function testAll()
  19. {
  20. $bag = new ParameterBag(array('foo' => 'bar'));
  21. $this->assertEquals(array('foo' => 'bar'), $bag->all(), '->all() gets all the input');
  22. }
  23. public function testKeys()
  24. {
  25. $bag = new ParameterBag(array('foo' => 'bar'));
  26. $this->assertEquals(array('foo'), $bag->keys());
  27. }
  28. public function testAdd()
  29. {
  30. $bag = new ParameterBag(array('foo' => 'bar'));
  31. $bag->add(array('bar' => 'bas'));
  32. $this->assertEquals(array('foo' => 'bar', 'bar' => 'bas'), $bag->all());
  33. }
  34. public function testRemove()
  35. {
  36. $bag = new ParameterBag(array('foo' => 'bar'));
  37. $bag->add(array('bar' => 'bas'));
  38. $this->assertEquals(array('foo' => 'bar', 'bar' => 'bas'), $bag->all());
  39. $bag->remove('bar');
  40. $this->assertEquals(array('foo' => 'bar'), $bag->all());
  41. }
  42. public function testReplace()
  43. {
  44. $bag = new ParameterBag(array('foo' => 'bar'));
  45. $bag->replace(array('FOO' => 'BAR'));
  46. $this->assertEquals(array('FOO' => 'BAR'), $bag->all(), '->replace() replaces the input with the argument');
  47. $this->assertFalse($bag->has('foo'), '->replace() overrides previously set the input');
  48. }
  49. public function testGet()
  50. {
  51. $bag = new ParameterBag(array('foo' => 'bar', 'null' => null));
  52. $this->assertEquals('bar', $bag->get('foo'), '->get() gets the value of a parameter');
  53. $this->assertEquals('default', $bag->get('unknown', 'default'), '->get() returns second argument as default if a parameter is not defined');
  54. $this->assertNull($bag->get('null', 'default'), '->get() returns null if null is set');
  55. }
  56. public function testGetDoesNotUseDeepByDefault()
  57. {
  58. $bag = new ParameterBag(array('foo' => array('bar' => 'moo')));
  59. $this->assertNull($bag->get('foo[bar]'));
  60. }
  61. /**
  62. * @group legacy
  63. * @dataProvider getInvalidPaths
  64. * @expectedException \InvalidArgumentException
  65. */
  66. public function testGetDeepWithInvalidPaths($path)
  67. {
  68. $bag = new ParameterBag(array('foo' => array('bar' => 'moo')));
  69. $bag->get($path, null, true);
  70. }
  71. public function getInvalidPaths()
  72. {
  73. return array(
  74. array('foo[['),
  75. array('foo[d'),
  76. array('foo[bar]]'),
  77. array('foo[bar]d'),
  78. );
  79. }
  80. /**
  81. * @group legacy
  82. */
  83. public function testGetDeep()
  84. {
  85. $bag = new ParameterBag(array('foo' => array('bar' => array('moo' => 'boo'))));
  86. $this->assertEquals(array('moo' => 'boo'), $bag->get('foo[bar]', null, true));
  87. $this->assertEquals('boo', $bag->get('foo[bar][moo]', null, true));
  88. $this->assertEquals('default', $bag->get('foo[bar][foo]', 'default', true));
  89. $this->assertEquals('default', $bag->get('bar[moo][foo]', 'default', true));
  90. }
  91. public function testSet()
  92. {
  93. $bag = new ParameterBag(array());
  94. $bag->set('foo', 'bar');
  95. $this->assertEquals('bar', $bag->get('foo'), '->set() sets the value of parameter');
  96. $bag->set('foo', 'baz');
  97. $this->assertEquals('baz', $bag->get('foo'), '->set() overrides previously set parameter');
  98. }
  99. public function testHas()
  100. {
  101. $bag = new ParameterBag(array('foo' => 'bar'));
  102. $this->assertTrue($bag->has('foo'), '->has() returns true if a parameter is defined');
  103. $this->assertFalse($bag->has('unknown'), '->has() return false if a parameter is not defined');
  104. }
  105. public function testGetAlpha()
  106. {
  107. $bag = new ParameterBag(array('word' => 'foo_BAR_012'));
  108. $this->assertEquals('fooBAR', $bag->getAlpha('word'), '->getAlpha() gets only alphabetic characters');
  109. $this->assertEquals('', $bag->getAlpha('unknown'), '->getAlpha() returns empty string if a parameter is not defined');
  110. }
  111. public function testGetAlnum()
  112. {
  113. $bag = new ParameterBag(array('word' => 'foo_BAR_012'));
  114. $this->assertEquals('fooBAR012', $bag->getAlnum('word'), '->getAlnum() gets only alphanumeric characters');
  115. $this->assertEquals('', $bag->getAlnum('unknown'), '->getAlnum() returns empty string if a parameter is not defined');
  116. }
  117. public function testGetDigits()
  118. {
  119. $bag = new ParameterBag(array('word' => 'foo_BAR_012'));
  120. $this->assertEquals('012', $bag->getDigits('word'), '->getDigits() gets only digits as string');
  121. $this->assertEquals('', $bag->getDigits('unknown'), '->getDigits() returns empty string if a parameter is not defined');
  122. }
  123. public function testGetInt()
  124. {
  125. $bag = new ParameterBag(array('digits' => '0123'));
  126. $this->assertEquals(123, $bag->getInt('digits'), '->getInt() gets a value of parameter as integer');
  127. $this->assertEquals(0, $bag->getInt('unknown'), '->getInt() returns zero if a parameter is not defined');
  128. }
  129. public function testFilter()
  130. {
  131. $bag = new ParameterBag(array(
  132. 'digits' => '0123ab',
  133. 'email' => 'example@example.com',
  134. 'url' => 'http://example.com/foo',
  135. 'dec' => '256',
  136. 'hex' => '0x100',
  137. 'array' => array('bang'),
  138. ));
  139. $this->assertEmpty($bag->filter('nokey'), '->filter() should return empty by default if no key is found');
  140. $this->assertEquals('0123', $bag->filter('digits', '', FILTER_SANITIZE_NUMBER_INT), '->filter() gets a value of parameter as integer filtering out invalid characters');
  141. $this->assertEquals('example@example.com', $bag->filter('email', '', FILTER_VALIDATE_EMAIL), '->filter() gets a value of parameter as email');
  142. $this->assertEquals('http://example.com/foo', $bag->filter('url', '', FILTER_VALIDATE_URL, array('flags' => FILTER_FLAG_PATH_REQUIRED)), '->filter() gets a value of parameter as URL with a path');
  143. // This test is repeated for code-coverage
  144. $this->assertEquals('http://example.com/foo', $bag->filter('url', '', FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED), '->filter() gets a value of parameter as URL with a path');
  145. $this->assertFalse($bag->filter('dec', '', FILTER_VALIDATE_INT, array(
  146. 'flags' => FILTER_FLAG_ALLOW_HEX,
  147. 'options' => array('min_range' => 1, 'max_range' => 0xff),
  148. )), '->filter() gets a value of parameter as integer between boundaries');
  149. $this->assertFalse($bag->filter('hex', '', FILTER_VALIDATE_INT, array(
  150. 'flags' => FILTER_FLAG_ALLOW_HEX,
  151. 'options' => array('min_range' => 1, 'max_range' => 0xff),
  152. )), '->filter() gets a value of parameter as integer between boundaries');
  153. $this->assertEquals(array('bang'), $bag->filter('array', ''), '->filter() gets a value of parameter as an array');
  154. }
  155. public function testGetIterator()
  156. {
  157. $parameters = array('foo' => 'bar', 'hello' => 'world');
  158. $bag = new ParameterBag($parameters);
  159. $i = 0;
  160. foreach ($bag as $key => $val) {
  161. ++$i;
  162. $this->assertEquals($parameters[$key], $val);
  163. }
  164. $this->assertEquals(count($parameters), $i);
  165. }
  166. public function testCount()
  167. {
  168. $parameters = array('foo' => 'bar', 'hello' => 'world');
  169. $bag = new ParameterBag($parameters);
  170. $this->assertEquals(count($parameters), count($bag));
  171. }
  172. public function testGetBoolean()
  173. {
  174. $parameters = array('string_true' => 'true', 'string_false' => 'false');
  175. $bag = new ParameterBag($parameters);
  176. $this->assertTrue($bag->getBoolean('string_true'), '->getBoolean() gets the string true as boolean true');
  177. $this->assertFalse($bag->getBoolean('string_false'), '->getBoolean() gets the string false as boolean false');
  178. $this->assertFalse($bag->getBoolean('unknown'), '->getBoolean() returns false if a parameter is not defined');
  179. }
  180. }