PageRenderTime 25ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Symfony/Component/HttpFoundation/Tests/ParameterBagTest.php

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