Closed
Description
Q | A |
---|---|
Bug report? | yes |
Feature request? | no |
BC Break report? | no |
RFC? | ? |
Symfony version | 4.1.x |
Related
- [FrameworkBundle] Allow fetching private services from test clients #26499
- [DI] Allow setting any public non-initialized services #24418
- Injecting dependencies and using Phpunit mocks after 3.2 deprecation #23311
The issues above demonstrate the newly committed functionality whereby we can freely access private services from the test client in phpunit test classes. Based on those issues, and the current documentation on symfony.com, I have come up with the following code, which works for my purposes:
class MyWebTestCase extends WebTestCase
{
public function setUp()
{
parent::setUp();
$this->client = static::createClient();
$this->client->disableReboot();
$this->container = $this->client->getContainer();
}
public function testSomething()
{
....
$this->container->get(MyCustomService::class); // WARNING: MyCustomService service is private, getting it from the container is deprecated since Symfony 3.2 and will fail in 4.0
....
}
}
class MyCustomConsoleCommandTestCase extends WebTestCase
{
public function setUp()
{
parent::setUp();
$this->application = new Application(self::bootKernel());
$this->client = static::createClient();
$this->container = $this->client->getContainer();
}
public function testSomething()
{
....
$this->container->get(MyCustomService::class); // WARNING: MyCustomService service is private, getting it from the container is deprecated since Symfony 3.2 and will fail in 4.0
....
}
}
While the code works, the calls to the test client container's get()
method produce the familiar service is private, getting it from the container is deprecated since Symfony 3.2 and will fail in 4.0
deprecation warning. If my approach to accessing the test container is incorrect, please let me know. Otherwise, shouldn't PhpUnitBridge
not throw said deprecation warning in these testing contexts?