doumindang2416 2018-07-11 10:22
浏览 51

PHPUnit + Guzzle:测试方法之间的Cookie持久性

I am using PHPUnit and Guzzle to make API unit test. The probleme I have is that I am not able or I can't really figure out how to persiste cookie between my test methods. Or maybe am doing it all wrong ^^

The first test testGetFirst gets the data and set a session cookie on server side. The response has a Set-Cookie header with the right cookie.

The seconde test testGetSecond should return a data set if the cookie exists. Unfortunatelly it seems that Guzzle::Client doesn't store/persiste the cookie between the methods.

    use PHPUnit\Framework\TestCase;
    use GuzzleHttp\Client;

    class MyTest extends Testcase
    {
        public $guzzleClient;

        /**
         * @before
         */
        function initVariables() {
            $this->guzzleClient = new Client([
                'base_uri' => 'http://apiuri.com',
                'cookies'  => true
            ]);
        }


        // This call get some data and set a cookie (session cookie) on server side          
        function testGetFirst() {
            $params = [
                'query' => [
                    'param1' => 'myparam'
                ]                
            ];

            $response = $this->guzzleClient->request('GET', '/', $params);
            // if I print out the response headers I get my cookie in 'Set-Cookie' header
            // I suppose the cookie has been set correctly 
            print_r($response->getHeaders());

            // if I print out the client conf cookie, I get the cookie too
            print_r($this->guzzleClient->getConfig('cookies')->toArray());
        }

        // This call get data to have access it need to use a cookie that has been set by testGetFirst
        // But unfortunatelly the cookie is empty while making the request
        /**
         * @depends testGetFirst
         */
        function testGetSecond() {
            $params = [
                'query' => [
                    'param1' => 'hello'
                ]
            ];

            // if I print out the client conf cookie, cookies is empty
            print_r($this->guzzleClient->getConfig('cookies')->toArray());

            $response = $this->guzzleClient->request('GET', '/second', $params);             
            // as  the request can't access to a cookie it sends an error response
        }


    }

I know there is CookieJar that I can use in each method and pass the Set-Cookie value in to Jar but I was hopping to avoid it.

Do you have any idea or suggestion ?

Very appreciate your help.

  • 写回答

1条回答 默认 最新

  • dougua9328 2019-07-09 20:17
    关注

    Due to the @before annotation for the initVariables method, this method is executed before each test. That method is creating a fresh client before each test.

    To solve your particular situation, since you're using the @depends testGetFirst annotation, you could have the testGetFirst method return the client object. The testGetSecond method can then receive it as an argument. Check the Test Dependencies documentation for more info regarding passing arguments into dependent tests.

    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算