ParameterBagTest.php 8.8 KB

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