array union in controllers

! fix yii\web\Controller::bindActionParams behaviour for union types
o some refactoring in the process
This commit is contained in:
Chris Reichel
2025-04-10 10:34:06 +02:00
parent 2fad37b597
commit a67bfc1274
3 changed files with 178 additions and 34 deletions

View File

@ -10,6 +10,7 @@ namespace yiiunit\framework\web;
use RuntimeException;
use Yii;
use yii\base\InlineAction;
use yii\web\BadRequestHttpException;
use yii\web\NotFoundHttpException;
use yii\web\Response;
use yii\web\ServerErrorHttpException;
@ -329,5 +330,52 @@ class ControllerTest extends TestCase
$args = $this->controller->bindActionParams($injectionAction, $params);
$this->assertSame('test', $args[0]);
$this->assertSame(1, $args[1]);
// test that a value PHP parsed to a string but that should be an int becomes one
$params = ['arg' => 'test', 'second' => '1'];
$args = $this->controller->bindActionParams($injectionAction, $params);
$this->assertSame('test', $args[0]);
$this->assertSame(1, $args[1]);
}
public function testUnionBindingActionParamsWithArray()
{
if (PHP_VERSION_ID < 80000) {
$this->markTestSkipped('Can not be tested on PHP < 8.0');
return;
}
// Use the PHP80 controller for this test
$this->controller = new FakePhp80Controller('fake', new \yii\web\Application([
'id' => 'app',
'basePath' => __DIR__,
'components' => [
'request' => [
'cookieValidationKey' => 'wefJDF8sfdsfSDefwqdxj9oq',
'scriptFile' => __DIR__ . '/index.php',
'scriptUrl' => '/index.php',
],
],
]));
$this->mockWebApplication(['controller' => $this->controller]);
$injectionAction = new InlineAction('array-or-int', $this->controller, 'actionArrayOrInt');
$params = ['foo' => 1];
try {
$args = $this->controller->bindActionParams($injectionAction, $params);
$this->assertSame(1, $args[0]);
} catch (BadRequestHttpException $e) {
$this->fail('Failed to bind int param for array|int union type!');
}
try {
$paramsArray = ['foo' => [1, 2, 3, 4]];
$args = $this->controller->bindActionParams($injectionAction, $paramsArray);
$this->assertSame([1, 2, 3, 4], $args[0]);
} catch (BadRequestHttpException $e) {
$this->fail('Failed to bind array param for array|int union type!');
}
}
}

View File

@ -17,4 +17,9 @@ class FakePhp80Controller extends Controller
{
}
public function actionArrayOrInt(array|int $foo)
{
}
}