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