8000 bug #33625 [DependencyInjection] Fix wrong exception when service is … · symfony/symfony@293a22a · GitHub
[go: up one dir, main page]

Skip to content

Commit 293a22a

Browse files
bug #33625 [DependencyInjection] Fix wrong exception when service is synthetic (k0d3r1s)
This PR was squashed before being merged into the 3.4 branch (closes #33625). Discussion ---------- [DependencyInjection] Fix wrong exception when service is synthetic | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | #32874 | License | MIT This fixes wrongfully thrown exception when service is defined as synthetic and some arguments are binded in _defaults Commits ------- 152dec9 [DependencyInjection] Fix wrong exception when service is synthetic
2 parents 82a62d2 + 152dec9 commit 293a22a

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

src/Symfony/Component/DependencyInjection/Compiler/AbstractRecursivePass.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ protected function processValue($value, $isRoot = false)
8989
*/
9090
protected function getConstructor(Definition $definition, $required)
9191
{
92+
if ($definition->isSynthetic()) {
93+
return null;
94+
}
95+
9296
if (\is_string($factory = $definition->getFactory())) {
9397
if (!\function_exists($factory)) {
9498
throw new RuntimeException(sprintf('Invalid service "%s": function "%s" does not exist.', $this->currentId, $factory));

src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -589,12 +589,10 @@ public function testSetterInjection()
589589
);
590590
}
591591

592-
/**
593-
* @exceptedExceptionMessage Invalid service "Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy": method "setLogger()" does not exist.
594-
*/
595592
public function testWithNonExistingSetterAndAutowiring()
596593
{
597594
$this->expectException('Symfony\Component\DependencyInjection\Exception\RuntimeException');
595+
$this->expectExceptionMessage('Invalid service "Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass": method "setLogger()" does not exist.');
598596
$container = new ContainerBuilder();
599597

600598
$definition = $container->register(CaseSensitiveClass::class, CaseSensitiveClass::class)->setAutowired(true);

src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolveBindingsPassTest.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\DependencyInjection\Argument\BoundArgument;
1616
use Symfony\Component\DependencyInjection\Compiler\AutowireRequiredMethodsPass;
17+
use Symfony\Component\DependencyInjection\Compiler\DefinitionErrorExceptionPass;
1718
use Symfony\Component\DependencyInjection\Compiler\ResolveBindingsPass;
1819
use Symfony\Component\DependencyInjection\ContainerBuilder;
1920
use Symfony\Component\DependencyInjection\Definition;
21+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
2022
use Symfony\Component\DependencyInjection\Reference;
2123
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
2224
use Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy;
@@ -110,12 +112,10 @@ public function testScalarSetter()
110112
$this->assertEquals([['setDefaultLocale', ['fr']]], $definition->getMethodCalls());
111113
}
112114

113-
/**
114-
* @exceptedExceptionMessage Invalid service "Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy": method "setLogger()" does not exist.
115-
*/
116115
public function testWithNonExistingSetterAndBinding()
117116
{
118117
$this->expectException('Symfony\Component\DependencyInjection\Exception\RuntimeException');
118+
$this->expectExceptionMessage('Invalid service "Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy": method "setLogger()" does not exist.');
119119
$container = new ContainerBuilder();
120120

121121
$bindings = [
@@ -129,4 +129,25 @@ public function testWithNonExistingSetterAndBinding()
129129
$pass = new ResolveBindingsPass();
130130
$pass->process($container);
131131
}
132+
133+
public function testSyntheticServiceWithBind()
134+
{
135+
$container = new ContainerBuilder();
136+
$argument = new BoundArgument('bar');
137+
138+
$container->register('foo', 'stdClass')
139+
->addArgument(new Reference('synthetic.service'));
140+
141+
$container->register('synthetic.service')
142+
->setSynthetic(true)
143+
->setBindings(['$apiKey' => $argument]);
144+
145+
$container->register(NamedArgumentsDummy::class, NamedArgumentsDummy::class)
146+
->setBindings(['$apiKey' => $argument]);
147+
148+
(new ResolveBindingsPass())->process($container);
149+
(new DefinitionErrorExceptionPass())->process($container);
150+
151+
$this->assertSame([1 => 'bar'], $container->getDefinition(NamedArgumentsDummy::class)->getArguments());
152+
}
132153
}

0 commit comments

Comments
 (0)
0