Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.4] Revert breaking changes in ControllerDispatcher::resolveMethodDependencies #18644

Closed
wants to merge 3 commits into from
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 4 additions & 13 deletions src/Illuminate/Routing/RouteDependencyResolverTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,19 @@ protected function resolveClassMethodDependencies(array $parameters, $instance,
*/
public function resolveMethodDependencies(array $parameters, ReflectionFunctionAbstract $reflector)
{
$results = [];

$instanceCount = 0;

$values = array_values($parameters);

foreach ($reflector->getParameters() as $key => $parameter) {
$instance = $this->transformDependency(
$parameter, $parameters
);

if (! is_null($instance)) {
$instanceCount++;

$results[$parameter->getName()] = $instance;
} else {
$results[$parameter->getName()] = isset($values[$key - $instanceCount])
? $values[$key - $instanceCount] : $parameter->getDefaultValue();
array_splice(
$parameters, $key, 0, [$instance]
);
}
}

return $results;
return $parameters;
}

/**
Expand Down
100 changes: 100 additions & 0 deletions tests/Routing/ControllerDispatcherTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace Illuminate\Tests\Routing;

use ReflectionMethod;
use PHPUnit\Framework\TestCase;
use Illuminate\Container\Container;
use Illuminate\Routing\ControllerDispatcher;

class ControllerDispatcherTest extends TestCase
{
/**
* @var \Illuminate\Routing\ControllerDispatcher
*/
protected $controllerDispatcher;

public function setUp()
{
parent::setUp();

$container = new Container();
$this->controllerDispatcher = new ControllerDispatcher($container);
}

public function targetResolveMethodDependenciesKeepKeys($first)
{
}

public function testResolveMethodDependenciesKeepKeys()
{
$parameters = [
'first' => 'first-value',
];
$target = new ReflectionMethod(self::class, 'targetResolveMethodDependenciesKeepKeys');

$actual = $this->controllerDispatcher->resolveMethodDependencies($parameters, $target);

$expected = [
'first' => 'first-value',
];
$this->assertEquals($expected, $actual);
}

public function targetResolveMethodDependenciesKeepsOrderOfKeysAndValues($first, $second)
{
}

public function testResolveMethodDependenciesKeepsOrderOfKeysAndValues()
{
$parameters = [
'second' => 'second-value',
'first' => 'first-value',
];
$target = new ReflectionMethod(self::class, 'targetResolveMethodDependenciesKeepsOrderOfKeysAndValues');

$actual = $this->controllerDispatcher->resolveMethodDependencies($parameters, $target);

$expected = [
'second' => 'second-value',
'first' => 'first-value',
];
$this->assertEquals($expected, $actual);
}

public function targetResolveMethodDependenciesKeepsUnknownKeys($first)
{
}

public function testResolveMethodDependenciesKeepsUnknownKeys()
{
$parameters = [
'first' => 'first-value',
'unknown' => 'unknown',
];
$target = new ReflectionMethod(self::class, 'targetResolveMethodDependenciesKeepsUnknownKeys');

$actual = $this->controllerDispatcher->resolveMethodDependencies($parameters, $target);

$expected = [
'first' => 'first-value',
'unknown' => 'unknown',
];
$this->assertEquals($expected, $actual);
}

public function targetResolveMethodDependenciesDoesNotCrashOnMissingKeys($first)
{
}

public function testResolveMethodDependenciesDoesNotCrashOnMissingKeys()
{
$parameters = [];
$target = new ReflectionMethod(self::class, 'targetResolveMethodDependenciesDoesNotCrashOnMissingKeys');

$actual = $this->controllerDispatcher->resolveMethodDependencies($parameters, $target);

$expected = [];
$this->assertEquals($expected, $actual);
}
}
7 changes: 5 additions & 2 deletions tests/Routing/RoutingRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,15 +314,18 @@ public function testClassesAndVariablesCanBeInjectedIntoRoutes()
unset($_SERVER['__test.route_inject']);
$router = $this->getRouter();
$router->get('foo/{var}/{bar?}/{baz?}', function (stdClass $foo, $var, $bar = 'test', stdClass $baz = null) {
$_SERVER['__test.route_inject'] = func_get_args();
// We build the array manually to ensure that we get the actual values in the method,
// and not the values that were originally sent to us; func_get_args() will only return
// the original values sent to us, not what the method received, including default values.
$_SERVER['__test.route_inject'] = [$foo, $var, $bar, $baz];

return 'hello';
});
$this->assertEquals('hello', $router->dispatch(Request::create('foo/bar', 'GET'))->getContent());
$this->assertInstanceOf('stdClass', $_SERVER['__test.route_inject'][0]);
$this->assertEquals('bar', $_SERVER['__test.route_inject'][1]);
$this->assertEquals('test', $_SERVER['__test.route_inject'][2]);
$this->assertInstanceOf('stdClass', $_SERVER['__test.route_inject'][3]);
$this->assertNull($_SERVER['__test.route_inject'][3]);
$this->assertArrayHasKey(3, $_SERVER['__test.route_inject']);
unset($_SERVER['__test.route_inject']);
}
Expand Down