[#17724] Fixing autowiring bug where if some args are set, new ones are put in the wrong spot

This commit is contained in:
Ryan Weaver 2016-02-21 18:51:49 -05:00
parent aadaeecb47
commit cf692a6bd8
2 changed files with 42 additions and 5 deletions

View File

@ -110,12 +110,13 @@ class AutowirePass implements CompilerPassInterface
$value = $parameter->getDefaultValue();
}
if ($argumentExists) {
$definition->replaceArgument($index, $value);
} else {
$definition->addArgument($value);
}
$arguments[$index] = $value;
}
// it's possible index 1 was set, then index 0, then 2, etc
// make sure that we re-order so they're injected as expected
ksort($arguments);
$definition->setArguments($arguments);
}
/**

View File

@ -282,6 +282,36 @@ class AutowirePassTest extends \PHPUnit_Framework_TestCase
$arguments = $container->getDefinition('bar')->getArguments();
$this->assertSame('foo', (string) $arguments[0]);
}
public function testSomeSpecificArgumentsAreSet()
{
$container = new ContainerBuilder();
$container->register('foo', __NAMESPACE__.'\Foo');
$container->register('a', __NAMESPACE__.'\A');
$container->register('dunglas', __NAMESPACE__.'\Dunglas');
$container->register('multiple', __NAMESPACE__.'\MultipleArguments')
->setAutowired(true)
// set the 2nd (index 1) argument only: autowire the first and third
// args are: A, Foo, Dunglas
->setArguments(array(
1 => new Reference('foo'),
));
$pass = new AutowirePass();
$pass->process($container);
$definition = $container->getDefinition('multiple');
// takes advantage of Reference's __toString
$this->assertEquals(
array(
new Reference('a'),
new Reference('foo'),
new Reference('dunglas'),
),
$definition->getArguments()
);
}
}
class Foo
@ -406,3 +436,9 @@ class NotGuessableArgumentForSubclass
{
}
}
class MultipleArguments
{
public function __construct(A $k, $foo, Dunglas $dunglas)
{
}
}