8000 bug #50710 [FrameworkBundle] Fix setting decorated services during te… · symfony/framework-bundle@930873e · GitHub
[go: up one dir, main page]

Skip to content

Commit 930873e

Browse files
committed
bug #50710 [FrameworkBundle] Fix setting decorated services during tests (nicolas-grekas)
This PR was merged into the 6.3 branch. Discussion ---------- [FrameworkBundle] Fix setting decorated services during tests | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - When a service is decorated, setting this service with the test container should set the inner service, not the outer one. Spotted while trying to set a custom scoped http-client in a test case, which was failing because this replaced the traceable+retryable+etc decoration chain we put on top of custom clients. Commits ------- b08f3cddc9 [FrameworkBundle] Fix setting decorated services during tests
2 parents a5c9e59 + a458e06 commit 930873e

File tree

6 files changed

+43
-62
lines changed

6 files changed

+43
-62
lines changed

DependencyInjection/Compiler/TestServiceContainerRealRefPass.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ public function process(ContainerBuilder $container)
4141
if ($id !== $target) {
4242
$renamedIds[$id] = $target;
4343
}
44+
if ($inner = $definitions[$target]->getTag('container.decorator')[0]['inner'] ?? null) {
45+
$renamedIds[$id] = $inner;
46+
}
4447
} else {
4548
unset($privateServices[$id]);
4649
}

Tests/Functional/Bundle/TestBundle/TestServiceContainer/PrivateService.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@
1313

1414
class PrivateService
1515
{
16+
public $inner;
1617
}

Tests/Functional/KernelTestCaseTest.php

Lines changed: 0 additions & 61 deletions
This file was deleted.

Tests/Functional/TestServiceContainerTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\PrivateService;
1717
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\PublicService;
1818
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\UnusedPrivateService;
19+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
1920

2021
class TestServiceContainerTest extends AbstractWebTestCase
2122
{
@@ -40,6 +41,33 @@ public function testThatPrivateServicesAreAvailableIfTestConfigIsEnabled()
4041
$this->assertFalse(static::getContainer()->has(UnusedPrivateService::class));
4142
}
4243

44+
public function testThatPrivateServicesCanBeSetIfTestConfigIsEnabled()
45+
{
46+
static::bootKernel(['test_case' => 'TestServiceContainer']);
47+
48+
$container = static::getContainer();
49+
50+
$service = new \stdClass();
51+
52+
$container->set('private_service', $service);
53+
$this->assertSame($service, $container->get('private_service'));
54+
55+
$this->expectException(InvalidArgumentException::class);
56+
$this->expectExceptionMessage('The "private_service" service is already initialized, you cannot replace it.');
57+
$container->set('private_service', new \stdClass());
58+
}
59+
60+
public function testSetDecoratedService()
61+
{
62+
static::bootKernel(['test_case' => 'TestServiceContainer']);
63+
64+
$container = static::getContainer();
65+
66+
$service = new PrivateService();
67+
$container->set('decorated', $service);
68+
$this->assertSame($service, $container->get('decorated')->inner);
69+
}
70+
4371
/**
4472
* @doesNotPerformAssertions
4573
*/

Tests/Functional/app/TestServiceContainer/services.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,18 @@ services:
88

99
private_service: '@Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\PrivateService'
1010

11+
decorated:
12+
class: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\PrivateService
13+
1114
Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\PublicService:
1215
public: true
1316
arguments:
1417
- '@Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\NonPublicService'
1518
- '@Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\PrivateService'
19+
- '@decorated'
20+
21+
decorator:
22+
class: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\PrivateService
23+
decorates: decorated
24+
properties:
25+
inner: '@.inner'

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"ext-xml": "*",
2222
"symfony/cache": "^5.4|^6.0",
2323
"symfony/config": "^6.1",
24-
"symfony/dependency-injection": "^6.3",
24+
"symfony/dependency-injection": "^6.3.1",
2525
"symfony/deprecation-contracts": "^2.5|^3",
2626
"symfony/error-handler": "^6.1",
2727
"symfony/event-dispatcher": "^5.4|^6.0",

0 commit comments

Comments
 (0)
0