Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #26499 [FrameworkBundle] Allow fetching private services from…
… test clients (nicolas-grekas) This PR was merged into the 4.1-dev branch. Discussion ---------- [FrameworkBundle] Allow fetching private services from test clients | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #25814 | License | MIT | Doc PR | - With this PR, `$client->getContainer()` returns a special container that gives access to private services as if they were public. Tests derived from `WebTestCase` and `KernelTestCase` can access this special container by using the new `static::$container` property. Commits ------- a840809 [FrameworkBundle] Allow fetching private services from test clients
- Loading branch information
Showing
10 changed files
with
230 additions
and
9 deletions.
There are no files selected for viewing
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
39 changes: 39 additions & 0 deletions
39
...y/Bundle/FrameworkBundle/DependencyInjection/Compiler/TestServiceContainerRealRefPass.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,39 @@ | ||
<?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\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
/** | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
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))); | ||
} | ||
} | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...y/Bundle/FrameworkBundle/DependencyInjection/Compiler/TestServiceContainerWeakRefPass.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,56 @@ | ||
<?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\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 <p@tchwork.com> | ||
*/ | ||
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); | ||
} | ||
} | ||
} |
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
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
105 changes: 105 additions & 0 deletions
105
src/Symfony/Bundle/FrameworkBundle/Test/TestContainer.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,105 @@ | ||
<?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\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 <p@tchwork.com> | ||
*/ | ||
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(); | ||
} | ||
} |
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
4 changes: 0 additions & 4 deletions
4
src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/Serializer/config.yml
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 |
---|---|---|
@@ -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 } |
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