PHPUnit ::如何設置和獲取cookie的函數,測試沒有得到錯誤:頭已經發送?PHPUnit ::如何設置和獲取cookie的功能,測試?
例,讓錯誤:
PHPUnit_Framework_Error_Warning:不能更改頭信息 - 已經
MyCookie.php
class MyCookie{
public static function createCookie(){
$uid = null;
$cookieName='test_cookie';
if(!isset($_COOKIE[$cookieName])){
$uid = unique_hash();
setcookie($cookieName, $uid, 0, '', '', false, true);
}
else{
$uid=$_COOKIE[$cookieName];
}
return $uid;
}
}
MyCookieTest.php
發送了頭class MyCookieTest extends PHPUnit_Framework_TestCase{
public function test_createCookie(){
MyCookie::createCookie();
assertThat(isset($_COOKIE['test_cookie']), is(true));
unset($_COOKIE['test_cookie']);
MyCookie::createCookie();
assertThat(isset($_COOKIE['test_cookie']), is(true));
}
}
感謝
順便說一句,你有自己的實現assertThat的或者您使用的PHPUnit的3.5發佈候選版本? http://sebastian-bergmann.de/archives/896-PHPUnit-3.5-Less-this-Required.html#content – 2010-10-15 14:18:57
assertThat是Hamcrest.php庫與phpunit – Yosef 2010-10-29 12:52:05