【问题标题】:Symfony Panther authentication seems not to be taken into accountSymfony Panther 身份验证似乎没有被考虑在内
【发布时间】:2020-07-29 16:10:42
【问题描述】:

我正在使用 Symfony Panther 进行一些测试。

当我想记录这样的用户时:

protected function setUpPanther()
{
    $client = static::createPantherClient(['readinessPath' => '/error']);
    $client->manage()->window()->maximize();
    $this->client = $client;
}

protected function loginPantherClient(Client $client, User $user)
{
    $client->request('GET', '/error');

    $session = self::$container->get('session');

    $token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
    $session->set('_security_main', serialize($token));
    $session->save();

    $cookie = new Cookie($session->getName(), $session->getId());

    $client->getCookieJar()->set($cookie);
}

public function testUpdateProductDetails()
{
    $this->setUpPanther();
    
    $admin = $this->getSuperAdminAccount();

    $this->loginPantherClient($this->client, $admin);
    $this->client->request('GET', '/');
    $this->assertSelectorExists('div.form-group');
}

一个奇怪的现象发生了。

  1. 我的客户端进入错误页面初始化cookie(必填)

  2. 这里我的印象是我的客户端的身份验证没有考虑到,因为如果你仔细看,在我的测试中,我然后提出一个请求

    $this->client->request('GET', '/');

但不是将我重定向到请求的页面,而是将我重定向到 / login,好像在请求期间,用户未经过身份验证,因此被重定向到登录页面,而在我的代码中,我验证了之前的用户

有人遇到过这个问题吗?

【问题讨论】:

    标签: php symfony authentication testing phpunit


    【解决方案1】:

    尝试从 loginPantherClient 函数返回客户端:

    protected function loginPantherClient(Client $client, User $user)
    {
        $client->request('GET', '/error');
    
        $session = self::$container->get('session');
    
        $token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
        $session->set('_security_main', serialize($token));
        $session->save();
    
        $cookie = new Cookie($session->getName(), $session->getId());
    
        $client->getCookieJar()->set($cookie);
        return $client;
    }
    
    public function testUpdateProductDetails()
    {
        $this->setUpPanther();
        
        $admin = $this->getSuperAdminAccount();
    
        $this->client = $this->loginPantherClient($this->client, $admin);
        $this->client->request('GET', '/');
        $this->assertSelectorExists('div.form-group');
    }
    

    或从 loginPantherClient 函数中删除 $client 参数:

    protected function loginPantherClient(User $user)
    {
        $this->client->request('GET', '/error');
    
        $session = self::$container->get('session');
    
        $token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
        $session->set('_security_main', serialize($token));
        $session->save();
    
        $cookie = new Cookie($session->getName(), $session->getId());
    
        $this->client->getCookieJar()->set($cookie);
        
    }
    
    public function testUpdateProductDetails()
    {
        $this->setUpPanther();
    
        $admin = $this->getSuperAdminAccount();
    
        $this->loginPantherClient($admin);
        $this->client->request('GET', '/');
        $this->assertSelectorExists('div.form-group');
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-11
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多