在函数中看到“ isGranted”时,令牌有问题

时间:2019-11-05 12:04:30

标签: php phpunit symfony4 functional-testing

在Symfony 4.2中创建功能测试时,令牌存在问题。

当我使用搜寻器对象与表单进行交互时,就会出现问题。首先,我创建令牌,该令牌将允许我进入应用程序。令牌在进入需要权限的操作时便会发挥作用,但是在代码执行过程中,如果它再次具有特定的进入权限或是否能够进入,则会遇到if评估为“ isGranted”的情况不经过。您必须对其进行评估时,令牌会丢失。为什么会这样呢?安全文件配置是否错误? ...

public function addCriteriosPropios ()
{
  try {
    if ($this->security->isGranted('ROLE_ACCESS')) {
      return;
    } else {
         ...   
    }
} catch (\Throwable $e) {
    echo $e;
    }
} 

Test Code
public function testBotonBuscar() {
  $this->logIn();

  $crawler = $this->client->request('POST', '/Comm/buscador');

  $form = $crawler->selectButton('Buscar')->form();
  $form['exp']->setValue('');
  $this->client->submit($form);
}


protected function loginIn() {
  $firewall = 'main';

  $token = new UsernamePasswordToken($this->usuarioTest, null, $firewall, (array) $roles);


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

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

  $session->set('_security_' . $firewall, serialize($token));
  $session->set('cuenta.id', $this->cuentaTest->getId());
  $session->set('cliente.id', $this->cuentaTest->getCliente()->getId());

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

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

  $session->save();
}

有1个失败:

1)App\Tests\Controller\Comm\BuscadorControllerTest::testBotonBuscar

无法断言类型为“异常”的异常与预期的异常“ Symfony\Component\Security\Core\Exception\AccessDeniedException”匹配。 消息是:“

  

令牌存储区不包含身份验证令牌。一种可能   原因可能是没有为此URL配置防火墙。”   /var/www/html/wetender/vendor/symfony/security-core/Authorization/AuthorizationChecker.php:49

/var/www/html/wetender/vendor/symfony/security-core/Security.php:70

/var/www/html/wetender/src/Utils/LicitacionesUtils.php:125

/var/www/html/wetender/src/Utils/CommUtils.php:269

/var/www/html/wetender/src/Controller/Comm/BuscadorController.php:99

/var/www/html/wetender/vendor/symfony/http-kernel/HttpKernel.php:150

/var/www/html/wetender/vendor/symfony/http-kernel/HttpKernel.php:67

/var/www/html/wetender/vendor/symfony/http-kernel/Kernel.php:198

/var/www/html/wetender/vendor/symfony/http-kernel/Client.php:68

/var/www/html/wetender/vendor/symfony/framework-bundle/Client.php:131

/var/www/html/wetender/vendor/symfony/browser-kit/Client.php:405

/var/www/html/wetender/vendor/symfony/browser-kit/Client.php:325

/var/www/html/wetender/tests/Controller/Comm/BuscadorControllerTest.php:116

0 个答案:

没有答案