From fbda90af6e7ef0bcbaed046fec783b2283d21ee8 Mon Sep 17 00:00:00 2001 From: Amrouche Hamza Date: Fri, 14 Jun 2019 09:12:25 +0200 Subject: [PATCH] [DI] Show the right class autowired when providing a non-existing class in constructor --- .../Compiler/AutowirePass.php | 9 +++++---- .../Tests/Compiler/AutowirePassTest.php | 16 ++++++++++++++++ .../Tests/Fixtures/ConstructNotExists.php | 10 ++++++++++ .../Fixtures/includes/autowiring_classes.php | 7 +++++++ .../Fixtures/yaml/services_not_existing.yml | 7 +++++++ .../Tests/Loader/YamlFileLoaderTest.php | 12 ++++++++++++ 6 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Fixtures/ConstructNotExists.php create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services_not_existing.yml diff --git a/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php b/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php index 2a1a8ab69b..3ae264f5c8 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php @@ -379,13 +379,14 @@ class AutowirePass extends AbstractRecursivePass $container->setAliases($this->container->getAliases()); $container->setDefinitions($this->container->getDefinitions()); $container->setResourceTracking(false); + $currentId = $this->currentId; - return function () use ($container, $reference, $label) { - return $this->createTypeNotFoundMessage($container, $reference, $label); + return function () use ($container, $reference, $label, $currentId) { + return $this->createTypeNotFoundMessage($container, $reference, $label, $currentId); }; } - private function createTypeNotFoundMessage(ContainerBuilder $container, TypedReference $reference, $label) + private function createTypeNotFoundMessage(ContainerBuilder $container, TypedReference $reference, $label, string $currentId) { if (!$r = $container->getReflectionClass($type = $reference->getType(), false)) { // either $type does not exist or a parent class does not exist @@ -409,7 +410,7 @@ class AutowirePass extends AbstractRecursivePass } } - $message = sprintf('Cannot autowire service "%s": %s %s', $this->currentId, $label, $message); + $message = sprintf('Cannot autowire service "%s": %s %s', $currentId, $label, $message); if (null !== $this->lastFailure) { $message = $this->lastFailure."\n".$message; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php index 3c08425fc2..3198456444 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php @@ -50,6 +50,22 @@ class AutowirePassTest extends TestCase $this->assertEquals(Foo::class, (string) $container->getDefinition('bar')->getArgument(0)); } + /** + * @expectedException \Symfony\Component\DependencyInjection\Exception\AutowiringFailedException + * @expectedExceptionMessage Cannot autowire service "Symfony\Component\DependencyInjection\Tests\CompilerEslaAction": argument "$notExisting" of method "Symfony\Component\DependencyInjection\Tests\Compiler\ElsaAction::__construct()" has type "Symfony\Component\DependencyInjection\Tests\Compiler\NotExisting" but this class was not found. + */ + public function testProcessNotExistingActionParam() + { + $container = new ContainerBuilder(); + + $container->register(Foo::class); + $barDefinition = $container->register(__NAMESPACE__.'EslaAction', __NAMESPACE__.'\ElsaAction'); + $barDefinition->setAutowired(true); + + (new ResolveClassPass())->process($container); + (new AutowirePass())->process($container); + } + public function testProcessVariadic() { $container = new ContainerBuilder(); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/ConstructNotExists.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/ConstructNotExists.php new file mode 100644 index 0000000000..dcf6484fb5 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/ConstructNotExists.php @@ -0,0 +1,10 @@ +getValues()[0]; }, $definition->getBindings())); } + /** + * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException + * @expectedExceptionMessage Cannot autowire service "Symfony\Component\DependencyInjection\Tests\Fixtures\ConstructNotExists": argument "$notExist" of method "__construct()" has type "Symfony\Component\DependencyInjection\Tests\Fixtures\NotExist" but this class was not found. + */ + public function testProcessNotExistingActionParam() + { + $container = new ContainerBuilder(); + $loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml')); + $loader->load('services_not_existing.yml'); + $container->compile(); + } + public function testFqcnLazyProxy() { $container = new ContainerBuilder();