Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

545
Views
¿Cómo acceder a un servicio privado en las pruebas de phpunit de Symfony 5.3?

Quiero definir un caso de prueba funcional para mis pruebas de phpunit para mi aplicación Symfony 5.3 que requiere el servicio privado security.password_hasher del contenedor.

Obtengo la siguiente excepción

  1. App\Tests\Functional\SiteResourceTest::testCreateSite Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException : El servicio o alias security.password_hasher se eliminó o se incorporó cuando se compiló el contenedor. Debe hacerlo público o dejar de usar el contenedor directamente y usar la inyección de dependencia en su lugar.

Seguí las instrucciones de la documentación sobre la recuperación de servicios en la prueba.

¿Qué estoy haciendo mal? ¿Cómo puedo arreglar esto?

 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); } }
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Lo resolví haciendo que el servicio sea explícitamente público en services_test.yaml :

 services: Symfony\Component\PasswordHasher\Hasher\UserPasswordHasher: public: true

Y luego recuperar el servicio por su nombre de clase

 $this->passwordHasher = $container->get(UserPasswordHasher::class);
over 4 years ago · Santiago Trujillo Report

0

Tuve que reescribir la definición completa del servicio para evitar el siguiente error durante la compilación del caché:

La definición de "security.user_password_hasher" no tiene clase. Si tiene la intención de inyectar este servicio dinámicamente en tiempo de ejecución, márquelo como synthetic=true. Si esta es una definición abstracta utilizada únicamente por definiciones secundarias, agregue abstract=true; de lo contrario, especifique una clase para eliminar este error.

servicios.yaml

 security.user_password_hasher: class: Symfony\Component\PasswordHasher\Hasher\UserPasswordHasher public: true arguments: [ '@security.password_hasher_factory' ]
over 4 years ago · Santiago Trujillo Report

0

Un kernel de prueba también puede hacer lo siguiente:

 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); } }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!