Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
测试期间Phpunit令牌存储错误_Php_Symfony_Phpunit - Fatal编程技术网

测试期间Phpunit令牌存储错误

测试期间Phpunit令牌存储错误,php,symfony,phpunit,Php,Symfony,Phpunit,我有一个测试文件来测试服务实例化,我用KnpMenuBundle创建了一个自定义菜单。 除了phpunit在测试我的MenuBuilder时返回错误外,一切正常 有一个函数用于测试所有服务实例化我的测试文件: class ServiceAvailabilityTest extends KernelTestCase { /** * @dataProvider getServiceIds * * @param $serviceId */ public

我有一个测试文件来测试服务实例化,我用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::具有数据集#423('menu.main')的testServiceInstance Symfony\Component\Security\Core\exception身份验证CredentialsNotFoundException:令牌存储不包含身份验证令牌。一个可能的原因可能是没有为此URL配置防火墙/code/vendor/symfony/symfony/src/symfony/Component/Security/Core/Authorization/AuthorizationChecker.php:57 /代码/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);
}

你试过这个吗?好的,谢谢,我将尝试此操作,并给出错误
Symfony\Component\Debug\Exception\ContextErrorException:Catchable致命错误:传递给Tests\serviceavaailabilitytetest::logiuser()的参数2必须实现接口Symfony\Component\Security\Core\User\UserInterface,给定null,在第46行的/code/tests/ServiceAvailabilityTest.php中调用并定义了
,这些消息基本上说明了您做错了什么。您需要一个用户对象,该用户对象必须实现UserInterface。(例如,如果您使用条令用户提供程序,应该是这样)是的,我添加了一个删除此错误的新用户。谢谢
static::bootKernel();
$this->loginUser('admin', $testUser);
$this->assertNotFalse(static::$kernel->getContainer()->get('security.authorization_checker')->isGranted('ROLE_ADMIN'));