如何在 Symfony 5.3 phpunit 测试中访问私人服务?

How to access a private service in Symfony 5.3 phpunit tests?

我想为我的 Symfony 5.3 应用程序的 phpunit-tests 定义一个功能测试用例,它需要来自容器的私有服务 security.password_hasher

我得到以下执行

  1. App\Tests\Functional\SiteResourceTest::testCreateSite Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: The security.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.

我按照有关 retrieving services in the test

的文档中的说明进行操作

我做错了什么?我该如何解决这个问题?

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);
    }
}

我通过在 services_test.yaml:

中显式设置服务 public 解决了这个问题
services:
    Symfony\Component\PasswordHasher\Hasher\UserPasswordHasher:
        public: true

然后通过类名检索服务

$this->passwordHasher = $container->get(UserPasswordHasher::class);

我不得不重写服务的完整定义以防止缓存编译期间出现以下错误:

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' ]

测试内核还可以执行以下操作:

class PublicService implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
        foreach ($container->getDefinitions() as $id => $definition) {
            if (stripos($id, 'whatEverIWant') === 0) {
                $definition->setPublic(true);
            }
        }
        foreach ($container->getAliases() as $id => $definition) {
            if (stripos($id, 'whatEverIWant') === 0) {
                $definition->setPublic(true);
            }
        }
    }
}

class AppKernel extends BaseKernel
{
    public function build(ContainerBuilder $container)
    {
        $container->addCompilerPass(new PublicService(), PassConfig::TYPE_OPTIMIZE);
        parent::build($container);
    }
}