测试期间 Phpunit token 存储错误

标签 php symfony phpunit

我有一个测试服务实例化的测试文件,我用 KnpMenuBundle 制作了一个自定义菜单。 一切正常,期待 phpunit 在测试我的 MenuBuilder 时返回错误。

有测试所有服务实例化我的测试文件的功能:

class ServiceAvailabilityTest extends KernelTestCase
{
    /**
    * @dataProvider getServiceIds
    *
    * @param $serviceId
    */
    public function testServiceInstance($serviceId)
    {
        static::bootKernel();

        static::$kernel->getContainer()->get($serviceId);
    }
}

在我的 MenuBuilder 上,我使用 authorizationChecker 来了解用户是否被授权,就像这样。

if ($this->authorizationChecker->isGranted('ROLE_ADMIN')) {
    $menu->addChild('sidebar.front.administration', ['route' => 'sonata_admin_redirect'])
         ->setExtra('translation_domain', 'navigation')
         ->setAttribute('icon', 'fa fa-eye');
}

当我删除所有这些时,测试没问题

$this->authorizationChecker->isGranted('ROLE_ADMIN')

运行 phpunit 时出现错误

1) Tests\ServiceAvailabilityTest::testServiceInstance with data set #423 ('menu.main') Symfony\Component\Security\Core\ExceptionAuthenticationCredentialsNotFoundException: The token storage contains no authentication token. One possible reason may be that there is no firewall configured for this URL. /code/vendor/symfony/symfony/src/Symfony/Component/Security/Core/Authorization/AuthorizationChecker.php:57 /code/src/AppBundle/Menu/MenuBuilder.php:192 /code/src/AppBundle/Menu/MenuBuilder.php:101 /code/app/cache/test/appTestDebugProjectContainer.php:8311 /code/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Container.php:312 /code/tests/ServiceAvailabilityTest.php:3

有我的菜单服务,如果你必须检查它们

menu.builder:
    class: AppBundle\Menu\MenuBuilder
    arguments: [ '@knp_menu.factory', '@doctrine', '@manager.server','@security.authorization_checker', '@request_stack' ]

menu.main:
    class: Knp\Menu\MenuItem
    factory: [ '@menu.builder', 'createMainMenu' ]
    arguments: [ '@request_stack' ]
    tags:
        - { name: knp_menu.menu, alias: sidebar }

我已经在互联网上搜索过,但他们通过像这样在 security.yml 上添加访问控制来解决这个问题

- { path: ^/, role: ROLE_USER }

但我没有任何菜单路径。

有人遇到过这个 phpunit 错误吗? 谢谢,

最佳答案

试试这个:

/**
 * @param string        $firewallName
 * @param UserInterface $user
 * @param array         $options
 * @param array         $server
 */
protected function loginUser($firewallName, UserInterface $user, array $options = array(), array $server = array())
{
    $this->client = static::createClient();

    $token = new UsernamePasswordToken($user, null, $firewallName, $user->getRoles());
    static::$kernel->getContainer()->get('security.token_storage')->setToken($token);
    // <2.8 this may be usefull
    //$request = new Request();
    //$event = new InteractiveLoginEvent($request, $token);
    //static::$kernel->getContainer()->get('event_dispatcher')->dispatch('security.interactive_login', $event);

    $session = $this->client->getContainer()->get('session');
    $session->set('_security_'.$firewallName, serialize($token));
    $session->save();
    $cookie = new Cookie($session->getName(), $session->getId());
    $this->client->getCookieJar()->set($cookie);
}

例如在您的测试用例/设置中::

static::bootKernel();
$this->loginUser('admin', $testUser);
$this->assertNotFalse(static::$kernel->getContainer()->get('security.authorization_checker')->isGranted('ROLE_ADMIN'));

关于测试期间 Phpunit token 存储错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34679489/

相关文章:

php - MySQL计算列值的实例并按列值排序

php - Symfony2 : Uploads a file using Ajax and jQuery

PHPUnit 测试异常和 AssertEquals

PHP: session 变量

php - Doctrine2 中的嵌套位置

mysql - 在 symfony 中使用 Doctrine 时我得到了错误的数据

php - 异常Eventlistener无法捕获RabbitMQ使用者的异常

php - Laravel 事件模拟

php - 在 PHPUnit 中重新运行上次失败的测试

php - 正则表达式条件