bug #29697 [DI] Fixed wrong factory method in exception (Wojciech Gorczyca)

This PR was submitted for the 4.2 branch but it was merged into the 4.1 branch instead (closes #29697).

Discussion
----------

[DI] Fixed wrong factory method in exception

| Q             | A
| ------------- | ---
| Branch?       |  4.2 <!-- see below -->
| Bug fix?      | yes
| New feature?  | no <!-- don't forget to update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | #29678   <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | n/a <!-- required for new features -->

When a service definition with a factory defines invalid arguments, the [resulting exception message ](https://github.com/symfony/symfony/blob/master/src/Symfony/Component/DependencyInjection/Compiler/ResolveNamedArgumentsPass.php#L70)incorrectly specifies the factory constructor instead of the factory method as not having the specified named arguments.

<!--
Write a short README entry for your feature/bugfix here (replace this comment block.)
This will help people understand your PR and can be used as a start of the Doc PR.
Additionally:
 - Bug fixes must be submitted against the lowest branch where they apply
   (lowest branches are regularly merged to upper ones so they get the fixes too).
 - Features and deprecations must be submitted against the master branch.
-->

Commits
-------

922885c892 [DI] Fixed wrong factory method in exception
This commit is contained in:
Fabien Potencier 2019-01-05 08:48:11 +01:00 committed by Christian Flothmann
parent e38b5d2990
commit 0a3d3d4dff
2 changed files with 22 additions and 0 deletions

View File

@ -49,6 +49,7 @@ class ResolveNamedArgumentsPass extends AbstractRecursivePass
if (null === $parameters) {
$r = $this->getReflectionMethod($value, $method);
$class = $r instanceof \ReflectionMethod ? $r->class : $this->currentId;
$method = $r->getName();
$parameters = $r->getParameters();
}

View File

@ -16,8 +16,10 @@ use Symfony\Component\DependencyInjection\Compiler\ResolveNamedArgumentsPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
use Symfony\Component\DependencyInjection\Tests\Fixtures\FactoryDummy;
use Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy;
use Symfony\Component\DependencyInjection\Tests\Fixtures\SimilarArgumentsDummy;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TestDefinition1;
/**
* @author Kévin Dunglas <dunglas@gmail.com>
@ -102,6 +104,7 @@ class ResolveNamedArgumentsPassTest extends TestCase
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @expectedExceptionMessage Invalid service "Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy": method "__construct()" has no argument named "$notFound". Check your service definition.
*/
public function testArgumentNotFound()
{
@ -114,6 +117,24 @@ class ResolveNamedArgumentsPassTest extends TestCase
$pass->process($container);
}
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @expectedExceptionMessage Invalid service "Symfony\Component\DependencyInjection\Tests\Fixtures\TestDefinition1": method "Symfony\Component\DependencyInjection\Tests\Fixtures\FactoryDummy::create()" has no argument named "$notFound". Check your service definition.
*/
public function testCorrectMethodReportedInException()
{
$container = new ContainerBuilder();
$container->register(FactoryDummy::class, FactoryDummy::class);
$definition = $container->register(TestDefinition1::class, TestDefinition1::class);
$definition->setFactory(array(FactoryDummy::class, 'create'));
$definition->setArguments(array('$notFound' => '123'));
$pass = new ResolveNamedArgumentsPass();
$pass->process($container);
}
public function testTypedArgument()
{
$container = new ContainerBuilder();