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

/vendor/symfony/http-foundation/Tests/ParameterBagTest.php

https://gitlab.com/4gdevs/online-class-record-system
PHP | 224 lines | 163 code | 48 blank | 13 comment | 0 complexity | f61cccb0e0410aefc8d1db9fffb75205 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 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. * @dataProvider getInvalidPaths
  63. * @expectedException \InvalidArgumentException
  64. */
  65. public function testGetDeepWithInvalidPaths($path)
  66. {
  67. $bag = new ParameterBag(array('foo' => array('bar' => 'moo')));
  68. $bag->get($path, null, true);
  69. }
  70. public function getInvalidPaths()
  71. {
  72. return array(
  73. array('foo[['),
  74. array('foo[d'),
  75. array('foo[bar]]'),
  76. array('foo[bar]d'),
  77. );
  78. }
  79. public function testGetDeep()
  80. {
  81. $bag = new ParameterBag(array('foo' => array('bar' => array('moo' => 'boo'))));
  82. $this->assertEquals(array('moo' => 'boo'), $bag->get('foo[bar]', null, true));
  83. $this->assertEquals('boo', $bag->get('foo[bar][moo]', null, true));
  84. $this->assertEquals('default', $bag->get('foo[bar][foo]', 'default', true));
  85. $this->assertEquals('default', $bag->get('bar[moo][foo]', 'default', true));
  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. public function testHas()
  96. {
  97. $bag = new ParameterBag(array('foo' => 'bar'));
  98. $this->assertTrue($bag->has('foo'), '->has() returns true if a parameter is defined');
  99. $this->assertFalse($bag->has('unknown'), '->has() return false if a parameter is not defined');
  100. }
  101. public function testGetAlpha()
  102. {
  103. $bag = new ParameterBag(array('word' => 'foo_BAR_012'));
  104. $this->assertEquals('fooBAR', $bag->getAlpha('word'), '->getAlpha() gets only alphabetic characters');
  105. $this->assertEquals('', $bag->getAlpha('unknown'), '->getAlpha() returns empty string if a parameter is not defined');
  106. }
  107. public function testGetAlnum()
  108. {
  109. $bag = new ParameterBag(array('word' => 'foo_BAR_012'));
  110. $this->assertEquals('fooBAR012', $bag->getAlnum('word'), '->getAlnum() gets only alphanumeric characters');
  111. $this->assertEquals('', $bag->getAlnum('unknown'), '->getAlnum() returns empty string if a parameter is not defined');
  112. }
  113. public function testGetDigits()
  114. {
  115. $bag = new ParameterBag(array('word' => 'foo_BAR_012'));
  116. $this->assertEquals('012', $bag->getDigits('word'), '->getDigits() gets only digits as string');
  117. $this->assertEquals('', $bag->getDigits('unknown'), '->getDigits() returns empty string if a parameter is not defined');
  118. }
  119. public function testGetInt()
  120. {
  121. $bag = new ParameterBag(array('digits' => '0123'));
  122. $this->assertEquals(123, $bag->getInt('digits'), '->getInt() gets a value of parameter as integer');
  123. $this->assertEquals(0, $bag->getInt('unknown'), '->getInt() returns zero if a parameter is not defined');
  124. }
  125. public function testFilter()
  126. {
  127. $bag = new ParameterBag(array(
  128. 'digits' => '0123ab',
  129. 'email' => 'example@example.com',
  130. 'url' => 'http://example.com/foo',
  131. 'dec' => '256',
  132. 'hex' => '0x100',
  133. 'array' => array('bang'),
  134. ));
  135. $this->assertEmpty($bag->filter('nokey'), '->filter() should return empty by default if no key is found');
  136. $this->assertEquals('0123', $bag->filter('digits', '', false, FILTER_SANITIZE_NUMBER_INT), '->filter() gets a value of parameter as integer filtering out invalid characters');
  137. $this->assertEquals('example@example.com', $bag->filter('email', '', false, FILTER_VALIDATE_EMAIL), '->filter() gets a value of parameter as email');
  138. $this->assertEquals('http://example.com/foo', $bag->filter('url', '', false, FILTER_VALIDATE_URL, array('flags' => FILTER_FLAG_PATH_REQUIRED)), '->filter() gets a value of parameter as URL with a path');
  139. // This test is repeated for code-coverage
  140. $this->assertEquals('http://example.com/foo', $bag->filter('url', '', false, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED), '->filter() gets a value of parameter as URL with a path');
  141. $this->assertFalse($bag->filter('dec', '', false, FILTER_VALIDATE_INT, array(
  142. 'flags' => FILTER_FLAG_ALLOW_HEX,
  143. 'options' => array('min_range' => 1, 'max_range' => 0xff),
  144. )), '->filter() gets a value of parameter as integer between boundaries');
  145. $this->assertFalse($bag->filter('hex', '', false, 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->assertEquals(array('bang'), $bag->filter('array', '', false), '->filter() gets a value of parameter as an array');
  150. }
  151. public function testGetIterator()
  152. {
  153. $parameters = array('foo' => 'bar', 'hello' => 'world');
  154. $bag = new ParameterBag($parameters);
  155. $i = 0;
  156. foreach ($bag as $key => $val) {
  157. ++$i;
  158. $this->assertEquals($parameters[$key], $val);
  159. }
  160. $this->assertEquals(count($parameters), $i);
  161. }
  162. public function testCount()
  163. {
  164. $parameters = array('foo' => 'bar', 'hello' => 'world');
  165. $bag = new ParameterBag($parameters);
  166. $this->assertEquals(count($parameters), count($bag));
  167. }
  168. public function testGetBoolean()
  169. {
  170. $parameters = array('string_true' => 'true', 'string_false' => 'false');
  171. $bag = new ParameterBag($parameters);
  172. $this->assertTrue($bag->getBoolean('string_true'), '->getBoolean() gets the string true as boolean true');
  173. $this->assertFalse($bag->getBoolean('string_false'), '->getBoolean() gets the string false as boolean false');
  174. $this->assertFalse($bag->getBoolean('unknown'), '->getBoolean() returns false if a parameter is not defined');
  175. }
  176. }