diff --git a/.github/expected-missing-return-types.diff b/.github/expected-missing-return-types.diff index bd455327ec038..6615dd6f12cc7 100644 --- a/.github/expected-missing-return-types.diff +++ b/.github/expected-missing-return-types.diff @@ -7391,17 +7391,6 @@ index 71e806fc15..d60290b3ed 100644 + public static function trustXSendfileTypeHeader(): void { self::$trustXSendfileTypeHeader = true; -diff --git a/src/Symfony/Component/HttpFoundation/ExpressionRequestMatcher.php b/src/Symfony/Component/HttpFoundation/ExpressionRequestMatcher.php -index fe65e920d9..6a78e6e779 100644 ---- a/src/Symfony/Component/HttpFoundation/ExpressionRequestMatcher.php -+++ b/src/Symfony/Component/HttpFoundation/ExpressionRequestMatcher.php -@@ -33,5 +33,5 @@ class ExpressionRequestMatcher extends RequestMatcher - * @return void - */ -- public function setExpression(ExpressionLanguage $language, Expression|string $expression) -+ public function setExpression(ExpressionLanguage $language, Expression|string $expression): void - { - $this->language = $language; diff --git a/src/Symfony/Component/HttpFoundation/FileBag.php b/src/Symfony/Component/HttpFoundation/FileBag.php index b74a02e2e1..51b4f7f1c3 100644 --- a/src/Symfony/Component/HttpFoundation/FileBag.php @@ -7621,66 +7610,6 @@ index 0bef6f8d70..ca99fd9dad 100644 + protected static function initializeFormats(): void { static::$formats = [ -diff --git a/src/Symfony/Component/HttpFoundation/RequestMatcher.php b/src/Symfony/Component/HttpFoundation/RequestMatcher.php -index 8c5f1d8134..fdd3a666e9 100644 ---- a/src/Symfony/Component/HttpFoundation/RequestMatcher.php -+++ b/src/Symfony/Component/HttpFoundation/RequestMatcher.php -@@ -73,5 +73,5 @@ class RequestMatcher implements RequestMatcherInterface - * @return void - */ -- public function matchScheme(string|array|null $scheme) -+ public function matchScheme(string|array|null $scheme): void - { - $this->schemes = null !== $scheme ? array_map('strtolower', (array) $scheme) : []; -@@ -83,5 +83,5 @@ class RequestMatcher implements RequestMatcherInterface - * @return void - */ -- public function matchHost(?string $regexp) -+ public function matchHost(?string $regexp): void - { - $this->host = $regexp; -@@ -95,5 +95,5 @@ class RequestMatcher implements RequestMatcherInterface - * @return void - */ -- public function matchPort(?int $port) -+ public function matchPort(?int $port): void - { - $this->port = $port; -@@ -105,5 +105,5 @@ class RequestMatcher implements RequestMatcherInterface - * @return void - */ -- public function matchPath(?string $regexp) -+ public function matchPath(?string $regexp): void - { - $this->path = $regexp; -@@ -117,5 +117,5 @@ class RequestMatcher implements RequestMatcherInterface - * @return void - */ -- public function matchIp(string $ip) -+ public function matchIp(string $ip): void - { - $this->matchIps($ip); -@@ -129,5 +129,5 @@ class RequestMatcher implements RequestMatcherInterface - * @return void - */ -- public function matchIps(string|array|null $ips) -+ public function matchIps(string|array|null $ips): void - { - $ips = null !== $ips ? (array) $ips : []; -@@ -143,5 +143,5 @@ class RequestMatcher implements RequestMatcherInterface - * @return void - */ -- public function matchMethod(string|array|null $method) -+ public function matchMethod(string|array|null $method): void - { - $this->methods = null !== $method ? array_map('strtoupper', (array) $method) : []; -@@ -153,5 +153,5 @@ class RequestMatcher implements RequestMatcherInterface - * @return void - */ -- public function matchAttribute(string $key, string $regexp) -+ public function matchAttribute(string $key, string $regexp): void - { - $this->attributes[$key] = $regexp; diff --git a/src/Symfony/Component/HttpFoundation/RequestStack.php b/src/Symfony/Component/HttpFoundation/RequestStack.php index 5aa8ba7934..80742b0764 100644 --- a/src/Symfony/Component/HttpFoundation/RequestStack.php diff --git a/UPGRADE-7.0.md b/UPGRADE-7.0.md index 672a620aeaa51..ad17a52193d6c 100644 --- a/UPGRADE-7.0.md +++ b/UPGRADE-7.0.md @@ -22,6 +22,19 @@ DoctrineBridge * DoctrineBridge now requires `doctrine/event-manager:^2` * Add parameter `$isSameDatabase` to `DoctrineTokenProvider::configureSchema()` +HttpFoundation +-------------- + + * Calling `ParameterBag::filter()` on an invalid value throws an `UnexpectedValueException` instead of returning `false`. + The exception is more specific for `InputBag` which throws a `BadRequestException` when invalid value is found. + The flag `FILTER_NULL_ON_FAILURE` can be used to return `null` instead of throwing an exception. + * The methods `ParameterBag::getInt()` and `ParameterBag::getBool()` no longer fallback to `0` or `false` + when the value cannot be converted to the expected type. They throw a `UnexpectedValueException` instead. + * Replace `RequestMatcher` with `ChainRequestMatcher` + * Replace `ExpressionRequestMatcher` with `RequestMatcher\ExpressionRequestMatcher` + * Remove `Request::getContentType()`, use `Request::getContentTypeFormat()` instead + * Throw an `InvalidArgumentException` when calling `Request::create()` with a malformed URI + Lock ---- diff --git a/src/Symfony/Component/HttpFoundation/CHANGELOG.md b/src/Symfony/Component/HttpFoundation/CHANGELOG.md index 5f1f6d5ce86a1..2d3669d2f0b17 100644 --- a/src/Symfony/Component/HttpFoundation/CHANGELOG.md +++ b/src/Symfony/Component/HttpFoundation/CHANGELOG.md @@ -1,6 +1,15 @@ CHANGELOG ========= +7.0 +--- + + * Calling `ParameterBag::filter()` throws an `UnexpectedValueException` on invalid value, unless flag `FILTER_NULL_ON_FAILURE` is set + * Calling `ParameterBag::getInt()` and `ParameterBag::getBool()` throws an `UnexpectedValueException` on invalid value + * Remove classes `RequestMatcher` and `ExpressionRequestMatcher` + * Remove `Request::getContentType()`, use `Request::getContentTypeFormat()` instead + * Throw an `InvalidArgumentException` when calling `Request::create()` with a malformed URI + 6.4 --- diff --git a/src/Symfony/Component/HttpFoundation/ExpressionRequestMatcher.php b/src/Symfony/Component/HttpFoundation/ExpressionRequestMatcher.php deleted file mode 100644 index fe65e920d92c0..0000000000000 --- a/src/Symfony/Component/HttpFoundation/ExpressionRequestMatcher.php +++ /dev/null @@ -1,56 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\HttpFoundation; - -use Symfony\Component\ExpressionLanguage\Expression; -use Symfony\Component\ExpressionLanguage\ExpressionLanguage; -use Symfony\Component\HttpFoundation\RequestMatcher\ExpressionRequestMatcher as NewExpressionRequestMatcher; - -trigger_deprecation('symfony/http-foundation', '6.2', 'The "%s" class is deprecated, use "%s" instead.', ExpressionRequestMatcher::class, NewExpressionRequestMatcher::class); - -/** - * ExpressionRequestMatcher uses an expression to match a Request. - * - * @author Fabien Potencier - * - * @deprecated since Symfony 6.2, use "Symfony\Component\HttpFoundation\RequestMatcher\ExpressionRequestMatcher" instead - */ -class ExpressionRequestMatcher extends RequestMatcher -{ - private ExpressionLanguage $language; - private Expression|string $expression; - - /** - * @return void - */ - public function setExpression(ExpressionLanguage $language, Expression|string $expression) - { - $this->language = $language; - $this->expression = $expression; - } - - public function matches(Request $request): bool - { - if (!isset($this->language)) { - throw new \LogicException('Unable to match the request as the expression language is not available. Try running "composer require symfony/expression-language".'); - } - - return $this->language->evaluate($this->expression, [ - 'request' => $request, - 'method' => $request->getMethod(), - 'path' => rawurldecode($request->getPathInfo()), - 'host' => $request->getHost(), - 'ip' => $request->getClientIp(), - 'attributes' => $request->attributes->all(), - ]) && parent::matches($request); - } -} diff --git a/src/Symfony/Component/HttpFoundation/InputBag.php b/src/Symfony/Component/HttpFoundation/InputBag.php index 77990f5711ece..38881700070dd 100644 --- a/src/Symfony/Component/HttpFoundation/InputBag.php +++ b/src/Symfony/Component/HttpFoundation/InputBag.php @@ -128,12 +128,6 @@ public function filter(string $key, mixed $default = null, int $filter = \FILTER return $value; } - $method = debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS | \DEBUG_BACKTRACE_PROVIDE_OBJECT, 2)[1]; - $method = ($method['object'] ?? null) === $this ? $method['function'] : 'filter'; - $hint = 'filter' === $method ? 'pass' : 'use method "filter()" with'; - - trigger_deprecation('symfony/http-foundation', '6.3', 'Ignoring invalid values when using "%s::%s(\'%s\')" is deprecated and will throw a "%s" in 7.0; '.$hint.' flag "FILTER_NULL_ON_FAILURE" to keep ignoring them.', $this::class, $method, $key, BadRequestException::class); - - return false; + throw new BadRequestException(sprintf('Input value "%s" is invalid and flag "FILTER_NULL_ON_FAILURE" was not set.', $key)); } } diff --git a/src/Symfony/Component/HttpFoundation/ParameterBag.php b/src/Symfony/Component/HttpFoundation/ParameterBag.php index 9d7012de35d30..998f16a1cd5fe 100644 --- a/src/Symfony/Component/HttpFoundation/ParameterBag.php +++ b/src/Symfony/Component/HttpFoundation/ParameterBag.php @@ -151,8 +151,7 @@ public function getString(string $key, string $default = ''): string */ public function getInt(string $key, int $default = 0): int { - // In 7.0 remove the fallback to 0, in case of failure an exception will be thrown - return $this->filter($key, $default, \FILTER_VALIDATE_INT, ['flags' => \FILTER_REQUIRE_SCALAR]) ?: 0; + return $this->filter($key, $default, \FILTER_VALIDATE_INT, ['flags' => \FILTER_REQUIRE_SCALAR]); } /** @@ -228,13 +227,7 @@ public function filter(string $key, mixed $default = null, int $filter = \FILTER return $value; } - $method = debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS | \DEBUG_BACKTRACE_PROVIDE_OBJECT, 2)[1]; - $method = ($method['object'] ?? null) === $this ? $method['function'] : 'filter'; - $hint = 'filter' === $method ? 'pass' : 'use method "filter()" with'; - - trigger_deprecation('symfony/http-foundation', '6.3', 'Ignoring invalid values when using "%s::%s(\'%s\')" is deprecated and will throw an "%s" in 7.0; '.$hint.' flag "FILTER_NULL_ON_FAILURE" to keep ignoring them.', $this::class, $method, $key, \UnexpectedValueException::class); - - return false; + throw new \UnexpectedValueException(sprintf('Parameter value "%s" is invalid and flag "FILTER_NULL_ON_FAILURE" was not set.', $key)); } /** diff --git a/src/Symfony/Component/HttpFoundation/Request.php b/src/Symfony/Component/HttpFoundation/Request.php index 0bef6f8d70796..3df05302bf4c4 100644 --- a/src/Symfony/Component/HttpFoundation/Request.php +++ b/src/Symfony/Component/HttpFoundation/Request.php @@ -345,8 +345,7 @@ public static function create(string $uri, string $method = 'GET', array $parame $components = parse_url($uri); if (false === $components) { - trigger_deprecation('symfony/http-foundation', '6.3', 'Calling "%s()" with an invalid URI is deprecated.', __METHOD__); - $components = []; + throw new \InvalidArgumentException(sprintf('Malformed URI "%s".', $uri)); } if (isset($components['host'])) { $server['SERVER_NAME'] = $components['host']; @@ -1337,18 +1336,6 @@ public function setRequestFormat(?string $format) $this->format = $format; } - /** - * Gets the usual name of the format associated with the request's media type (provided in the Content-Type header). - * - * @deprecated since Symfony 6.2, use getContentTypeFormat() instead - */ - public function getContentType(): ?string - { - trigger_deprecation('symfony/http-foundation', '6.2', 'The "%s()" method is deprecated, use "getContentTypeFormat()" instead.', __METHOD__); - - return $this->getContentTypeFormat(); - } - /** * Gets the usual name of the format associated with the request's media type (provided in the Content-Type header). * diff --git a/src/Symfony/Component/HttpFoundation/RequestMatcher.php b/src/Symfony/Component/HttpFoundation/RequestMatcher.php deleted file mode 100644 index 8c5f1d8134635..0000000000000 --- a/src/Symfony/Component/HttpFoundation/RequestMatcher.php +++ /dev/null @@ -1,200 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\HttpFoundation; - -trigger_deprecation('symfony/http-foundation', '6.2', 'The "%s" class is deprecated, use "%s" instead.', RequestMatcher::class, ChainRequestMatcher::class); - -/** - * RequestMatcher compares a pre-defined set of checks against a Request instance. - * - * @author Fabien Potencier - * - * @deprecated since Symfony 6.2, use ChainRequestMatcher instead - */ -class RequestMatcher implements RequestMatcherInterface -{ - private ?string $path = null; - private ?string $host = null; - private ?int $port = null; - - /** - * @var string[] - */ - private array $methods = []; - - /** - * @var string[] - */ - private array $ips = []; - - /** - * @var string[] - */ - private array $attributes = []; - - /** - * @var string[] - */ - private array $schemes = []; - - /** - * @param string|string[]|null $methods - * @param string|string[]|null $ips - * @param string|string[]|null $schemes - */ - public function __construct(string $path = null, string $host = null, string|array $methods = null, string|array $ips = null, array $attributes = [], string|array $schemes = null, int $port = null) - { - $this->matchPath($path); - $this->matchHost($host); - $this->matchMethod($methods); - $this->matchIps($ips); - $this->matchScheme($schemes); - $this->matchPort($port); - - foreach ($attributes as $k => $v) { - $this->matchAttribute($k, $v); - } - } - - /** - * Adds a check for the HTTP scheme. - * - * @param string|string[]|null $scheme An HTTP scheme or an array of HTTP schemes - * - * @return void - */ - public function matchScheme(string|array|null $scheme) - { - $this->schemes = null !== $scheme ? array_map('strtolower', (array) $scheme) : []; - } - - /** - * Adds a check for the URL host name. - * - * @return void - */ - public function matchHost(?string $regexp) - { - $this->host = $regexp; - } - - /** - * Adds a check for the the URL port. - * - * @param int|null $port The port number to connect to - * - * @return void - */ - public function matchPort(?int $port) - { - $this->port = $port; - } - - /** - * Adds a check for the URL path info. - * - * @return void - */ - public function matchPath(?string $regexp) - { - $this->path = $regexp; - } - - /** - * Adds a check for the client IP. - * - * @param string $ip A specific IP address or a range specified using IP/netmask like 192.168.1.0/24 - * - * @return void - */ - public function matchIp(string $ip) - { - $this->matchIps($ip); - } - - /** - * Adds a check for the client IP. - * - * @param string|string[]|null $ips A specific IP address or a range specified using IP/netmask like 192.168.1.0/24 - * - * @return void - */ - public function matchIps(string|array|null $ips) - { - $ips = null !== $ips ? (array) $ips : []; - - $this->ips = array_reduce($ips, static fn (array $ips, string $ip) => array_merge($ips, preg_split('/\s*,\s*/', $ip)), []); - } - - /** - * Adds a check for the HTTP method. - * - * @param string|string[]|null $method An HTTP method or an array of HTTP methods - * - * @return void - */ - public function matchMethod(string|array|null $method) - { - $this->methods = null !== $method ? array_map('strtoupper', (array) $method) : []; - } - - /** - * Adds a check for request attribute. - * - * @return void - */ - public function matchAttribute(string $key, string $regexp) - { - $this->attributes[$key] = $regexp; - } - - public function matches(Request $request): bool - { - if ($this->schemes && !\in_array($request->getScheme(), $this->schemes, true)) { - return false; - } - - if ($this->methods && !\in_array($request->getMethod(), $this->methods, true)) { - return false; - } - - foreach ($this->attributes as $key => $pattern) { - $requestAttribute = $request->attributes->get($key); - if (!\is_string($requestAttribute)) { - return false; - } - if (!preg_match('{'.$pattern.'}', $requestAttribute)) { - return false; - } - } - - if (null !== $this->path && !preg_match('{'.$this->path.'}', rawurldecode($request->getPathInfo()))) { - return false; - } - - if (null !== $this->host && !preg_match('{'.$this->host.'}i', $request->getHost())) { - return false; - } - - if (null !== $this->port && 0 < $this->port && $request->getPort() !== $this->port) { - return false; - } - - if (IpUtils::checkIp($request->getClientIp() ?? '', $this->ips)) { - return true; - } - - // Note to future implementors: add additional checks above the - // foreach above or else your check might not be run! - return 0 === \count($this->ips); - } -} diff --git a/src/Symfony/Component/HttpFoundation/Tests/ExpressionRequestMatcherTest.php b/src/Symfony/Component/HttpFoundation/Tests/ExpressionRequestMatcherTest.php deleted file mode 100644 index 02917f3a3ff74..0000000000000 --- a/src/Symfony/Component/HttpFoundation/Tests/ExpressionRequestMatcherTest.php +++ /dev/null @@ -1,70 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\HttpFoundation\Tests; - -use PHPUnit\Framework\TestCase; -use Symfony\Component\ExpressionLanguage\ExpressionLanguage; -use Symfony\Component\HttpFoundation\ExpressionRequestMatcher; -use Symfony\Component\HttpFoundation\Request; - -/** - * @group legacy - */ -class ExpressionRequestMatcherTest extends TestCase -{ - public function testWhenNoExpressionIsSet() - { - $this->expectException(\LogicException::class); - $expressionRequestMatcher = new ExpressionRequestMatcher(); - $expressionRequestMatcher->matches(new Request()); - } - - /** - * @dataProvider provideExpressions - */ - public function testMatchesWhenParentMatchesIsTrue($expression, $expected) - { - $request = Request::create('/foo'); - $expressionRequestMatcher = new ExpressionRequestMatcher(); - - $expressionRequestMatcher->setExpression(new ExpressionLanguage(), $expression); - $this->assertSame($expected, $expressionRequestMatcher->matches($request)); - } - - /** - * @dataProvider provideExpressions - */ - public function testMatchesWhenParentMatchesIsFalse($expression) - { - $request = Request::create('/foo'); - $request->attributes->set('foo', 'foo'); - $expressionRequestMatcher = new ExpressionRequestMatcher(); - $expressionRequestMatcher->matchAttribute('foo', 'bar'); - - $expressionRequestMatcher->setExpression(new ExpressionLanguage(), $expression); - $this->assertFalse($expressionRequestMatcher->matches($request)); - } - - public static function provideExpressions() - { - return [ - ['request.getMethod() == method', true], - ['request.getPathInfo() == path', true], - ['request.getHost() == host', true], - ['request.getClientIp() == ip', true], - ['request.attributes.all() == attributes', true], - ['request.getMethod() == method && request.getPathInfo() == path && request.getHost() == host && request.getClientIp() == ip && request.attributes.all() == attributes', true], - ['request.getMethod() != method', false], - ['request.getMethod() != method && request.getPathInfo() == path && request.getHost() == host && request.getClientIp() == ip && request.attributes.all() == attributes', false], - ]; - } -} diff --git a/src/Symfony/Component/HttpFoundation/Tests/InputBagTest.php b/src/Symfony/Component/HttpFoundation/Tests/InputBagTest.php index 21b108ceb949f..6bb4285d1b32e 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/InputBagTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/InputBagTest.php @@ -12,15 +12,12 @@ namespace Symfony\Component\HttpFoundation\Tests; use PHPUnit\Framework\TestCase; -use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait; use Symfony\Component\HttpFoundation\Exception\BadRequestException; use Symfony\Component\HttpFoundation\InputBag; use Symfony\Component\HttpFoundation\Tests\Fixtures\FooEnum; class InputBagTest extends TestCase { - use ExpectDeprecationTrait; - public function testGet() { $bag = new InputBag(['foo' => 'bar', 'null' => null, 'int' => 1, 'float' => 1.0, 'bool' => false, 'stringable' => new class() implements \Stringable { @@ -39,28 +36,24 @@ public function __toString(): string $this->assertFalse($bag->get('bool'), '->get() gets the value of a bool parameter'); } - /** - * @group legacy - */ public function testGetIntError() { - $this->expectDeprecation('Since symfony/http-foundation 6.3: Ignoring invalid values when using "Symfony\Component\HttpFoundation\InputBag::getInt(\'foo\')" is deprecated and will throw a "Symfony\Component\HttpFoundation\Exception\BadRequestException" in 7.0; use method "filter()" with flag "FILTER_NULL_ON_FAILURE" to keep ignoring them.'); - $bag = new InputBag(['foo' => 'bar']); - $result = $bag->getInt('foo'); - $this->assertSame(0, $result); + + $this->expectException(BadRequestException::class); + $this->expectExceptionMessage('Input value "foo" is invalid and flag "FILTER_NULL_ON_FAILURE" was not set.'); + + $bag->getInt('foo'); } - /** - * @group legacy - */ public function testGetBooleanError() { - $this->expectDeprecation('Since symfony/http-foundation 6.3: Ignoring invalid values when using "Symfony\Component\HttpFoundation\InputBag::getBoolean(\'foo\')" is deprecated and will throw a "Symfony\Component\HttpFoundation\Exception\BadRequestException" in 7.0; use method "filter()" with flag "FILTER_NULL_ON_FAILURE" to keep ignoring them.'); - $bag = new InputBag(['foo' => 'bar']); - $result = $bag->getBoolean('foo'); - $this->assertFalse($result); + + $this->expectException(BadRequestException::class); + $this->expectExceptionMessage('Input value "foo" is invalid and flag "FILTER_NULL_ON_FAILURE" was not set.'); + + $bag->getBoolean('foo'); } public function testGetString() diff --git a/src/Symfony/Component/HttpFoundation/Tests/ParameterBagTest.php b/src/Symfony/Component/HttpFoundation/Tests/ParameterBagTest.php index e4c911d9a4ff4..a05d244b1e212 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/ParameterBagTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/ParameterBagTest.php @@ -12,15 +12,12 @@ namespace Symfony\Component\HttpFoundation\Tests; use PHPUnit\Framework\TestCase; -use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait; use Symfony\Component\HttpFoundation\Exception\BadRequestException; use Symfony\Component\HttpFoundation\ParameterBag; use Symfony\Component\HttpFoundation\Tests\Fixtures\FooEnum; class ParameterBagTest extends TestCase { - use ExpectDeprecationTrait; - public function testConstructor() { $this->testAll(); @@ -186,28 +183,24 @@ public function testGetInt() $this->assertSame(1, $bag->getInt('bool', 0), '->getInt() returns 1 if a parameter is true'); } - /** - * @group legacy - */ public function testGetIntExceptionWithArray() { - $this->expectDeprecation('Since symfony/http-foundation 6.3: Ignoring invalid values when using "Symfony\Component\HttpFoundation\ParameterBag::getInt(\'digits\')" is deprecated and will throw an "UnexpectedValueException" in 7.0; use method "filter()" with flag "FILTER_NULL_ON_FAILURE" to keep ignoring them.'); - $bag = new ParameterBag(['digits' => ['123']]); - $result = $bag->getInt('digits', 0); - $this->assertSame(0, $result); + + $this->expectException(\UnexpectedValueException::class); + $this->expectExceptionMessage('Parameter value "digits" is invalid and flag "FILTER_NULL_ON_FAILURE" was not set.'); + + $bag->getInt('digits'); } - /** - * @group legacy - */ public function testGetIntExceptionWithInvalid() { - $this->expectDeprecation('Since symfony/http-foundation 6.3: Ignoring invalid values when using "Symfony\Component\HttpFoundation\ParameterBag::getInt(\'word\')" is deprecated and will throw an "UnexpectedValueException" in 7.0; use method "filter()" with flag "FILTER_NULL_ON_FAILURE" to keep ignoring them.'); - $bag = new ParameterBag(['word' => 'foo_BAR_012']); - $result = $bag->getInt('word', 0); - $this->assertSame(0, $result); + + $this->expectException(\UnexpectedValueException::class); + $this->expectExceptionMessage('Parameter value "word" is invalid and flag "FILTER_NULL_ON_FAILURE" was not set.'); + + $bag->getInt('word'); } public function testGetString() @@ -333,16 +326,14 @@ public function testGetBoolean() $this->assertTrue($bag->getBoolean('unknown', true), '->getBoolean() returns default if a parameter is not defined'); } - /** - * @group legacy - */ public function testGetBooleanExceptionWithInvalid() { - $this->expectDeprecation('Since symfony/http-foundation 6.3: Ignoring invalid values when using "Symfony\Component\HttpFoundation\ParameterBag::getBoolean(\'invalid\')" is deprecated and will throw an "UnexpectedValueException" in 7.0; use method "filter()" with flag "FILTER_NULL_ON_FAILURE" to keep ignoring them.'); - $bag = new ParameterBag(['invalid' => 'foo']); - $result = $bag->getBoolean('invalid', 0); - $this->assertFalse($result); + + $this->expectException(\UnexpectedValueException::class); + $this->expectExceptionMessage('Parameter value "invalid" is invalid and flag "FILTER_NULL_ON_FAILURE" was not set.'); + + $bag->getBoolean('invalid'); } public function testGetEnum() diff --git a/src/Symfony/Component/HttpFoundation/Tests/RequestMatcherTest.php b/src/Symfony/Component/HttpFoundation/Tests/RequestMatcherTest.php deleted file mode 100644 index cda2b1f2334d6..0000000000000 --- a/src/Symfony/Component/HttpFoundation/Tests/RequestMatcherTest.php +++ /dev/null @@ -1,215 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\HttpFoundation\Tests; - -use PHPUnit\Framework\TestCase; -use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\RequestMatcher; -use Symfony\Component\HttpFoundation\Response; - -/** - * @group legacy - */ -class RequestMatcherTest extends TestCase -{ - /** - * @dataProvider getMethodData - */ - public function testMethod($requestMethod, $matcherMethod, $isMatch) - { - $matcher = new RequestMatcher(); - $matcher->matchMethod($matcherMethod); - $request = Request::create('', $requestMethod); - $this->assertSame($isMatch, $matcher->matches($request)); - - $matcher = new RequestMatcher(null, null, $matcherMethod); - $request = Request::create('', $requestMethod); - $this->assertSame($isMatch, $matcher->matches($request)); - } - - public static function getMethodData() - { - return [ - ['get', 'get', true], - ['get', ['get', 'post'], true], - ['get', 'post', false], - ['get', 'GET', true], - ['get', ['GET', 'POST'], true], - ['get', 'POST', false], - ]; - } - - public function testScheme() - { - $httpRequest = Request::create(''); - $httpsRequest = Request::create('', 'get', [], [], [], ['HTTPS' => 'on']); - - $matcher = new RequestMatcher(); - $matcher->matchScheme('https'); - $this->assertFalse($matcher->matches($httpRequest)); - $this->assertTrue($matcher->matches($httpsRequest)); - - $matcher->matchScheme('http'); - $this->assertFalse($matcher->matches($httpsRequest)); - $this->assertTrue($matcher->matches($httpRequest)); - - $matcher = new RequestMatcher(); - $this->assertTrue($matcher->matches($httpsRequest)); - $this->assertTrue($matcher->matches($httpRequest)); - } - - /** - * @dataProvider getHostData - */ - public function testHost($pattern, $isMatch) - { - $matcher = new RequestMatcher(); - $request = Request::create('', 'get', [], [], [], ['HTTP_HOST' => 'foo.example.com']); - - $matcher->matchHost($pattern); - $this->assertSame($isMatch, $matcher->matches($request)); - - $matcher = new RequestMatcher(null, $pattern); - $this->assertSame($isMatch, $matcher->matches($request)); - } - - public function testPort() - { - $matcher = new RequestMatcher(); - $request = Request::create('', 'get', [], [], [], ['HTTP_HOST' => null, 'SERVER_PORT' => 8000]); - - $matcher->matchPort(8000); - $this->assertTrue($matcher->matches($request)); - - $matcher->matchPort(9000); - $this->assertFalse($matcher->matches($request)); - - $matcher = new RequestMatcher(null, null, null, null, [], null, 8000); - $this->assertTrue($matcher->matches($request)); - } - - public static function getHostData() - { - return [ - ['.*\.example\.com', true], - ['\.example\.com$', true], - ['^.*\.example\.com$', true], - ['.*\.sensio\.com', false], - ['.*\.example\.COM', true], - ['\.example\.COM$', true], - ['^.*\.example\.COM$', true], - ['.*\.sensio\.COM', false], - ]; - } - - public function testPath() - { - $matcher = new RequestMatcher(); - - $request = Request::create('/admin/foo'); - - $matcher->matchPath('/admin/.*'); - $this->assertTrue($matcher->matches($request)); - - $matcher->matchPath('/admin'); - $this->assertTrue($matcher->matches($request)); - - $matcher->matchPath('^/admin/.*$'); - $this->assertTrue($matcher->matches($request)); - - $matcher->matchMethod('/blog/.*'); - $this->assertFalse($matcher->matches($request)); - } - - public function testPathWithLocaleIsNotSupported() - { - $matcher = new RequestMatcher(); - $request = Request::create('/en/login'); - $request->setLocale('en'); - - $matcher->matchPath('^/{_locale}/login$'); - $this->assertFalse($matcher->matches($request)); - } - - public function testPathWithEncodedCharacters() - { - $matcher = new RequestMatcher(); - $request = Request::create('/admin/fo%20o'); - $matcher->matchPath('^/admin/fo o*$'); - $this->assertTrue($matcher->matches($request)); - } - - public function testAttributes() - { - $matcher = new RequestMatcher(); - - $request = Request::create('/admin/foo'); - $request->attributes->set('foo', 'foo_bar'); - - $matcher->matchAttribute('foo', 'foo_.*'); - $this->assertTrue($matcher->matches($request)); - - $matcher->matchAttribute('foo', 'foo'); - $this->assertTrue($matcher->matches($request)); - - $matcher->matchAttribute('foo', '^foo_bar$'); - $this->assertTrue($matcher->matches($request)); - - $matcher->matchAttribute('foo', 'babar'); - $this->assertFalse($matcher->matches($request)); - } - - public function testAttributesWithClosure() - { - $matcher = new RequestMatcher(); - - $request = Request::create('/admin/foo'); - $request->attributes->set('_controller', fn () => new Response('foo')); - - $matcher->matchAttribute('_controller', 'babar'); - $this->assertFalse($matcher->matches($request)); - } - - public function testIps() - { - $matcher = new RequestMatcher(); - - $request = Request::create('', 'GET', [], [], [], ['REMOTE_ADDR' => '127.0.0.1']); - - $matcher->matchIp('127.0.0.1'); - $this->assertTrue($matcher->matches($request)); - - $matcher->matchIp('192.168.0.1'); - $this->assertFalse($matcher->matches($request)); - - $matcher->matchIps('127.0.0.1'); - $this->assertTrue($matcher->matches($request)); - - $matcher->matchIps('127.0.0.1, ::1'); - $this->assertTrue($matcher->matches($request)); - - $matcher->matchIps('192.168.0.1, ::1'); - $this->assertFalse($matcher->matches($request)); - - $matcher->matchIps(['127.0.0.1', '::1']); - $this->assertTrue($matcher->matches($request)); - - $matcher->matchIps(['192.168.0.1', '::1']); - $this->assertFalse($matcher->matches($request)); - - $matcher->matchIps(['1.1.1.1', '2.2.2.2', '127.0.0.1, ::1']); - $this->assertTrue($matcher->matches($request)); - - $matcher->matchIps(['1.1.1.1', '2.2.2.2', '192.168.0.1, ::1']); - $this->assertFalse($matcher->matches($request)); - } -} diff --git a/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php b/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php index 308e9e6fd8863..bf90979b169ea 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php @@ -12,7 +12,6 @@ namespace Symfony\Component\HttpFoundation\Tests; use PHPUnit\Framework\TestCase; -use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait; use Symfony\Component\HttpFoundation\Exception\ConflictingHeadersException; use Symfony\Component\HttpFoundation\Exception\JsonException; use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException; @@ -24,8 +23,6 @@ class RequestTest extends TestCase { - use ExpectDeprecationTrait; - protected function tearDown(): void { Request::setTrustedProxies([], -1); @@ -81,19 +78,6 @@ public function testIsNoCache() $this->assertFalse($isNoCache); } - /** - * @group legacy - */ - public function testGetContentType() - { - $this->expectDeprecation('Since symfony/http-foundation 6.2: The "Symfony\Component\HttpFoundation\Request::getContentType()" method is deprecated, use "getContentTypeFormat()" instead.'); - $request = new Request(); - - $contentType = $request->getContentType(); - - $this->assertNull($contentType); - } - public function testGetContentTypeFormat() { $request = new Request(); @@ -2569,12 +2553,10 @@ public function testReservedFlags() } } - /** - * @group legacy - */ - public function testInvalidUriCreationDeprecated() + public function testMalformedUriCreationException() { - $this->expectDeprecation('Since symfony/http-foundation 6.3: Calling "Symfony\Component\HttpFoundation\Request::create()" with an invalid URI is deprecated.'); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Malformed URI "/invalid-path:123".'); Request::create('/invalid-path:123'); } }