[DI] Show the right class autowired when providing a non-existing class in constructor

This commit is contained in:
Amrouche Hamza 2019-06-14 09:12:25 +02:00
parent e02da2ab35
commit fbda90af6e
No known key found for this signature in database
GPG Key ID: E45A3DA456145BC1
6 changed files with 57 additions and 4 deletions

View File

@ -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;

View File

@ -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();

View File

@ -0,0 +1,10 @@
<?php
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
class ConstructNotExists
{
public function __construct(NotExist $notExist)
{
}
}

View File

@ -419,3 +419,10 @@ class NonAutowirableDecorator implements DecoratorInterface
{
}
}
final class ElsaAction
{
public function __construct(NotExisting $notExisting)
{
}
}

View File

@ -0,0 +1,7 @@
services:
_defaults:
public: true
autowire: true
autoconfigure: true
Symfony\Component\DependencyInjection\Tests\Fixtures\ConstructNotExists: ~

View File

@ -804,6 +804,18 @@ class YamlFileLoaderTest extends TestCase
], array_map(function (BoundArgument $v) { return $v->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();