InputBagTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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\Bridge\PhpUnit\ExpectDeprecationTrait;
  13. use Symfony\Component\HttpFoundation\InputBag;
  14. class InputBagTest extends TestCase
  15. {
  16. use ExpectDeprecationTrait;
  17. public function testGet()
  18. {
  19. $bag = new InputBag(['foo' => 'bar', 'null' => null, 'int' => 1, 'float' => 1.0, 'bool' => false]);
  20. $this->assertSame('bar', $bag->get('foo'), '->get() gets the value of a string parameter');
  21. $this->assertSame('default', $bag->get('unknown', 'default'), '->get() returns second argument as default if a parameter is not defined');
  22. $this->assertNull($bag->get('null', 'default'), '->get() returns null if null is set');
  23. $this->assertSame(1, $bag->get('int'), '->get() gets the value of an int parameter');
  24. $this->assertSame(1.0, $bag->get('float'), '->get() gets the value of a float parameter');
  25. $this->assertFalse($bag->get('bool'), '->get() gets the value of a bool parameter');
  26. }
  27. public function testGetDoesNotUseDeepByDefault()
  28. {
  29. $bag = new InputBag(['foo' => ['bar' => 'moo']]);
  30. $this->assertNull($bag->get('foo[bar]'));
  31. }
  32. public function testFilterArray()
  33. {
  34. $bag = new InputBag([
  35. 'foo' => ['12', '8'],
  36. ]);
  37. $result = $bag->filter('foo', null, \FILTER_VALIDATE_INT, \FILTER_FORCE_ARRAY);
  38. $this->assertSame([12, 8], $result);
  39. }
  40. /**
  41. * @group legacy
  42. */
  43. public function testFilterCallback()
  44. {
  45. $bag = new InputBag(['foo' => 'bar']);
  46. $this->expectDeprecation('Since symfony/http-foundation 5.2: Not passing a Closure together with FILTER_CALLBACK to "Symfony\Component\HttpFoundation\InputBag::filter()" is deprecated. Wrap your filter in a closure instead.');
  47. $this->assertSame('BAR', $bag->filter('foo', null, \FILTER_CALLBACK, ['options' => 'strtoupper']));
  48. }
  49. /**
  50. * @group legacy
  51. */
  52. public function testSetWithNonScalarOrArrayIsDeprecated()
  53. {
  54. $bag = new InputBag();
  55. $this->expectDeprecation('Since symfony/http-foundation 5.1: Passing "Symfony\Component\HttpFoundation\InputBag" as a 2nd Argument to "Symfony\Component\HttpFoundation\InputBag::set()" is deprecated, pass a scalar, array, or null instead.');
  56. $bag->set('foo', new InputBag());
  57. }
  58. /**
  59. * @group legacy
  60. */
  61. public function testGettingANonScalarValueIsDeprecated()
  62. {
  63. $bag = new InputBag(['foo' => ['a', 'b']]);
  64. $this->expectDeprecation('Since symfony/http-foundation 5.1: Retrieving a non-scalar value from "Symfony\Component\HttpFoundation\InputBag::get()" is deprecated, and will throw a "Symfony\Component\HttpFoundation\Exception\BadRequestException" exception in Symfony 6.0, use "Symfony\Component\HttpFoundation\InputBag::all($key)" instead.');
  65. $bag->get('foo');
  66. }
  67. /**
  68. * @group legacy
  69. */
  70. public function testGetWithNonStringDefaultValueIsDeprecated()
  71. {
  72. $bag = new InputBag(['foo' => 'bar']);
  73. $this->expectDeprecation('Since symfony/http-foundation 5.1: Passing a non-scalar value as 2nd argument to "Symfony\Component\HttpFoundation\InputBag::get()" is deprecated, pass a scalar or null instead.');
  74. $bag->get('foo', ['a', 'b']);
  75. }
  76. /**
  77. * @group legacy
  78. */
  79. public function testFilterArrayWithoutArrayFlagIsDeprecated()
  80. {
  81. $bag = new InputBag(['foo' => ['bar', 'baz']]);
  82. $this->expectDeprecation('Since symfony/http-foundation 5.1: Filtering an array value with "Symfony\Component\HttpFoundation\InputBag::filter()" without passing the FILTER_REQUIRE_ARRAY or FILTER_FORCE_ARRAY flag is deprecated');
  83. $bag->filter('foo', \FILTER_VALIDATE_INT);
  84. }
  85. }