ParameterBagTest.php 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. public function testSet()
  63. {
  64. $bag = new ParameterBag(array());
  65. $bag->set('foo', 'bar');
  66. $this->assertEquals('bar', $bag->get('foo'), '->set() sets the value of parameter');
  67. $bag->set('foo', 'baz');
  68. $this->assertEquals('baz', $bag->get('foo'), '->set() overrides previously set parameter');
  69. }
  70. public function testHas()
  71. {
  72. $bag = new ParameterBag(array('foo' => 'bar'));
  73. $this->assertTrue($bag->has('foo'), '->has() returns true if a parameter is defined');
  74. $this->assertFalse($bag->has('unknown'), '->has() return false if a parameter is not defined');
  75. }
  76. public function testGetAlpha()
  77. {
  78. $bag = new ParameterBag(array('word' => 'foo_BAR_012'));
  79. $this->assertEquals('fooBAR', $bag->getAlpha('word'), '->getAlpha() gets only alphabetic characters');
  80. $this->assertEquals('', $bag->getAlpha('unknown'), '->getAlpha() returns empty string if a parameter is not defined');
  81. }
  82. public function testGetAlnum()
  83. {
  84. $bag = new ParameterBag(array('word' => 'foo_BAR_012'));
  85. $this->assertEquals('fooBAR012', $bag->getAlnum('word'), '->getAlnum() gets only alphanumeric characters');
  86. $this->assertEquals('', $bag->getAlnum('unknown'), '->getAlnum() returns empty string if a parameter is not defined');
  87. }
  88. public function testGetDigits()
  89. {
  90. $bag = new ParameterBag(array('word' => 'foo_BAR_012'));
  91. $this->assertEquals('012', $bag->getDigits('word'), '->getDigits() gets only digits as string');
  92. $this->assertEquals('', $bag->getDigits('unknown'), '->getDigits() returns empty string if a parameter is not defined');
  93. }
  94. public function testGetInt()
  95. {
  96. $bag = new ParameterBag(array('digits' => '0123'));
  97. $this->assertEquals(123, $bag->getInt('digits'), '->getInt() gets a value of parameter as integer');
  98. $this->assertEquals(0, $bag->getInt('unknown'), '->getInt() returns zero if a parameter is not defined');
  99. }
  100. public function testFilter()
  101. {
  102. $bag = new ParameterBag(array(
  103. 'digits' => '0123ab',
  104. 'email' => 'example@example.com',
  105. 'url' => 'http://example.com/foo',
  106. 'dec' => '256',
  107. 'hex' => '0x100',
  108. 'array' => array('bang'),
  109. ));
  110. $this->assertEmpty($bag->filter('nokey'), '->filter() should return empty by default if no key is found');
  111. $this->assertEquals('0123', $bag->filter('digits', '', FILTER_SANITIZE_NUMBER_INT), '->filter() gets a value of parameter as integer filtering out invalid characters');
  112. $this->assertEquals('example@example.com', $bag->filter('email', '', FILTER_VALIDATE_EMAIL), '->filter() gets a value of parameter as email');
  113. $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');
  114. // This test is repeated for code-coverage
  115. $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');
  116. $this->assertFalse($bag->filter('dec', '', FILTER_VALIDATE_INT, array(
  117. 'flags' => FILTER_FLAG_ALLOW_HEX,
  118. 'options' => array('min_range' => 1, 'max_range' => 0xff),
  119. )), '->filter() gets a value of parameter as integer between boundaries');
  120. $this->assertFalse($bag->filter('hex', '', FILTER_VALIDATE_INT, array(
  121. 'flags' => FILTER_FLAG_ALLOW_HEX,
  122. 'options' => array('min_range' => 1, 'max_range' => 0xff),
  123. )), '->filter() gets a value of parameter as integer between boundaries');
  124. $this->assertEquals(array('bang'), $bag->filter('array', ''), '->filter() gets a value of parameter as an array');
  125. }
  126. public function testGetIterator()
  127. {
  128. $parameters = array('foo' => 'bar', 'hello' => 'world');
  129. $bag = new ParameterBag($parameters);
  130. $i = 0;
  131. foreach ($bag as $key => $val) {
  132. ++$i;
  133. $this->assertEquals($parameters[$key], $val);
  134. }
  135. $this->assertEquals(count($parameters), $i);
  136. }
  137. public function testCount()
  138. {
  139. $parameters = array('foo' => 'bar', 'hello' => 'world');
  140. $bag = new ParameterBag($parameters);
  141. $this->assertCount(count($parameters), $bag);
  142. }
  143. public function testGetBoolean()
  144. {
  145. $parameters = array('string_true' => 'true', 'string_false' => 'false');
  146. $bag = new ParameterBag($parameters);
  147. $this->assertTrue($bag->getBoolean('string_true'), '->getBoolean() gets the string true as boolean true');
  148. $this->assertFalse($bag->getBoolean('string_false'), '->getBoolean() gets the string false as boolean false');
  149. $this->assertFalse($bag->getBoolean('unknown'), '->getBoolean() returns false if a parameter is not defined');
  150. }
  151. }