diff --git a/src/Symfony/Bundle/FrameworkBundle/Client.php b/src/Symfony/Bundle/FrameworkBundle/Client.php index bc76ce28f0cd..1499d050370a 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Client.php +++ b/src/Symfony/Bundle/FrameworkBundle/Client.php @@ -30,13 +30,15 @@ class Client extends BaseClient private $hasPerformedRequest = false; private $profiler = false; private $reboot = true; + private $container; /** * {@inheritdoc} */ - public function __construct(KernelInterface $kernel, array $server = array(), History $history = null, CookieJar $cookieJar = null) + public function __construct(KernelInterface $kernel, array $server = array(), History $history = null, CookieJar $cookieJar = null, ContainerInterface $container = null) { parent::__construct($kernel, $server, $history, $cookieJar); + $this->container = $container; } /** @@ -46,7 +48,7 @@ public function __construct(KernelInterface $kernel, array $server = array(), Hi */ public function getContainer() { - return $this->kernel->getContainer(); + return $this->container ?? $this->kernel->getContainer(); } /** diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/TestServiceContainerRealRefPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/TestServiceContainerRealRefPass.php new file mode 100644 index 000000000000..9e36a80d00ce --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/TestServiceContainerRealRefPass.php @@ -0,0 +1,39 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler; + +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Reference; + +/** + * @author Nicolas Grekas + */ +class TestServiceContainerRealRefPass implements CompilerPassInterface +{ + public function process(ContainerBuilder $container) + { + if (!$container->hasDefinition('test.service_container')) { + return; + } + + $testContainer = $container->getDefinition('test.service_container'); + $privateContainer = $container->getDefinition((string) $testContainer->getArgument(2)); + $definitions = $container->getDefinitions(); + + foreach ($privateContainer->getArgument(0) as $id => $argument) { + if (isset($definitions[$target = (string) $argument->getValues()[0]])) { + $argument->setValues(array(new Reference($target))); + } + } + } +} diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/TestServiceContainerWeakRefPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/TestServiceContainerWeakRefPass.php new file mode 100644 index 000000000000..b704bf54e48a --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/TestServiceContainerWeakRefPass.php @@ -0,0 +1,56 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler; + +use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument; +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Reference; + +/** + * @author Nicolas Grekas + */ +class TestServiceContainerWeakRefPass implements CompilerPassInterface +{ + public function process(ContainerBuilder $container) + { + if (!$container->hasDefinition('test.service_container')) { + return; + } + + $privateServices = array(); + $definitions = $container->getDefinitions(); + + foreach ($definitions as $id => $definition) { + if (!$definition->isPublic() && !$definition->getErrors() && !$definition->isAbstract()) { + $privateServices[$id] = new ServiceClosureArgument(new Reference($id, ContainerBuilder::IGNORE_ON_UNINITIALIZED_REFERENCE)); + } + } + + $aliases = $container->getAliases(); + + foreach ($aliases as $id => $alias) { + if (!$alias->isPublic()) { + while (isset($aliases[$target = (string) $alias])) { + $alias = $aliases[$target]; + } + if (isset($definitions[$target]) && !$definitions[$target]->getErrors() && !$definitions[$target]->isAbstract()) { + $privateServices[$id] = new ServiceClosureArgument(new Reference($target, ContainerBuilder::IGNORE_ON_UNINITIALIZED_REFERENCE)); + } + } + } + + if ($privateServices) { + $definitions[(string) $definitions['test.service_container']->getArgument(2)]->replaceArgument(0, $privateServices); + } + } +} diff --git a/src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php b/src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php index f25bb71240b9..9511c4bc61ce 100644 --- a/src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php +++ b/src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php @@ -23,6 +23,8 @@ use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\LoggingTranslatorPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddExpressionLanguageProvidersPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ContainerBuilderDebugDumpPass; +use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TestServiceContainerWeakRefPass; +use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TestServiceContainerRealRefPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\UnusedTagsPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\WorkflowGuardListenerPass; use Symfony\Component\Console\Application; @@ -114,6 +116,8 @@ public function build(ContainerBuilder $container) $this->addCompilerPassIfExists($container, FormPass::class); $container->addCompilerPass(new WorkflowGuardListenerPass()); $container->addCompilerPass(new ResettableServicePass()); + $container->addCompilerPass(new TestServiceContainerWeakRefPass(), PassConfig::TYPE_BEFORE_REMOVING, -32); + $container->addCompilerPass(new TestServiceContainerRealRefPass(), PassConfig::TYPE_AFTER_REMOVING); if ($container->getParameter('kernel.debug')) { $container->addCompilerPass(new AddDebugLogProcessorPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -32); diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/test.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/test.xml index ff109c4cd242..d7aab2e068a5 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/test.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/test.xml @@ -16,6 +16,7 @@ %test.client.parameters% + @@ -33,5 +34,15 @@ + + + + + + + + + + diff --git a/src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php b/src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php index be396e055a73..8dfc292073ed 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php +++ b/src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php @@ -12,6 +12,7 @@ namespace Symfony\Bundle\FrameworkBundle\Test; use PHPUnit\Framework\TestCase; +use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ResettableContainerInterface; use Symfony\Component\HttpKernel\KernelInterface; @@ -29,6 +30,11 @@ abstract class KernelTestCase extends TestCase */ protected static $kernel; + /** + * @var ContainerInterface + */ + protected static $container; + /** * @return string The Kernel class name * @@ -60,6 +66,9 @@ protected static function bootKernel(array $options = array()) static::$kernel = static::createKernel($options); static::$kernel->boot(); + $container = static::$kernel->getContainer(); + static::$container = $container->has('test.service_container') ? $container->get('test.service_container') : $container; + return static::$kernel; } diff --git a/src/Symfony/Bundle/FrameworkBundle/Test/TestContainer.php b/src/Symfony/Bundle/FrameworkBundle/Test/TestContainer.php new file mode 100644 index 000000000000..451faa89bd9e --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Test/TestContainer.php @@ -0,0 +1,105 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\FrameworkBundle\Test; + +use Psr\Container\ContainerInterface as PsrContainerInterface; +use Symfony\Component\DependencyInjection\Container; +use Symfony\Component\DependencyInjection\ContainerInterface as SymfonyContainerInterface; +use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; + +/** + * @author Nicolas Grekas + */ +class TestContainer extends Container +{ + private $publicContainer; + private $privateContainer; + + public function __construct(?ParameterBagInterface $parameterBag, SymfonyContainerInterface $publicContainer, PsrContainerInterface $privateContainer) + { + $this->parameterBag = $parameterBag ?? $publicContainer->getParameterBag(); + $this->publicContainer = $publicContainer; + $this->privateContainer = $privateContainer; + } + + /** + * {@inheritdoc} + */ + public function compile() + { + $this->publicContainer->compile(); + } + + /** + * {@inheritdoc} + */ + public function isCompiled() + { + return $this->publicContainer->isCompiled(); + } + + /** + * {@inheritdoc} + */ + public function set($id, $service) + { + $this->publicContainer->set($id, $service); + } + + /** + * {@inheritdoc} + */ + public function has($id) + { + return $this->publicContainer->has($id) || $this->privateContainer->has($id); + } + + /** + * {@inheritdoc} + */ + public function get($id, $invalidBehavior = /* self::EXCEPTION_ON_INVALID_REFERENCE */ 1) + { + return $this->privateContainer->has($id) ? $this->privateContainer->get($id) : $this->publicContainer->get($id, $invalidBehavior); + } + + /** + * {@inheritdoc} + */ + public function initialized($id) + { + return $this->publicContainer->initialized($id); + } + + /** + * {@inheritdoc} + */ + public function reset() + { + $this->publicContainer->reset(); + } + + /** + * {@inheritdoc} + */ + public function getServiceIds() + { + return $this->publicContainer->getServiceIds(); + } + + /** + * {@inheritdoc} + */ + public function getRemovedIds() + { + return $this->publicContainer->getRemovedIds(); + } +} diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/PropertyInfoTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/PropertyInfoTest.php index f98072ce7b39..6adde224275b 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/PropertyInfoTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/PropertyInfoTest.php @@ -18,9 +18,8 @@ class PropertyInfoTest extends WebTestCase public function testPhpDocPriority() { static::bootKernel(array('test_case' => 'Serializer')); - $container = static::$kernel->getContainer(); - $this->assertEquals(array(new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_INT))), $container->get('test.property_info')->getTypes('Symfony\Bundle\FrameworkBundle\Tests\Functional\Dummy', 'codes')); + $this->assertEquals(array(new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_INT))), static::$container->get('property_info')->getTypes('Symfony\Bundle\FrameworkBundle\Tests\Functional\Dummy', 'codes')); } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/Serializer/config.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/Serializer/config.yml index e4090041bb19..cac135c315d0 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/Serializer/config.yml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/Serializer/config.yml @@ -1,10 +1,6 @@ imports: - { resource: ../config/default.yml } -services: - _defaults: { public: true } - test.property_info: '@property_info' - framework: serializer: { enabled: true } property_info: { enabled: true } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/config/framework.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/config/framework.yml index a313b3339a08..84c6e64e2a91 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/config/framework.yml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/config/framework.yml @@ -4,7 +4,7 @@ framework: validation: { enabled: true, enable_annotations: true } csrf_protection: true form: true - test: ~ + test: true default_locale: en session: storage_id: session.storage.mock_file