bug #33625 [DependencyInjection] Fix wrong exception when service is synthetic (k0d3r1s)

This PR was squashed before being merged into the 3.4 branch (closes #33625).

Discussion
----------

[DependencyInjection] Fix wrong exception when service is synthetic

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | #32874
| License       | MIT

This fixes wrongfully thrown exception when service is defined as synthetic and some arguments are binded in _defaults

Commits
-------

152dec95bc [DependencyInjection] Fix wrong exception when service is synthetic
This commit is contained in:
Nicolas Grekas 2019-09-27 17:48:09 +02:00
commit 293a22a433
3 changed files with 29 additions and 6 deletions

View File

@ -89,6 +89,10 @@ abstract class AbstractRecursivePass implements CompilerPassInterface
*/
protected function getConstructor(Definition $definition, $required)
{
if ($definition->isSynthetic()) {
return null;
}
if (\is_string($factory = $definition->getFactory())) {
if (!\function_exists($factory)) {
throw new RuntimeException(sprintf('Invalid service "%s": function "%s" does not exist.', $this->currentId, $factory));

View File

@ -589,12 +589,10 @@ class AutowirePassTest extends TestCase
);
}
/**
* @exceptedExceptionMessage Invalid service "Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy": method "setLogger()" does not exist.
*/
public function testWithNonExistingSetterAndAutowiring()
{
$this->expectException('Symfony\Component\DependencyInjection\Exception\RuntimeException');
$this->expectExceptionMessage('Invalid service "Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass": method "setLogger()" does not exist.');
$container = new ContainerBuilder();
$definition = $container->register(CaseSensitiveClass::class, CaseSensitiveClass::class)->setAutowired(true);

View File

@ -14,9 +14,11 @@ namespace Symfony\Component\DependencyInjection\Tests\Compiler;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Argument\BoundArgument;
use Symfony\Component\DependencyInjection\Compiler\AutowireRequiredMethodsPass;
use Symfony\Component\DependencyInjection\Compiler\DefinitionErrorExceptionPass;
use Symfony\Component\DependencyInjection\Compiler\ResolveBindingsPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
use Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy;
@ -110,12 +112,10 @@ class ResolveBindingsPassTest extends TestCase
$this->assertEquals([['setDefaultLocale', ['fr']]], $definition->getMethodCalls());
}
/**
* @exceptedExceptionMessage Invalid service "Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy": method "setLogger()" does not exist.
*/
public function testWithNonExistingSetterAndBinding()
{
$this->expectException('Symfony\Component\DependencyInjection\Exception\RuntimeException');
$this->expectExceptionMessage('Invalid service "Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy": method "setLogger()" does not exist.');
$container = new ContainerBuilder();
$bindings = [
@ -129,4 +129,25 @@ class ResolveBindingsPassTest extends TestCase
$pass = new ResolveBindingsPass();
$pass->process($container);
}
public function testSyntheticServiceWithBind()
{
$container = new ContainerBuilder();
$argument = new BoundArgument('bar');
$container->register('foo', 'stdClass')
->addArgument(new Reference('synthetic.service'));
$container->register('synthetic.service')
->setSynthetic(true)
->setBindings(['$apiKey' => $argument]);
$container->register(NamedArgumentsDummy::class, NamedArgumentsDummy::class)
->setBindings(['$apiKey' => $argument]);
(new ResolveBindingsPass())->process($container);
(new DefinitionErrorExceptionPass())->process($container);
$this->assertSame([1 => 'bar'], $container->getDefinition(NamedArgumentsDummy::class)->getArguments());
}
}