From 0a3d3d4dff2870056b86b04903bacbb02a854f2a Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 5 Jan 2019 08:48:11 +0100 Subject: [PATCH] bug #29697 [DI] Fixed wrong factory method in exception (Wojciech Gorczyca) This PR was submitted for the 4.2 branch but it was merged into the 4.1 branch instead (closes #29697). Discussion ---------- [DI] Fixed wrong factory method in exception | Q | A | ------------- | --- | Branch? | 4.2 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #29678 | License | MIT | Doc PR | n/a When a service definition with a factory defines invalid arguments, the [resulting exception message ](https://github.com/symfony/symfony/blob/master/src/Symfony/Component/DependencyInjection/Compiler/ResolveNamedArgumentsPass.php#L70)incorrectly specifies the factory constructor instead of the factory method as not having the specified named arguments. Commits ------- 922885c892 [DI] Fixed wrong factory method in exception --- .../Compiler/ResolveNamedArgumentsPass.php | 1 + .../ResolveNamedArgumentsPassTest.php | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/Symfony/Component/DependencyInjection/Compiler/ResolveNamedArgumentsPass.php b/src/Symfony/Component/DependencyInjection/Compiler/ResolveNamedArgumentsPass.php index 90cf64adbec3..36980df09fd9 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/ResolveNamedArgumentsPass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/ResolveNamedArgumentsPass.php @@ -49,6 +49,7 @@ protected function processValue($value, $isRoot = false) if (null === $parameters) { $r = $this->getReflectionMethod($value, $method); $class = $r instanceof \ReflectionMethod ? $r->class : $this->currentId; + $method = $r->getName(); $parameters = $r->getParameters(); } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolveNamedArgumentsPassTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolveNamedArgumentsPassTest.php index fe681b41df78..087d7ab79508 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolveNamedArgumentsPassTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolveNamedArgumentsPassTest.php @@ -16,8 +16,10 @@ use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass; +use Symfony\Component\DependencyInjection\Tests\Fixtures\FactoryDummy; use Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy; use Symfony\Component\DependencyInjection\Tests\Fixtures\SimilarArgumentsDummy; +use Symfony\Component\DependencyInjection\Tests\Fixtures\TestDefinition1; /** * @author Kévin Dunglas @@ -102,6 +104,7 @@ public function testClassNoConstructor() /** * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException + * @expectedExceptionMessage Invalid service "Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy": method "__construct()" has no argument named "$notFound". Check your service definition. */ public function testArgumentNotFound() { @@ -114,6 +117,24 @@ public function testArgumentNotFound() $pass->process($container); } + /** + * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException + * @expectedExceptionMessage Invalid service "Symfony\Component\DependencyInjection\Tests\Fixtures\TestDefinition1": method "Symfony\Component\DependencyInjection\Tests\Fixtures\FactoryDummy::create()" has no argument named "$notFound". Check your service definition. + */ + public function testCorrectMethodReportedInException() + { + $container = new ContainerBuilder(); + + $container->register(FactoryDummy::class, FactoryDummy::class); + + $definition = $container->register(TestDefinition1::class, TestDefinition1::class); + $definition->setFactory(array(FactoryDummy::class, 'create')); + $definition->setArguments(array('$notFound' => '123')); + + $pass = new ResolveNamedArgumentsPass(); + $pass->process($container); + } + public function testTypedArgument() { $container = new ContainerBuilder();