Description
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.