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

DI Container should be used to instantiate integration tests #25814

Copy link
Copy link
Closed
@fracz

Description

@fracz
Issue body actions
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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    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.