8000 [FrameworkBundle] Fix setting decorated services during tests by nicolas-grekas · Pull Request #50710 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[FrameworkBundle] Fix setting decorated services during tests #50710

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public function process(ContainerBuilder $container)
if ($id !== $target) {
$renamedIds[$id] = $target;
}
if ($inner = $definitions[$target]->getTag('container.decorator')[0]['inner'] ?? null) {
$renamedIds[$id] = $inner;
}
} else {
unset($privateServices[$id]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@

class PrivateService
{
public $inner;
}

This file was deleted.

10000
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\PrivateService;
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\PublicService;
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\UnusedPrivateService;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;

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

public function testThatPrivateServicesCanBeSetIfTestConfigIsEnabled()
{
static::bootKernel(['test_case' => 'TestServiceContainer']);

$container = static::getContainer();

$service = new \stdClass();

$container->set('private_service', $service);
$this->assertSame($service, $container->get('private_service'));

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The "private_service" service is already initialized, you cannot replace it.');
$container->set('private_service', new \stdClass());
}

public function testSetDecoratedService()
{
static::bootKernel(['test_case' => 'TestServiceContainer']);

$container = static::getContainer();

$service = new PrivateService();
$container->set('decorated', $service);
$this->assertSame($service, $container->get('decorated')->inner);
}

/**
* @doesNotPerformAssertions
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,18 @@ services:

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

decorated:
class: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\PrivateService

Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\PublicService:
public: true
arguments:
- '@Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\NonPublicService'
- '@Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\PrivateService'
- '@decorated'

decorator:
class: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\PrivateService
decorates: decorated
properties:
inner: '@.inner'
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/FrameworkBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"ext-xml": "*",
"symfony/cache": "^5.4|^6.0",
"symfony/config": "^6.1",
"symfony/dependency-injection": "^6.3",
"symfony/dependency-injection": "^6.3.1",
"symfony/deprecation-contracts": "^2.5|^3",
"symfony/error-handler": "^6.1",
"symfony/event-dispatcher": "^5.4|^6.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function process(ContainerBuilder $container)
$definitions->insert([$id, $definition], [$decorated[2], --$order]);
}
$decoratingDefinitions = [];
$decoratedIds = [];

$tagsToKeep = $container->hasParameter('container.behavior_describing_tags')
? $container->getParameter('container.behavior_describing_tags')
Expand All @@ -58,6 +59,7 @@ public function process(ContainerBuilder $container)
$renamedId = $id.'.inner';
}

$decoratedIds[$inner] ??= $renamedId;
$this->currentId = $renamedId;
$this->processValue($definition);

Expand Down Expand Up @@ -114,7 +116,7 @@ public function process(ContainerBuilder $container)
}

foreach ($decoratingDefinitions as $inner => $definition) {
$definition->addTag('container.decorator', ['id' => $inner]);
$definition->addTag('container.decorator', ['id' => $inner, 'inner' => $decoratedIds[$inner]]);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public function testProcessWithInvalidDecorated()
public function testProcessNoInnerAliasWithInvalidDecorated()
{
$container = new ContainerBuilder();
$decoratorDefinition = $container
$container
->register('decorator')
->setDecoratedService('unknown_decorated', null, 0, ContainerInterface::NULL_ON_INVALID_REFERENCE)
;
Expand All @@ -173,7 +173,7 @@ public function testProcessNoInnerAliasWithInvalidDecorated()
public function testProcessWithInvalidDecoratedAndWrongBehavior()
{
$container = new ContainerBuilder();
$decoratorDefinition = $container
$container
->register('decorator')
->setDecoratedService('unknown_decorated', null, 0, 12)
;
Expand All @@ -198,7 +198,7 @@ public function testProcessMovesTagsFromDecoratedDefinitionToDecoratingDefinitio
$this->process($container);

$this->assertEmpty($container->getDefinition('baz.inner')->getTags());
$this->assertEquals(['bar' => ['attr' => 'baz'], 'foobar' => ['attr' => 'bar'], 'container.decorator' => [['id' => 'foo']]], $container->getDefinition('baz')->getTags());
$this->assertEquals(['bar' => ['attr' => 'baz'], 'foobar' => ['attr' => 'bar'], 'container.decorator' => [['id' => 'foo', 'inner' => 'baz.inner']]], $container->getDefinition('baz')->getTags());
}

public function testProcessMovesTagsFromDecoratedDefinitionToDecoratingDefinitionMultipleTimes()
Expand All @@ -221,7 +221,7 @@ public function testProcessMovesTagsFromDecoratedDefinitionToDecoratingDefinitio
$this->process($container);

$this->assertEmpty($container->getDefinition('deco1')->getTags());
$this->assertEquals(['bar' => ['attr' => 'baz'], 'container.decorator' => [['id' => 'foo']]], $container->getDefinition('deco2')->getTags());
$this->assertEquals(['bar' => ['attr' => 'baz'], 'container.decorator' => [['id' => 'foo', 'inner' => 'deco1.inner']]], $container->getDefinition('deco2')->getTags());
}

public function testProcessLeavesServiceLocatorTagOnOriginalDefinition()
Expand All @@ -240,7 +240,7 @@ public function testProcessLeavesServiceLocatorTagOnOriginalDefinition()
$this->process($container);

$this->assertEquals(['container.service_locator' => [0 => []]], $container->getDefinition('baz.inner')->getTags());
$this->assertEquals(['bar' => ['attr' => 'baz'],  F438 9;foobar' => ['attr' => 'bar'], 'container.decorator' => [['id' => 'foo']]], $container->getDefinition('baz')->getTags());
$this->assertEquals(['bar' => ['attr' => 'baz'], 'foobar' => ['attr' => 'bar'], 'container.decorator' => [['id' => 'foo', 'inner' => 'baz.inner']]], $container->getDefinition('baz')->getTags());
}

public function testProcessLeavesServiceSubscriberTagOnOriginalDefinition()
Expand All @@ -259,7 +259,7 @@ public function testProcessLeavesServiceSubscriberTagOnOriginalDefinition()
$this->process($container);

$this->assertEquals(['container.service_subscriber' => [], 'container.service_subscriber.locator' => []], $container->getDefinition('baz.inner')->getTags());
$this->assertEquals(['bar' => ['attr' => 'baz'], 'foobar' => ['attr' => 'bar'], 'container.decorator' => [['id' => 'foo']]], $container->getDefinition('baz')->getTags());
$this->assertEquals(['bar' => ['attr' => 'baz'], 'foobar' => ['attr' => 'bar'], 'container.decorator' => [['id' => 'foo', 'inner' => 'baz.inner']]], $container->getDefinition('baz')->getTags());
}

public function testProcessLeavesProxyTagOnOriginalDefinition()
Expand All @@ -278,7 +278,7 @@ public function testProcessLeavesProxyTagOnOriginalDefinition()
$this->process($container);

$this->assertEquals(['proxy' => 'foo'], $container->getDefinition('baz.inner')->getTags());
$this->assertEquals(['bar' => ['attr' => 'baz'], 'foobar' => ['attr' => 'bar'], 'container.decorator' => [['id' => 'foo']]], $container->getDefinition('baz')->getTags());
$this->assertEquals(['bar' => ['attr' => 'baz'], 'foobar' => ['attr' => 'bar'], 'container.decorator' => [['id' => 'foo', 'inner' => 'baz.inner']]], $container->getDefinition('baz')->getTags());
}

public function testCannotDecorateSyntheticService()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ services:
class: Symfony\Component\DependencyInjection\Tests\Fixtures\StdClassDecorator
public: true
tags:
- container.decorator: { id: decorated }
- container.decorator: { id: decorated, inner: decorator42 }
arguments: [!service { class: stdClass }]
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ services:
class: Class2
public: true
tags:
- container.decorator: { id: bar }
- container.decorator: { id: bar, inner: b }
file: file.php
lazy: true
arguments: [!service { class: Class1 }]
3DDE Expand Down
0