ParameterBagTest.php 7.2 KB

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