Skip to content

Commit

Permalink
[DependencyInjection] Fix wrong exception when service is synthetic
Browse files Browse the repository at this point in the history
  • Loading branch information
k0d3r1s authored and nicolas-grekas committed Sep 27, 2019
1 parent c52e96a commit 9cf8179
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
4 changes: 4 additions & 0 deletions Compiler/AbstractRecursivePass.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ protected function processValue($value, $isRoot = false)
*/
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));
Expand Down
4 changes: 1 addition & 3 deletions Tests/Compiler/AutowirePassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -589,12 +589,10 @@ public function testSetterInjection()
);
}

/**
* @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);
Expand Down
27 changes: 24 additions & 3 deletions Tests/Compiler/ResolveBindingsPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
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;
Expand Down Expand Up @@ -110,12 +112,10 @@ public function testScalarSetter()
$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 = [
Expand All @@ -129,4 +129,25 @@ public function testWithNonExistingSetterAndBinding()
$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());
}
}

0 comments on commit 9cf8179

Please sign in to comment.