亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Symfony 測試 - 帶有 Guard 身份驗證的空令牌

Symfony 測試 - 帶有 Guard 身份驗證的空令牌

PHP
元芳怎么了 2022-07-09 10:07:33
我的 Symfony Web 應用程序中有一個 Guard 身份驗證。我想執行一些單元測試。我無法在我的測試中模擬身份驗證。null調用時令牌保持不變$tokenStorage->getToken()。筆記:登錄身份驗證在dev和環境下工作prod。我看到很多相關主題都沒有成功和文檔。Symfony 版本:3.4。重現:您可以從此repo(symfony 項目)重現錯誤。這個 repo 定義了一個User帶有自定義約束驗證器的實體ExampleValidator。在這個約束中,我需要擁有當前登錄的用戶。代碼示例:手動創建用戶后,login測試中使用的函數:private function logIn($firewallName = 'main'){   // dummy call to bypass the hasPreviousSession check   $crawler = $this->client->request('GET', '/');   $session = $this->client->getContainer()->get('session');   /** @var User $user */   $user = $this->entityManager->getRepository(User::class)       ->findOneBy(['email' => '[email protected]']);   // you may need to use a different token class depending on your application.   // for example, when using Guard authentication you must instantiate PostAuthenticationGuardToken   $token = new PostAuthenticationGuardToken($user, $firewallName, [new Role('ROLE_CLIENT')]);        self::$kernel->getContainer()->get('security.token_storage')->setToken($token);   $session->set('_security_'.$firewallName, serialize($token));   $session->save();   $cookie = new Cookie($session->getName(), $session->getId());   $this->client->getCookieJar()->set($cookie);}tokenStorage來自(來自服務功能)的用戶調用:class ExampleValidator extends ConstraintValidator{    protected $requestStack;    protected $em;    protected $user_id;    public function __construct(RequestStack $request,                                EntityManager $em,                                TokenStorage $tokenStorage){        $this->requestStack = $request;        $this->em = $em;        /** @var User $user */        // Token is always null        $user = $tokenStorage->getToken()->getUser();        $this->user_id = $user != "anon." ? $user->getId() : null;    }    /**     * @param $value     * @param Constraint $constraint     */    public function validate($value, Constraint $constraint)    {        // validation rules ...    }}
查看完整描述

1 回答

?
慕田峪7331174

TA貢獻1828條經驗 獲得超13個贊

我相信您的問題的根源是您正在使用多個容器實例。特別是,您的logIn()函數在客戶端的容器上運行,但驗證器來自您在setUp(). 因此,您logIn()對客戶端容器所做的更改不會影響您實際測試的驗證器。


在任何地方使用相同的容器,例如來自客戶端的容器,應該可以解決這個問題。對存儲庫的以下更改使測試通過:


diff --git a/tests/AppBundle/Validator/UserTest.php b/tests/AppBundle/Validator/UserTest.php

index f15c854..603e566 100644

--- a/tests/AppBundle/Validator/UserTest.php

+++ b/tests/AppBundle/Validator/UserTest.php

@@ -44,10 +44,7 @@ class UserTest extends WebTestCase{

         $this->container = $this->client->getContainer();

         $this->entityManager = $this->container->get('doctrine.orm.entity_manager');


-        // Set validator

-        $kernel = $this->createKernel();

-        $kernel->boot();

-        $this->validator = $kernel->getContainer()->get('validator');

+        $this->validator = $this->client->getContainer()->get('validator');


         // Create one user

         $this->createOneUser();

@@ -100,7 +97,7 @@ class UserTest extends WebTestCase{

         // you may need to use a different token class depending on your application.

         // for example, when using Guard authentication you must instantiate PostAuthenticationGuardToken

         $token = new PostAuthenticationGuardToken($user, $firewallName, [new Role('ROLE_CLIENT')]);

-        self::$kernel->getContainer()->get('security.token_storage')->setToken($token);

+        $this->client->getContainer()->get('security.token_storage')->setToken($token);


         $session->set('_security_'.$firewallName, serialize($token));

         $session->save();


查看完整回答
反對 回復 2022-07-09
  • 1 回答
  • 0 關注
  • 171 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號