Skip to content

Commit

Permalink
bug #16642 [DI][autowiring] throw exception when many services use th…
Browse files Browse the repository at this point in the history
…e same class. (aitboudad)

This PR was merged into the 2.8 branch.

Discussion
----------

[DI][autowiring] throw exception when many services use the same class.

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Fixed tickets  | #16639
| Tests pass?   | yes
| License       | MIT

Commits
-------

a21a016 [DI][autowiring] throw exception when many services use the same class.
  • Loading branch information
fabpot committed Nov 30, 2015
2 parents 6f349c6 + a21a016 commit 729b98c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
Expand Up @@ -227,7 +227,7 @@ private function set($type, $id)
*/
private function createAutowiredDefinition(\ReflectionClass $typeHint, $id)
{
if (!$typeHint->isInstantiable()) {
if (isset($this->notGuessableTypes[$typeHint->name]) || !$typeHint->isInstantiable()) {
throw new RuntimeException(sprintf('Unable to autowire argument of type "%s" for the service "%s".', $typeHint->name, $id));
}

Expand Down
Expand Up @@ -117,6 +117,56 @@ public function testTypeCollision()
$pass->process($container);
}

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
* @expectedExceptionMessage Unable to autowire argument of type "Symfony\Component\DependencyInjection\Tests\Compiler\Foo" for the service "a".
*/
public function testTypeNotGuessable()
{
$container = new ContainerBuilder();

$container->register('a1', __NAMESPACE__.'\Foo');
$container->register('a2', __NAMESPACE__.'\Foo');
$aDefinition = $container->register('a', __NAMESPACE__.'\NotGuessableArgument');
$aDefinition->setAutowired(true);

$pass = new AutowirePass();
$pass->process($container);
}

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
* @expectedExceptionMessage Unable to autowire argument of type "Symfony\Component\DependencyInjection\Tests\Compiler\A" for the service "a".
*/
public function testTypeNotGuessableWithSubclass()
{
$container = new ContainerBuilder();

$container->register('a1', __NAMESPACE__.'\B');
$container->register('a2', __NAMESPACE__.'\B');
$aDefinition = $container->register('a', __NAMESPACE__.'\NotGuessableArgumentForSubclass');
$aDefinition->setAutowired(true);

$pass = new AutowirePass();
$pass->process($container);
}

public function testTypeNotGuessableWithTypeSet()
{
$container = new ContainerBuilder();

$container->register('a1', __NAMESPACE__.'\Foo');
$container->register('a2', __NAMESPACE__.'\Foo')->addAutowiringType(__NAMESPACE__.'\Foo');
$aDefinition = $container->register('a', __NAMESPACE__.'\NotGuessableArgument');
$aDefinition->setAutowired(true);

$pass = new AutowirePass();
$pass->process($container);

$this->assertCount(1, $container->getDefinition('a')->getArguments());
$this->assertEquals('a2', (string) $container->getDefinition('a')->getArgument(0));
}

public function testWithTypeSet()
{
$container = new ContainerBuilder();
Expand Down Expand Up @@ -335,3 +385,15 @@ public function __construct(Dunglas $k, NotARealClass $r)
{
}
}
class NotGuessableArgument
{
public function __construct(Foo $k)
{
}
}
class NotGuessableArgumentForSubclass
{
public function __construct(A $k)
{
}
}

0 comments on commit 729b98c

Please sign in to comment.