Description
Q | A |
---|---|
Bug report? | no |
Feature request? | yes |
BC Break report? | no |
RFC? | yes |
Symfony version | 3.4 |
IMO, testing of services from DI container deserves some love. Currently, some workarouds are being planned so testing a private service is still possible in Symfony 4.x and does not throw deprecation errors in 3.2+. See: symfony/symfony-docs#8097 and #24543
Having some Java background, I have a suggestion that tests should be instantiated by the container just like other production services and they should expose their dependencies via setters or constructor. I mean:
class PostControllerTest extends ContainerAutoconfiguredTestCaseOrWhatever
{
/** @var MyPrivateService */
private $serviceToTest;
public function __construct(MyPrivateService $service) {
$this->serviceToTest = $service;
}
public function testSomething() {
//...
}
}
In Java with Spring IoC Container this is achieved by custom test runner that registers appropriate listeners that inject required dependencies after test instantiation and before its launch. Spring docs.
Is it possible in PHP and PHPUnit? Do you think this would mitigate some "hacky" configuration of making some services non-container-private just for sake of testing?