8000 bug #27048 [FrameworkBundle] Register all private services on the tes… · symfony/symfony@2232d99 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2232d99

Browse files
bug #27048 [FrameworkBundle] Register all private services on the test service container (jakzal)
This PR was merged into the 4.1-dev branch. Discussion ---------- [FrameworkBundle] Register all private services on the test service container | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Before this fix, only services explicitly configured with public=false were exposed via the test service container: ```php $container->register(PrivateService::class, PrivateService::class)->setPublic(false); ``` but not those explicitly configured as private: ```php $container->register(PrivateService::class, PrivateService::class)->setPrivate(true); ``` nor those implicitly configured as private: ```php $container->register(PrivateService::class, PrivateService::class); ``` Commits ------- 6677393 [FrameworkBundle] Register all private services on the test service container
2 parents 0a83b17 + 6677393 commit 2232d99

File tree

10 files changed

+129
-2
lines changed

10 files changed

+129
-2
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/TestServiceContainerWeakRefPass.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ public function process(ContainerBuilder $container)
3131
$definitions = $container->getDefinitions();
3232

3333
foreach ($definitions as $id => $definition) {
34-
if (!$definition->isPublic() & 8000 & !$definition->getErrors() && !$definition->isAbstract()) {
34+
if ((!$definition->isPublic() || $definition->isPrivate()) && !$definition->getErrors() && !$definition->isAbstract()) {
3535
$privateServices[$id] = new ServiceClosureArgument(new Reference($id, ContainerBuilder::IGNORE_ON_UNINITIALIZED_REFERENCE));
3636
}
3737
}
3838

3939
$aliases = $container->getAliases();
4040

4141
foreach ($aliases as $id => $alias) {
42-
if (!$alias->isPublic()) {
42+
if (!$alias->isPublic() || $alias->isPrivate()) {
4343
while (isset($aliases[$target = (string) $alias])) {
4444
$alias = $aliases[$target];
4545
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer;
4+
5+
class NonPublicService
6+
{
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer;
4+
5+
class PrivateService
6+
{
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer;
4+
5+
class PublicService
6+
{
7+
private $nonPublicService;
8+
9+
private $privateService;
10+
11+
public function __construct(NonPublicService $nonPublicService, PrivateService $privateService)
12+
{
13+
$this->nonPublicService = $nonPublicService;
14+
$this->privateService = $privateService;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer;
4+
5+
class UnusedPrivateService
6+
{
7+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;
13+
14+
use Symfony\Bundle\FrameworkBundle\Test\TestContainer;
15+
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\NonPublicService;
16+
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\PrivateService;
17+
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\PublicService;
18+
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\UnusedPrivateService;
19+
use Symfony\Component\DependencyInjection\ContainerInterface;
20+
21+
class TestServiceContainerTest extends WebTestCase
22+
{
23+
public function testThatPrivateServicesAreUnavailableIfTestConfigIsDisabled()
24+
{
25+
static::bootKernel(array('test_case' => 'TestServiceContainer', 'root_config' => 'test_disabled.yml', 'environment' => 'test_disabled'));
26+
27+
$this->assertInstanceOf(ContainerInterface::class, static::$container);
28+
$this->assertNotInstanceOf(TestContainer::class, static::$container);
29+
$this->assertTrue(static::$container->has(PublicService::class));
30+
$this->assertFalse(static::$container->has(NonPublicService::class));
31+
$this->assertFalse(static::$container->has(PrivateService::class));
32+
$this->assertFalse(static::$container->has('private_service'));
33+
$this->assertFalse(static::$container->has(UnusedPrivateService::class));
34+
}
35+
36+
public function testThatPrivateServicesAreAvailableIfTestConfigIsEnabled()
37+
{
38+
static::bootKernel(array('test_case' => 'TestServiceContainer'));
39+
40+
$this->assertInstanceOf(TestContainer::class, static::$container);
41+
$this-> F438 ;assertTrue(static::$container->has(PublicService::class));
42+
$this->assertTrue(static::$container->has(NonPublicService::class));
43+
$this->assertTrue(static::$container->has(PrivateService::class));
44+
$this->assertTrue(static::$container->has('private_service'));
45+
$this->assertTrue(static::$container->has(UnusedPrivateService::class));
46+
}
47+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
13+
14+
return array(
15+
new FrameworkBundle(),
16+
);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
imports:
2+
- { resource: ../config/default.yml }
3+
- { resource: services.yml }
4+
5+
framework:
6+
test: true
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
services:
2+
Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\NonPublicService:
3+
public: false
4+
5+
Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\PrivateService: ~
6+
7+
Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\UnusedPrivateService: ~
8+
9+
private_service: '@Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\PrivateService'
10+
11+
Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\PublicService:
12+
public: true
13+
arguments:
14+
- '@Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\NonPublicService'
15+
- '@Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer\PrivateService'
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
imports:
2+
- { resource: ../config/default.yml }
3+
- { resource: services.yml }
4+
5+
framework:
6+
test: false

0 commit comments

Comments
 (0)
0