Skip to content

Commit

Permalink
Merge pull request #16643 from cakephp/fix-16640
Browse files Browse the repository at this point in the history
Improve casting of integer routing parameters
  • Loading branch information
markstory committed Jul 25, 2022
2 parents db7b005 + 6615ea8 commit f95953c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Controller/ControllerFactory.php
Expand Up @@ -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':
Expand Down
24 changes: 17 additions & 7 deletions tests/TestCase/Controller/ControllerFactoryTest.php
Expand Up @@ -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([
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

Expand Down

0 comments on commit f95953c

Please sign in to comment.