8000 Test the suggestion of already registered services · symfony/symfony@407f132 · GitHub
[go: up one dir, main page]

Skip to content

Commit 407f132

Browse files
committed
Test the suggestion of already registered services
1 parent decaf23 commit 407f132

File tree

3 files changed

+27
-21
lines changed

3 files changed

+27
-21
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,14 +336,16 @@ private function createTypeAlternatives(TypedReference $reference)
336336
if ($message = $this->getAliasesSuggestionForType($type = $reference->getType())) {
337337
return ' '.$message;
338338
}
339-
if (isset($this->ambiguousServiceTypes[$type])) {
339+
340+
$servicesAndAliases = $this->container->getServiceIds();
341+
if (!$this->container->has($type) && false !== $key = array_search(strtolower($type), array_map('strtolower', $servicesAndAliases))) {
342+
return sprintf(' Did you mean "%s"?', $servicesAndAliases[$key]);
343+
} elseif (isset($this->ambiguousServiceTypes[$type])) {
340344
$message = sprintf('one of these existing services: "%s"', implode('", "', $this->ambiguousServiceTypes[$type]));
341345
} elseif (isset($this->types[$type])) {
342346
$message = sprintf('the existing "%s" service', $this->types[$type]);
343347
} elseif ($reference->getRequiringClass() && !$reference->canBeAutoregistered()) {
344348
return ' It cannot be auto-registered because it is from a different root namespace.';
345-
} elseif (is_array($this->types) && (null !== $key = array_search(strtolower($type), array_map('strtolower', $this->types)))) {
346-
return sprintf(' Maybe you mean %s instead of %s ?', $key, $type);
347349
} else {
348350
return;
349351
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,24 @@ public function provideNotWireableCalls()
685685
);
686686
}
687687

688+
/**
689+
* @expectedException \Symfony\Component\DependencyInjection\Exception\AutowiringFailedException
690+
* @expectedExceptionMessage Cannot autowire service "foo": argument "$sam" of method "Symfony\Component\DependencyInjection\Tests\Compiler\NotWireable::setNotAutowireableBecauseOfATypo()" references class "Symfony\Component\DependencyInjection\Tests\Compiler\lesTilleuls" but no such service exists. Did you mean "Symfony\Component\DependencyInjection\Tests\Compiler\LesTilleuls"?
691+
*/
692+
public function testSuggestRegisteredServicesWithSimilarCase()
693+
{
694+
$container = new ContainerBuilder();
695+
696+
$container->register(LesTilleuls::class, LesTilleuls::class);
697+
$container->register('foo', NotWireable::class)->setAutowired(true)
698+
->addMethodCall('setNotAutowireableBecauseOfATypo', array())
699+
;
700+
701+
(new ResolveClassPass())->process($container);
702+
(new AutowireRequiredMethodsPass())->process($container);
703+
(new AutowirePass())->process($container);
704+
}
705+
688706
/**
689707
* @expectedException \Symfony\Component\DependencyInjection\Exception\AutowiringFailedException
690708
* @expectedExceptionMessage Cannot autowire service "j": argument "$i" of method "Symfony\Component\DependencyInjection\Tests\Compiler\J::__construct()" references class "Symfony\Component\DependencyInjection\Tests\Compiler\I" but no such service exists. Try changing the type-hint to "Symfony\Component\DependencyInjection\Tests\Compiler\IInterface" instead.

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

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,6 @@ class Foo
66
{
77
}
88

9-
class FooFoo
10-
{
11-
public function __construct(Foo $foo)
12-
{
13-
}
14-
}
15-
169
class Bar
1710
{
1811
public function __construct(Foo $foo)
@@ -300,23 +293,16 @@ public function setChildMethodWithoutDocBlock(A $a)
300293
}
301294
}
302295

303-
class ARealClass {
304-
305-
}
306-
307-
class ClassMisMatch {
308-
309-
public function __construct(arealclass $n = null)
310-
{
311-
}
312-
}
313-
314296
class NotWireable
315297
{
316298
public function setNotAutowireable(NotARealClass $n)
317299
{
318300
}
319301

302+
public function setNotAutowireableBecauseOfATypo(lesTilleuls $sam)
303+
{
304+
}
305+
320306
public function setBar()
321307
{
322308
}

0 commit comments

Comments
 (0)
0