[DependencyInjection] Fix wrong exception when service is synthetic

This commit is contained in:
k0d3r1s 2019-09-18 16:21:11 +03:00 committed by Nicolas Grekas
parent 6723e60540
commit 152dec95bc
3 changed files with 29 additions and 6 deletions

View File

@ -89,6 +89,10 @@ abstract class AbstractRecursivePass implements CompilerPassInterface
*/
protected function getConstructor(Definition $definition, $required)
{
if ($definition->isSynthetic()) {
return null;
}
if (\is_string($factory = $definition->getFactory())) {
if (!\function_exists($factory)) {
throw new RuntimeException(sprintf('Invalid service "%s": function "%s" does not exist.', $this->currentId, $factory));

View File

@ -589,12 +589,10 @@ class AutowirePassTest extends TestCase
);
}
/**
* @exceptedExceptionMessage Invalid service "Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy": method "setLogger()" does not exist.
*/
public function testWithNonExistingSetterAndAutowiring()
{
$this->expectException('Symfony\Component\DependencyInjection\Exception\RuntimeException');
$this->expectExceptionMessage('Invalid service "Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass": method "setLogger()" does not exist.');
$container = new ContainerBuilder();
$definition = $container->register(CaseSensitiveClass::class, CaseSensitiveClass::class)->setAutowired(true);

View File

@ -14,9 +14,11 @@ namespace Symfony\Component\DependencyInjection\Tests\Compiler;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Argument\BoundArgument;
use Symfony\Component\DependencyInjection\Compiler\AutowireRequiredMethodsPass;
use Symfony\Component\DependencyInjection\Compiler\DefinitionErrorExceptionPass;
use Symfony\Component\DependencyInjection\Compiler\ResolveBindingsPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
use Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy;
@ -110,12 +112,10 @@ class ResolveBindingsPassTest extends TestCase
$this->assertEquals([['setDefaultLocale', ['fr']]], $definition->getMethodCalls());
}
/**
* @exceptedExceptionMessage Invalid service "Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy": method "setLogger()" does not exist.
*/
public function testWithNonExistingSetterAndBinding()
{
$this->expectException('Symfony\Component\DependencyInjection\Exception\RuntimeException');
$this->expectExceptionMessage('Invalid service "Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy": method "setLogger()" does not exist.');
$container = new ContainerBuilder();
$bindings = [
@ -129,4 +129,25 @@ class ResolveBindingsPassTest extends TestCase
$pass = new ResolveBindingsPass();
$pass->process($container);
}
public function testSyntheticServiceWithBind()
{
$container = new ContainerBuilder();
$argument = new BoundArgument('bar');
$container->register('foo', 'stdClass')
->addArgument(new Reference('synthetic.service'));
$container->register('synthetic.service')
->setSynthetic(true)
->setBindings(['$apiKey' => $argument]);
$container->register(NamedArgumentsDummy::class, NamedArgumentsDummy::class)
->setBindings(['$apiKey' => $argument]);
(new ResolveBindingsPass())->process($container);
(new DefinitionErrorExceptionPass())->process($container);
$this->assertSame([1 => 'bar'], $container->getDefinition(NamedArgumentsDummy::class)->getArguments());
}
}