代码之家  ›  专栏  ›  技术社区  ›  The Pixel Developer

如何使用phpunit测试cookie和会话?

  •  11
  • The Pixel Developer  · 技术社区  · 14 年前

    使用phpunit可以很容易地测试原始的php代码,但是那些严重依赖cookie的代码呢?会话可能是一个很好的例子。

    有没有不需要我设置的方法 $_COOKIE 在我的测试中有数据吗?这感觉像是一种下流的做事方式。

    3 回复  |  直到 8 年前
        1
  •  6
  •   user367861    14 年前

    这是代码的一个常见问题,尤其是Lagacy PHP代码。常用的技术是进一步抽象相关对象中的cookie/会话变量,并使用控制反转技术将这些依赖项拉入范围。

    http://martinfowler.com/articles/injection.html

    现在,在执行测试之前,您将实例化一个cookie/会话对象的模拟版本并提供默认数据。

    我想,通过在执行测试之前简单地覆盖超级全局值,可以用遗留代码实现相同的效果。

    干杯, 亚历克斯

        2
  •  2
  •   Etrahkad    10 年前

    我知道这是相当古老的,但我相信这需要更新,因为技术已经改进,从原来的帖子。我可以使用php 5.4和phpunit 3.7来获得使用此解决方案的会话:

    class UserTest extends \PHPUnit_Framework_TestCase {
        //....
        public function __construct () {
            ob_start();
        }
    
        protected function setUp() {
           $this->object = new \User();
        }
    
        public function testUserLogin() {
           $this->object->setUsername('test');
           $this->object->setPassword('testpw');
           // sets the session within:
           $this->assertEquals(true, $this->object->login());
        }
    }
    
        3
  •  0
  •   Roel Vermeulen    8 年前

    我发现我可以使用phpunit来测试我的网站中严重依赖会话的部分的行为,通过结合 卷曲 和A 曲奇饼干 通过了 会话ID .

    以下 Curl 类使用 CURLOPT_COOKIE 用于传递会话参数的选项。静态变量 $sessionid 保存不同curl调用之间的会话。此外,可以使用静态函数更改会话 changeSession .

    class Curl {
        private $ch;
        private static $sessionid;
    
        public function __construct($url, $options) {
            $this->ch = curl_init($url);
    
            if (!self::$sessionid)
                self::$sessionid = .. generateRandomString() ..;
    
            $options = $options + array(
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_COOKIE => 'PHPSESSID=' . self::$sessionid);
    
            foreach ($options as $key => $val) {
                curl_setopt($this->ch, $key, $val);
            }
        }
    
        public function getResponse() {
            if ($this->response) {
                return $this->response;
            }
    
            $response = curl_exec($this->ch);
            $error    = curl_error($this->ch);
            $errno    = curl_errno($this->ch);
            $header_size = curl_getinfo($this->ch, CURLINFO_HEADER_SIZE);
            $this->header = substr($response, 0, $header_size);
            $response = substr($response, $header_size);
    
            if (is_resource($this->ch)) {
                curl_close($this->ch);
            }
    
            if (0 !== $errno) {
                throw new \RuntimeException($error, $errno);
            }
    
            return $this->response = $response;
        }
    
        public function __toString() {
            return $this->getResponse();
        }
    
        public static function changeSession() {
            self::$SESSIONID = Practicalia::generateRandomString();
        }
    }
    

    示例调用

    $data = array(
        'action' => 'someaction',
        'info' => 'someinfo'
    );
    
    $curl = new Curl(
        'http://localhost/somephp.php', 
        array(
            CURLOPT_POSTFIELDS => http_build_query($data)));
    
    $response = $curl->getResponse();
    

    任何后续的curl调用都将自动使用与前一个会话相同的会话,除非 Curl::changeSession() 被称为。