8000 [HttpKernel] Allow variadic controller parameters to be resolved. by akerouanton · Pull Request #15777 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
8000 Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,14 @@ protected function doGetArguments(Request $request, $controller, array $paramete
{
$attributes = $request->attributes->all();
$arguments = array();
$variadicAvailable = method_exists('\ReflectionMethod', 'isVariadic');
foreach ($parameters as $param) {
if (array_key_exists($param->name, $attributes)) {
$arguments[] = $attributes[$param->name];
if ($variadicAvailable && $param->isVariadic() && is_array($attributes[$param->name])) {
$arguments = array_merge($arguments, array_values($attributes[$param->name]));
} else {
$arguments[] = $attributes[$param->name];
}
} elseif ($param->getClass() && $param->getClass()->isInstance($request)) {
$arguments[] = $request;
} elseif ($param->isDefaultValueAvailable()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\HttpKernel\Tests\Controller;

use Symfony\Component\HttpKernel\Controller\ControllerResolver;
use Symfony\Component\HttpKernel\Tests\Fixtures\Controller\VariadicController;
use Symfony\Component\HttpFoundation\Request;

class ControllerResolverTest extends \PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -196,6 +197,22 @@ public function testGetArguments()
$this->assertEquals(array($request), $resolver->getArguments($request, $controller), '->getArguments() injects the request');
}

/**
* @requires PHP 5.6
*/
public function testGetVariadicArguments()
{
$resolver = new ControllerResolver();

$request = Request::create('/');
$param1 = new \stdClass();
$param2 = new \stdClass();
$request->attributes->set('foo', 'foo');
$request->attributes->set('bar', array($param1, $param2));
$controller = array(new VariadicController(), 'action');
$this->assertEquals(array('foo', $param1, $param2), $resolver->getArguments($request, $controller));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be assertSame() as assertEquals() would not make a distinction between ['foo', $param1, $param2] and ['foo', $param1, $param1].

Also, consider using strings instead of stdClass here. In this case type doesn't matter and if we can make the test shorter, we should ;)

}

public function testCreateControllerCanReturnAnyCallable()
{
$mock = $this->getMock('Symfony\Component\HttpKernel\Controller\ControllerResolver', array('createController'));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Symfony\Component\HttpKernel\Tests\Fixtures\Controller;

class VariadicController
{
public function action($foo,...$bar)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space after the comma.

{
}
}
0