I want to define a functional testcase for my phpunit-tests for my Symfony 5.3 application which requires the private service security.password_hasher
from the container.
I get the following execption
App\Tests\Functional\SiteResourceTest::testCreateSite
Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: Thesecurity.password_hasher
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.
I followed the instructions from the documentation about retrieving services in the test
What am i doing wrong? How can i fix this?
class CustomApiTestCase extends ApiTestCase
{
protected UserPasswordHasher $passwordHasher;
protected function setUp(): void
{
// (1) boot the Symfony kernel
self::bootKernel();
// (2) use static::getContainer() to access the service container
$container = static::getContainer();
// (3) run some service & test the result
$this->passwordHasher = $container->get('security.password_hasher');
}
protected function createUser(
string $email,
string $password,
): User {
$user = new User();
$user->setEmail($email);
$encoded = $this->passwordHasher->hash($password);
$user->setPassword($encoded);
$em = self::getContainer()->get('doctrine')->getManager();
$em->persist($user);
$em->flush();
return $user;
}
protected function createUserAndLogIn(Client $client, string $email, string $password): User
{
$user = $this->createUser($email, $password);
$this->logIn($client, $email, $password);
return $user;
}
protected function logIn(Client $client, string $email, string $password)
{
$client->request('POST', '/login', [
'headers' => ['Content-Type' => 'application/json'],
'json' => [
'email' => $email,
'password' => $password
],
]);
$this->assertResponseStatusCodeSame(204);
}
}
I had to rewrite the complete definition of the service to prevent the following error during cache compilation:
The definition for "security.user_password_hasher" has no class. If you intend to inject this service dynamically at runtime, please mark it as synthetic=true. If this is an abstract definition solely used by child definitions, please add abstract=true, otherwise specify a class to get rid of this error.
services.yaml
security.user_password_hasher:
class: Symfony\Component\PasswordHasher\Hasher\UserPasswordHasher
public: true
arguments:
[ '@security.password_hasher_factory' ]