Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Private service not injected anywhere is not found in test #29603

Copy link
Copy link
Closed
@Nek-

Description

@Nek-
Issue body actions

Symfony version(s) affected: >=4.1

Description
Symfony 4.1 introduces a new container used in tests. It allows you to retrieve a private service. This is a really nice improvement. But it's in conflict with another feature: private services that are not injected are removed from the container.

In case you test a service using the new test container, it may result in "service not found exception". This case happens a lot in TDD development.

It may be understandable and an acceptable behavior (after all, that's what happen in your real application, the same behavior is expected when you get the container) but when you try to get the service in debug:container command, everything works without telling you the service will be removed! This is leading to a misunderstanding of what is happening and naturally, headache.

How to reproduce

// not used anywhere
class AwesomeService {}
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
class AwesomeServiceTest extends KernelTestCase
{
    public testSomething()
    {
        self::bootKernel();
        $awesomeService = self::$container->get(AwesomeService::class);
    }
}

Result in:

Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: The "AwesomeService" service or alias has been removed or inlined when the container was compiled. You should either make it public, or stop using the container directly and use dependency injection instead.

Possible Solution

When I do bin/console debug:container AwesomeService, I should see in the table: warning: this service is not injected and will be remove before runtime or something like that.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.