Skip to content

Commit

Permalink
bug #27048 [FrameworkBundle] Register all private services on the tes…
Browse files Browse the repository at this point in the history
…t 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
  • Loading branch information
nicolas-grekas committed Apr 25, 2018
2 parents 0a83b17 + 6677393 commit 2232d99
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 2 deletions.
Expand Up @@ -31,15 +31,15 @@ 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));
}
}

$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];
}
Expand Down
@@ -0,0 +1,7 @@
<?php

namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer;

class NonPublicService
{
}
@@ -0,0 +1,7 @@
<?php

namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer;

class PrivateService
{
}
@@ -0,0 +1,16 @@
<?php

namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer;

class PublicService
{
private $nonPublicService;

private $privateService;

public function __construct(NonPublicService $nonPublicService, PrivateService $privateService)
{
$this->nonPublicService = $nonPublicService;
$this->privateService = $privateService;
}
}
@@ -0,0 +1,7 @@
<?php

namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestServiceContainer;

class UnusedPrivateService
{
}
@@ -0,0 +1,47 @@
<?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\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));
}
}
@@ -0,0 +1,16 @@
<?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.
*/

use Symfony\Bundle\FrameworkBundle\FrameworkBundle;

return array(
new FrameworkBundle(),
);
@@ -0,0 +1,6 @@
imports:
- { resource: ../config/default.yml }
- { resource: services.yml }

framework:
test: true
@@ -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'
@@ -0,0 +1,6 @@
imports:
- { resource: ../config/default.yml }
- { resource: services.yml }

framework:
test: false

0 comments on commit 2232d99

Please sign in to comment.