Skip to content

Commit fbda90a

Browse files
committed
[DI] Show the right class autowired when providing a non-existing class in constructor
1 parent e02da2a commit fbda90a

File tree

6 files changed

+57
-4
lines changed

6 files changed

+57
-4
lines changed

src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -379,13 +379,14 @@ private function createTypeNotFoundMessageCallback(TypedReference $reference, $l
379379
$container->setAliases($this->container->getAliases());
380380
$container->setDefinitions($this->container->getDefinitions());
381381
$container->setResourceTracking(false);
382+
$currentId = $this->currentId;
382383

383-
return function () use ($container, $reference, $label) {
384-
return $this->createTypeNotFoundMessage($container, $reference, $label);
384+
return function () use ($container, $reference, $label, $currentId) {
385+
return $this->createTypeNotFoundMessage($container, $reference, $label, $currentId);
385386
};
386387
}
387388

388-
private function createTypeNotFoundMessage(ContainerBuilder $container, TypedReference $reference, $label)
389+
private function createTypeNotFoundMessage(ContainerBuilder $container, TypedReference $reference, $label, string $currentId)
389390
{
390391
if (!$r = $container->getReflectionClass($type = $reference->getType(), false)) {
391392
// either $type does not exist or a parent class does not exist
@@ -409,7 +410,7 @@ private function createTypeNotFoundMessage(ContainerBuilder $container, TypedRef
409410
}
410411
}
411412

412-
$message = sprintf('Cannot autowire service "%s": %s %s', $this->currentId, $label, $message);
413+
$message = sprintf('Cannot autowire service "%s": %s %s', $currentId, $label, $message);
413414

414415
if (null !== $this->lastFailure) {
415416
$message = $this->lastFailure."\n".$message;

src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,22 @@ public function testProcess()
5050
$this->assertEquals(Foo::class, (string) $container->getDefinition('bar')->getArgument(0));
5151
}
5252

53+
/**
54+
* @expectedException \Symfony\Component\DependencyInjection\Exception\AutowiringFailedException
55+
* @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.
56+
*/
57+
public function testProcessNotExistingActionParam()
58+
{
59+
$container = new ContainerBuilder();
60+
61+
$container->register(Foo::class);
62+
$barDefinition = $container->register(__NAMESPACE__.'EslaAction', __NAMESPACE__.'\ElsaAction');
63+
$barDefinition->setAutowired(true);
64+
65+
(new ResolveClassPass())->process($container);
66+
(new AutowirePass())->process($container);
67+
}
68+
5369
public function testProcessVariadic()
5470
{
5571
$container = new ContainerBuilder();
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
4+
5+
class ConstructNotExists
6+
{
7+
public function __construct(NotExist $notExist)
8+
{
9+
}
10+
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,3 +419,10 @@ public function __construct(LoggerInterface $logger, DecoratorInterface $decorat
419419
{
420420
}
421421
}
422+
423+
final class ElsaAction
424+
{
425+
public function __construct(NotExisting $notExisting)
426+
{
427+
}
428+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
services:
2+
_defaults:
3+
public: true
4+
autowire: true
5+
autoconfigure: true
6+
7+
Symfony\Component\DependencyInjection\Tests\Fixtures\ConstructNotExists: ~

src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,18 @@ public function testBindings()
804804
], array_map(function (BoundArgument $v) { return $v->getValues()[0]; }, $definition->getBindings()));
805805
}
806806

807+
/**
808+
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
809+
* @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.
810+
*/
811+
public function testProcessNotExistingActionParam()
812+
{
813+
$container = new ContainerBuilder();
814+
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
815+
$loader->load('services_not_existing.yml');
816+
$container->compile();
817+
}
818+
807819
public function testFqcnLazyProxy()
808820
{
809821
$container = new ContainerBuilder();

0 commit comments

Comments
 (0)