第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Symfony 測(cè)試 - 帶有 Guard 身份驗(yàn)證的空令牌

Symfony 測(cè)試 - 帶有 Guard 身份驗(yàn)證的空令牌

PHP
元芳怎么了 2022-07-09 10:07:33
我的 Symfony Web 應(yīng)用程序中有一個(gè) Guard 身份驗(yàn)證。我想執(zhí)行一些單元測(cè)試。我無(wú)法在我的測(cè)試中模擬身份驗(yàn)證。null調(diào)用時(shí)令牌保持不變$tokenStorage->getToken()。筆記:登錄身份驗(yàn)證在dev和環(huán)境下工作prod。我看到很多相關(guān)主題都沒(méi)有成功和文檔。Symfony 版本:3.4。重現(xiàn):您可以從此repo(symfony 項(xiàng)目)重現(xiàn)錯(cuò)誤。這個(gè) repo 定義了一個(gè)User帶有自定義約束驗(yàn)證器的實(shí)體ExampleValidator。在這個(gè)約束中,我需要擁有當(dāng)前登錄的用戶。代碼示例:手動(dòng)創(chuàng)建用戶后,login測(cè)試中使用的函數(shù):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' => 'user1@example.com']);   // 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來(lái)自(來(lái)自服務(wù)功能)的用戶調(diào)用: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貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超13個(gè)贊

我相信您的問(wèn)題的根源是您正在使用多個(gè)容器實(shí)例。特別是,您的logIn()函數(shù)在客戶端的容器上運(yùn)行,但驗(yàn)證器來(lái)自您在setUp(). 因此,您logIn()對(duì)客戶端容器所做的更改不會(huì)影響您實(shí)際測(cè)試的驗(yàn)證器。


在任何地方使用相同的容器,例如來(lái)自客戶端的容器,應(yīng)該可以解決這個(gè)問(wèn)題。對(duì)存儲(chǔ)庫(kù)的以下更改使測(cè)試通過(guò):


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


查看完整回答
反對(duì) 回復(fù) 2022-07-09
  • 1 回答
  • 0 關(guān)注
  • 156 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)