加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

当我从phpunit登录时,当前用户的对象在Symfony控制器中为NULL

发布时间:2020-12-13 22:26:59 所属栏目:PHP教程 来源:网络整理
导读:我有 PHPUnit的登录功能,并将其用作测试用户的授权. private function logIn(Client $client){ $session = $client-getContainer()-get('session'); /** @var User $user */ $user = $client-getContainer()-get('doctrine')-getRepository('DWDAdminBundle:
我有 PHPUnit的登录功能,并将其用作测试用户的授权.

private function logIn(Client $client)
{
    $session = $client->getContainer()->get('session');

    /** @var User $user */
    $user = $client->getContainer()->get('doctrine')->getRepository('DWDAdminBundle:User')->find(1);
    //Here I have user object

    $firewall = 'main';
    $token = new UsernamePasswordToken($user,null,$firewall,['ROLE_ADMIN']);
    $client->getContainer()->get('security.token_storage')->setToken($token);
    $session->set('_security_'.$firewall,serialize($token));
    $session->save();

    $cookie = new Cookie($session->getName(),$session->getId());
    $client->getCookieJar()->set($cookie);
}

在测试过的动作中有这个代码:

$checkResult = $this->get('dwd.service.coupon')->checkCoupon(
            $coupon,$request->query->all(),$this->getUser()->getPortalId()
            // Here $this->getUser returned NULL
        );

虽然我在testU的setUp()中授权了我的测试用户,但函数getUser()以某种方式返回了Null

public function setUp()
{
    parent::setUp();

    $this->client = static::createClient();
    $this->logIn($this->client);

}

解决方法

被测试的动作是否在同一个防火墙下?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读