Skip to content

[FrameworkBundle] give access to non-shared services when using test.service_container #27528

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 15, 2018
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
@@ -0,0 +1,58 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler;

use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TestServiceContainerWeakRefPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TestServiceContainerRealRefPass;
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ServiceLocator;
use Symfony\Component\DependencyInjection\Reference;

class TestServiceContainerRefPassesTest extends TestCase
{
public function testProcess()
{
$container = new ContainerBuilder();
$container->register('test.private_services_locator', ServiceLocator::class)
->setPublic(true)
->addArgument(0, array());

$container->addCompilerPass(new TestServiceContainerWeakRefPass(), PassConfig::TYPE_BEFORE_REMOVING, -32);
$container->addCompilerPass(new TestServiceContainerRealRefPass(), PassConfig::TYPE_AFTER_REMOVING);

$container->register('Test\public_service')
->setPublic(true)
->addArgument(new Reference('Test\private_used_shared_service'))
->addArgument(new Reference('Test\private_used_non_shared_service'))
;

$container->register('Test\private_used_shared_service');
$container->register('Test\private_unused_shared_service');
$container->register('Test\private_used_non_shared_service')->setShared(false);
$container->register('Test\private_unused_non_shared_service')->setShared(false);

$container->compile();

$expected = array(
'Test\private_used_shared_service' => new ServiceClosureArgument(new Reference('Test\private_used_shared_service')),
'Test\private_used_non_shared_service' => new ServiceClosureArgument(new Reference('Test\private_used_non_shared_service')),
'Psr\Container\ContainerInterface' => new ServiceClosureArgument(new Reference('service_container')),
'Symfony\Component\DependencyInjection\ContainerInterface' => new ServiceClosureArgument(new Reference('service_container')),
);
$this->assertEquals($expected, $container->getDefinition('test.private_services_locator')->getArgument(0));
$this->assertSame($container, $container->get('test.private_services_locator')->get('Psr\Container\ContainerInterface'));
$this->assertFalse($container->getDefinition('Test\private_used_non_shared_service')->isShared());
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/FrameworkBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"php": "^7.1.3",
"ext-xml": "*",
"symfony/cache": "~3.4|~4.0",
"symfony/dependency-injection": "^4.1",
"symfony/dependency-injection": "^4.1.1",
"symfony/config": "~3.4|~4.0",
"symfony/event-dispatcher": "^4.1",
"symfony/http-foundation": "^4.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ private function isInlineableDefinition($id, Definition $definition, ServiceRefe
}

if (!$definition->isShared()) {
foreach ($graph->getNode($id)->getInEdges() as $edge) {
if ($edge->isWeak()) {
return false;
}
}

return true;
}

Expand Down