Fixing a bug where if a core class was autowired, autowiring tried to autowire optional args as if they were required
This commit is contained in:
parent
34d5f9eefe
commit
178a0f73b7
@ -273,6 +273,12 @@ class AutowirePass extends AbstractRecursivePass
|
||||
|
||||
// no default value? Then fail
|
||||
if (!$parameter->isDefaultValueAvailable()) {
|
||||
// For core classes, isDefaultValueAvailable() can
|
||||
// be false when isOptional() returns true. If the
|
||||
// argument *is* optional, allow it to be missing
|
||||
if ($parameter->isOptional()) {
|
||||
continue;
|
||||
}
|
||||
throw new AutowiringFailedException($this->currentId, sprintf('Cannot autowire service "%s": argument "$%s" of method "%s()" must have a type-hint or be given a value explicitly.', $this->currentId, $parameter->name, $class !== $this->currentId ? $class.'::'.$method : $method));
|
||||
}
|
||||
|
||||
|
@ -512,6 +512,23 @@ class AutowirePassTest extends TestCase
|
||||
);
|
||||
}
|
||||
|
||||
public function testOptionalArgsNoRequiredForCoreClasses()
|
||||
{
|
||||
$container = new ContainerBuilder();
|
||||
|
||||
$container->register('pdo_service', \PDO::class)
|
||||
->addArgument('sqlite:/foo.db')
|
||||
->setAutowired(true);
|
||||
|
||||
(new AutowirePass())->process($container);
|
||||
|
||||
$definition = $container->getDefinition('pdo_service');
|
||||
$this->assertEquals(
|
||||
array('sqlite:/foo.db'),
|
||||
$definition->getArguments()
|
||||
);
|
||||
}
|
||||
|
||||
public function testSetterInjection()
|
||||
{
|
||||
$container = new ContainerBuilder();
|
||||
|
Reference in New Issue
Block a user