From 9cf81798f857205c5bbb4c8c7895f838d40b0c4b Mon Sep 17 00:00:00 2001 From: k0d3r1s Date: Wed, 18 Sep 2019 16:21:11 +0300 Subject: [PATCH] [DependencyInjection] Fix wrong exception when service is synthetic --- Compiler/AbstractRecursivePass.php | 4 ++++ Tests/Compiler/AutowirePassTest.php | 4 +--- Tests/Compiler/ResolveBindingsPassTest.php | 27 +++++++++++++++++++--- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/Compiler/AbstractRecursivePass.php b/Compiler/AbstractRecursivePass.php index a89bf1647..5ca2b2246 100644 --- a/Compiler/AbstractRecursivePass.php +++ b/Compiler/AbstractRecursivePass.php @@ -89,6 +89,10 @@ protected function processValue($value, $isRoot = false) */ 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)); diff --git a/Tests/Compiler/AutowirePassTest.php b/Tests/Compiler/AutowirePassTest.php index f729f72ba..7eb7f6024 100644 --- a/Tests/Compiler/AutowirePassTest.php +++ b/Tests/Compiler/AutowirePassTest.php @@ -589,12 +589,10 @@ public function testSetterInjection() ); } - /** - * @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); diff --git a/Tests/Compiler/ResolveBindingsPassTest.php b/Tests/Compiler/ResolveBindingsPassTest.php index a44fea4d6..fd526caa9 100644 --- a/Tests/Compiler/ResolveBindingsPassTest.php +++ b/Tests/Compiler/ResolveBindingsPassTest.php @@ -14,9 +14,11 @@ 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 @@ public function testScalarSetter() $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 @@ public function testWithNonExistingSetterAndBinding() $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()); + } }