diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/TestServiceContainerWeakRefPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/TestServiceContainerWeakRefPass.php index b704bf54e48a..62f17d64f144 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/TestServiceContainerWeakRefPass.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/TestServiceContainerWeakRefPass.php @@ -31,7 +31,7 @@ public function process(ContainerBuilder $container) $definitions = $container->getDefinitions(); foreach ($definitions as $id => $definition) { - if (!$definition->isPublic() && !$definition->getErrors() && !$definition->isAbstract()) { + if ((!$definition->isPublic() || $definition->isPrivate()) && !$definition->getErrors() && !$definition->isAbstract()) { $privateServices[$id] = new ServiceClosureArgument(new Reference($id, ContainerBuilder::IGNORE_ON_UNINITIALIZED_REFERENCE)); } } @@ -39,7 +39,7 @@ public function process(ContainerBuilder $container) $aliases = $container->getAliases(); foreach ($aliases as $id => $alias) { - if (!$alias->isPublic()) { + if (!$alias->isPublic() || $alias->isPrivate()) { while (isset($aliases[$target = (string) $alias])) { $alias = $aliases[$target]; } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/TestServiceContainer/NonPublicService.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/TestServiceContainer/NonPublicService.php new file mode 100644 index 000000000000..5c9261ac77bb --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/TestServiceContainer/NonPublicService.php @@ -0,0 +1,7 @@ +nonPublicService = $nonPublicService; + $this->privateService = $privateService; + } +} diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/TestServiceContainer/UnusedPrivateService.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/TestServiceContainer/UnusedPrivateService.php new file mode 100644 index 000000000000..25a7244a5015 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/TestServiceContainer/UnusedPrivateService.php @@ -0,0 +1,7 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\FrameworkBundle\Tests\Functional; + +use Symfony\Bundle\FrameworkBundle\Test\TestContainer; +use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\NonPublicService; +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\ContainerInterface; + +class TestServiceContainerTest extends WebTestCase +{ + public function testThatPrivateServicesAreUnavailableIfTestConfigIsDisabled() + { + static::bootKernel(array('test_case' => 'TestServiceContainer', 'root_config' => 'test_disabled.yml', 'environment' => 'test_disabled')); + + $this->assertInstanceOf(ContainerInterface::class, static::$container); + $this->assertNotInstanceOf(TestContainer::class, static::$container); + $this->assertTrue(static::$container->has(PublicService::class)); + $this->assertFalse(static::$container->has(NonPublicService::class)); + $this->assertFalse(static::$container->has(PrivateService::class)); + $this->assertFalse(static::$container->has('private_service')); + $this->assertFalse(static::$container->has(UnusedPrivateService::class)); + } + + public function testThatPrivateServicesAreAvailableIfTestConfigIsEnabled() + { + static::bootKernel(array('test_case' => 'TestServiceContainer')); + + $this->assertInstanceOf(TestContainer::class, static::$container); + $this->assertTrue(static::$container->has(PublicService::class)); + $this->assertTrue(static::$container->has(NonPublicService::class)); + $this->assertTrue(static::$container->has(PrivateService::class)); + $this->assertTrue(static::$container->has('private_service')); + $this->assertTrue(static::$container->has(UnusedPrivateService::class)); + } +} diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/TestServiceContainer/bundles.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/TestServiceContainer/bundles.php new file mode 100644 index 000000000000..144db9023603 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/TestServiceContainer/bundles.php @@ -0,0 +1,16 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use Symfony\Bundle\FrameworkBundle\FrameworkBundle; + +return array( + new FrameworkBundle(), +); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/TestServiceContainer/config.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/TestServiceContainer/config.yml new file mode 100644 index 000000000000..84af2df66271 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/TestServiceContainer/config.yml @@ -0,0 +1,6 @@ +imports: + - { resource: ../config/default.yml } + - { resource: services.yml } + +framework: + test: true diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/TestServiceContainer/services.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/TestServiceContainer/services.yml new file mode 100644 index 000000000000..523cca58d0b6 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/TestServiceContainer/services.yml @@ -0,0 +1,15 @@ +services: + Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\NonPublicService: + public: false + + Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\PrivateService: ~ + + Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\UnusedPrivateService: ~ + + private_service: '@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' diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/TestServiceContainer/test_disabled.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/TestServiceContainer/test_disabled.yml new file mode 100644 index 000000000000..6f1b62e4a7a2 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/TestServiceContainer/test_disabled.yml @@ -0,0 +1,6 @@ +imports: + - { resource: ../config/default.yml } + - { resource: services.yml } + +framework: + test: false