diff --git a/src/Controller/ControllerFactory.php b/src/Controller/ControllerFactory.php index 8c1cd2e7035..f38719a8557 100644 --- a/src/Controller/ControllerFactory.php +++ b/src/Controller/ControllerFactory.php @@ -268,7 +268,7 @@ protected function coerceStringToType(string $argument, ReflectionNamedType $typ case 'float': return is_numeric($argument) ? (float)$argument : null; case 'int': - return ctype_digit($argument) ? (int)$argument : null; + return filter_var($argument, FILTER_VALIDATE_INT) ? (int)$argument : null; case 'bool': return $argument === '0' ? false : ($argument === '1' ? true : null); case 'array': diff --git a/tests/TestCase/Controller/ControllerFactoryTest.php b/tests/TestCase/Controller/ControllerFactoryTest.php index ffbe2c9ba33..a82ed010b18 100644 --- a/tests/TestCase/Controller/ControllerFactoryTest.php +++ b/tests/TestCase/Controller/ControllerFactoryTest.php @@ -733,15 +733,13 @@ public function testInvokePassedParametersCoercion(): void 'plugin' => null, 'controller' => 'Dependencies', 'action' => 'requiredTyped', - 'pass' => ['1.0', '02', '0', '8,9'], + 'pass' => ['1.0', '2', '0', '8,9'], ], ]); $controller = $this->factory->create($request); $result = $this->factory->invoke($controller); $data = json_decode((string)$result->getBody(), true); - - $this->assertNotNull($data); $this->assertSame(['one' => 1.0, 'two' => 2, 'three' => false, 'four' => ['8', '9']], $data); $request = new ServerRequest([ @@ -750,16 +748,29 @@ public function testInvokePassedParametersCoercion(): void 'plugin' => null, 'controller' => 'Dependencies', 'action' => 'requiredTyped', - 'pass' => ['1.0', '02', '0', ''], + 'pass' => ['1.0', '2', '0', ''], ], ]); $controller = $this->factory->create($request); $result = $this->factory->invoke($controller); $data = json_decode((string)$result->getBody(), true); - - $this->assertNotNull($data); $this->assertSame(['one' => 1.0, 'two' => 2, 'three' => false, 'four' => []], $data); + + $request = new ServerRequest([ + 'url' => 'test_plugin_three/dependencies/requiredTyped', + 'params' => [ + 'plugin' => null, + 'controller' => 'Dependencies', + 'action' => 'requiredTyped', + 'pass' => ['1.0', '-1', '0', ''], + ], + ]); + $controller = $this->factory->create($request); + + $result = $this->factory->invoke($controller); + $data = json_decode((string)$result->getBody(), true); + $this->assertSame(['one' => 1.0, 'two' => -1, 'three' => false, 'four' => []], $data); } /** @@ -781,7 +792,6 @@ public function testInvokeOptionalTypedParam(): void $result = $this->factory->invoke($controller); $data = json_decode((string)$result->getBody(), true); - $this->assertNotNull($data); $this->assertSame(['one' => 1.0, 'two' => 2, 'three' => true], $data); }