Skip to content

Test service container #4

Closed
Closed
@ossinkine

Description

@ossinkine

Symfony 4.1 introduced new testing service container which allows to get private services in tests.
For example following code is correct:

namespace App\Tests\Controller;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Tools\SchemaTool;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;

class DefaultControllerTest extends WebTestCase
{
    public function testIndex(): void
    {
        $client = static::createClient();

        /** @var ContainerInterface $container */
        $container = $client->getContainer();
        $entityManager = $container->get(EntityManagerInterface::class);
        // do something with entity manager

        $client->request('GET', '/');

        $this->assertSame(200, $client->getResponse()->getStatusCode());
    }
}

But PHPStan throws error "Service "Doctrine\ORM\EntityManagerInterface" is private."

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions