English 中文(简体)
如何利用PHPUnit测试厨师和会议?
原标题:Using PHPUnit to test cookies and sessions, how?

有了PHPUnit,它很容易测试原始的PHP代码,但严重依赖 co的代码是什么? 届会可以成为很好的例子。

是否有一种方法要求我安装_$_COOKIE,在我测试期间有数据? 它感觉到做事情的 way。

最佳回答

这在法典,特别是落后的购买力平价法方面是一个共同的问题。 采用的共同技术是进一步推敲相关物体中的COOKIE/SESSION变量,并利用控制技术的转移将这些依赖因素推向范围。

rel=“noretinger”>http://martinfowler.com/articles/injection.html

现在,在你进行测试之前,你将立即对 Cook/Session物体进行模拟,并提供违约数据。

我认为,通过遗产法典,在进行试验之前简单地超越了超级全球价值,就可以产生同样的效果。

Cheers, Alex

问题回答

我的理解是,这是很老的,但我认为,随着技术自最初员额以来有所改善,需要更新。 我得以利用第php5.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());
    }
}

我发现,我可以利用PHPUnit,通过Curl(>>>通过session id对我网站主要依靠会议的部分的行为进行测试。

缩略语 班级使用<代码>CURLOPT_COOKIE选择通过会议参数。 静态变量<代码>sessionid节省了不同Curl电话之间的会议费用。 此外,还可使用静态功能<代码>change改变。

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(<>>/code>。





相关问题
why the session in iis automatically log out?

I used iis6, and when i called a function Directory.delete(), the all the session relate this website will be logged out. and i debugged the website, i found no exception. any one have ideas on this ? ...

Check session from a view in CodeIgniter

What is the best way to check session from a view in CodeIgniter, it shows no way in their user guide, otherwise I will have to make two views on everything, which is kinda weird...still a newbie to ...

Can I get the size of a Session object in bytes in c#?

Is it possible to get the size(in bytes) of a Session object after storing something such as a datatable inside it? I want to get the size of a particular Session object, such as Session["table1"], ...

提供严格分类的出席会议物体

提供严格分类的与会机会的最佳方式是什么? 我正计划转而选择矩阵,这正在促使汇编者抱怨我的幻觉方案拟订方法......

PHP Session is not destroying after user logout

I m trying to create an authentication mechanism for my PHP Application and I m having difficulty destroying the session. I ve tried unsetting the authentication token which was previously set within ...