Symfony2从phpunit测试中的分支模板访问全局变量

时间:2022-10-24 14:20:38

I have a twig template with {{ app.user }}. The problem is, that in a phpunit test (a class, which extends WebTestCase) it is defined as NULL. Simulating Authentication with a Token (http://symfony.com/doc/current/cookbook/testing/simulating_authentication.html) or simulating HTTP Authentification (http://symfony.com/doc/current/cookbook/testing/http_authentication.html) does not help. So how can I set a twig global variable from a phpunit test? And why simulating authentification is not working in this case?

我有一个带有{{app.user}的twig模板。问题是,在phpunit测试(一个扩展WebTestCase的类)中,它被定义为NULL。使用令牌(http://symfony.com/doc/current/cookbook/testing/simulating_authenticating.html)模拟身份验证或模拟HTTP验证(http://symfony.com/doc/current/cookbook/testing/http_authentic.html)没有帮助。那么如何从phpunit测试中设置一个twig全局变量呢?为什么在这种情况下模拟认证不起作用呢?

1 个解决方案

#1


5  

I had a similar problem (logging in properly) what solved it finally for me was to change the way I log in(it's based on http://symfony.com/doc/current/cookbook/testing/simulating_authentication.html but I handle the session differently):

我有一个类似的问题(正确地登录),最终解决了我的问题是改变了登录的方式(它基于http://symfony.com/doc/current/cookbook/testing/simulating_authentic. html,但我以不同的方式处理会话):

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

    $this->em = $this->get('doctrine')->getManager();
    $this->client = static::createClient(array('environment' => 'test'));
}

protected function logIn() {
    $repo = $this->em->getRepository('XXXXXXX');
    $user = $repo->findOneByUsername('YYYYYYY');

    $session = new Session(new MockFileSessionStorage());
    $firewall = 'main';
    $token = new UsernamePasswordToken($user, null, $firewall, array('ROLE_ADMIN'));
    $this->client->getContainer()->get('security.context')->setToken($token);
    $session->set('_security_' . $firewall, serialize($token));
    $session->save();

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

    return $user;
}

#1


5  

I had a similar problem (logging in properly) what solved it finally for me was to change the way I log in(it's based on http://symfony.com/doc/current/cookbook/testing/simulating_authentication.html but I handle the session differently):

我有一个类似的问题(正确地登录),最终解决了我的问题是改变了登录的方式(它基于http://symfony.com/doc/current/cookbook/testing/simulating_authentic. html,但我以不同的方式处理会话):

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

    $this->em = $this->get('doctrine')->getManager();
    $this->client = static::createClient(array('environment' => 'test'));
}

protected function logIn() {
    $repo = $this->em->getRepository('XXXXXXX');
    $user = $repo->findOneByUsername('YYYYYYY');

    $session = new Session(new MockFileSessionStorage());
    $firewall = 'main';
    $token = new UsernamePasswordToken($user, null, $firewall, array('ROLE_ADMIN'));
    $this->client->getContainer()->get('security.context')->setToken($token);
    $session->set('_security_' . $firewall, serialize($token));
    $session->save();

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

    return $user;
}