Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[DI][autowiring] throw exception when many services use the same class.
  • Loading branch information
aitboudad committed Nov 24, 2015
commit a21a0166c692d1cbb71a9aa0c68f68b6a20215f1
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add another test that is just like this, except that you explicitly call set() to map \Foo to service a1. You would assert that this would autowire a1 and not throw an exception

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And, what about sub-classes? What if Foo extends Bar, all instances in the container are Foo, but I type-hint with Bar? Or more concretely: what if all instances in the container are Symfony\Bridge\Monolog\Logger and I type-hint Monolog\Logger? Does this cover that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

/**
* @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)
{
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.