symfony2测试用例请求cookie [英] symfony2 test case request cookie

查看:109
本文介绍了symfony2测试用例请求cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的测试中,我想指定一个与请求一起使用的cookie.我追溯了代码,以了解如何在客户端的__construct中使用cookie jar.尽管此处的var_dump和服务器端的var_dump显示没有随请求一起发送cookie.我还尝试使用HTTP_COOKIE发送一个更简单的字符串,如图所示.

In my test, I'd like to specify a cookie to go along with the request. I traced the code back to see how a cookie jar is used in the Client's __construct. Though a var_dump here and a var_dump on the server side show no cookie being sent with the request. I also tried sending a simpler string with HTTP_COOKIE as shown.

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\BrowserKit\Cookie;
use Symfony\Component\BrowserKit\CookieJar;
class DefaultControllerTest extends WebTestCase {
    public function test() {
        $jar = new CookieJar();
        $cookie = new Cookie('locale2', 'fr', time() + 3600 * 24 * 7, '/', null, false, false);
        $jar->set($cookie);
        $client = static::createClient(array(), array(), $jar);  //this doesn't seem to attach cookies as expected!
        $crawler = $client->request(
            'GET', //method
            '/', //uri
            array(), //parameters
            array(), //files
            array(
                'HTTP_ACCEPT_LANGUAGE' => 'en_US',
                //'HTTP_COOKIE' => 'locale2=fr' //this doesn't work either!
            ) //server
        );

        var_dump($client->getRequest());
    }
}

推荐答案

您的代码有误:

$client = static::createClient(array(), array(), $jar); // Third parameter ?

方法createClient定义如下(对于Symfony 2.0.0):

Method createClient is defined as following (for Symfony 2.0.0):

static protected function createClient(array $options = array(), array $server = array())

因此,它仅使用两个参数,并且没有放置cookie的位置,因为createClient方法从测试容器中获取一个客户端实例:

So, it takes only two parameters and there is no place for cookie, because createClient method takes an instance of client from the test container:

$client = static::$kernel->getContainer()->get('test.client');
$client->setServerParameters($server);

return $client;

这是test.client服务的定义:

<service id="test.client" class="%test.client.class%" scope="prototype">
    <argument type="service" id="kernel" />
    <argument>%test.client.parameters%</argument>
    <argument type="service" id="test.client.history" />
    <argument type="service" id="test.client.cookiejar" />
</service>

<service id="test.client.cookiejar" class="%test.client.cookiejar.class%" scope="prototype" />

现在我们看到,cookie jar服务被注入到test.client中,并具有一个范围prototype,这意味着在对该服务的每次访问中都会创建一个新对象.

Now we see, that cookie jar service is injected into test.client and have a scope prototype which means that new object will be created on each access to that service.

但是Client类具有方法getCookieJar(),您可以使用它为请求设置特定的cookie(未经测试,但预期可以使用):

However Client class has a method getCookieJar() and you can use it to set specific cookies for request (not tested, but expected to work):

$client = static::createClient();
$cookie = new Cookie('locale2', 'fr', time() + 3600 * 24 * 7, '/', null, false, false);
$client->getCookieJar()->set($cookie);

这篇关于symfony2测试用例请求cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆