dominik
dominik

Reputation: 81

Unable to access to services in tests

When i try to invoke a container via

self::bootKernel();
$container = static::getContainer();

I get an error "LogicException: Could not find service "test.service_container". Try updating the "framework.test" config to "true"." Even though my config/packages/test/framework.php looks like this

return static function (FrameworkConfig $framework): void
{
    $framework
        ->test(true);

    $session = $framework->session();

    $session
        ->storageFactoryId('session.storage.factory.mock_file');
};

I can get to the container itself via

$container = self::$kernel->getContainer();

however, I cannot access any of my services. Decause I get an error every time "Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: The "App\User\Infrastructure\Repository\UserRepository" 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."

Upvotes: 5

Views: 3423

Answers (1)

dominik
dominik

Reputation: 81

The problem was solved by modifying the "configureContainer" method in the kernel. It was necessary to add import services_test.php

$container->import($this->getConfigDir() . '/{services}_' . $this->environment . '.php');

Upvotes: 2

Related Questions