Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug #27528 [FrameworkBundle] give access to non-shared services when …
…using test.service_container (nicolas-grekas) This PR was merged into the 4.1 branch. Discussion ---------- [FrameworkBundle] give access to non-shared services when using test.service_container | Q | A | ------------- | --- | Branch? | 4.1 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #27488 | License | MIT | Doc PR | - Commits ------- 516ff5a [FrameworkBundle] give access to non-shared services when using test.service_container
- Loading branch information
Showing
3 changed files
with
65 additions
and
1 deletion.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
.../FrameworkBundle/Tests/DependencyInjection/Compiler/TestServiceContainerRefPassesTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters