【发布时间】:2014-05-03 05:57:33
【问题描述】:
我正在尝试为我的所有 HTTP 请求设置单元测试。每个请求都需要身份验证,而对于我的应用,它需要通过 cookie 和 DB 查询进行身份验证。
我在父控制器中有一个 preDispatch 方法,如下所示:
$this->cookie = Cookie::readCookie();
if (is_null($this->cookie))
{
return $this->failResponseView();
}
$this->dm = $this->getServiceLocator()->get('doctrine.documentmanager.odm_default');
//Does not have authoriziation
if (!$this->hasAppAccess())
{
return $this->failResponseView();
}
就应用而言,这一直运行良好。但是每次运行phpunit 都会失败,因为无法读取cookie,或者在读取响应之前正在写入响应。
这是我在测试设置方法中反映我在常规应用中所做的事情:
$this->_cookie = new Cookie(array('access_token' => $profile['token']));
$this->_cookie->setCookie();
但是,当代码到达这一点时,我收到了这个Exception。我的问题是,我如何在运行phpunit 时伪造或绕过我的 cookie 身份验证,以确保所有这些经过身份验证的请求都能正常工作?
Cannot modify header information - headers already sent by (output started at D:
\www\app\vendor\phpunit\phpunit\PHPUnit\Util\Printer.php:172)
更新
看起来因为 PHPUnit\Util\Printer 正在输出到 STDOUT(见上文),所以我不喜欢我正在尝试编写 cookie。运行它允许完全执行
phpunit --stderr
所以我可以调用 setCookie() 方法,它执行得很好。但是当我到达Cookie::readCookie() 的地步时,即使它已经被设置,它也无法读取它。它返回 null。
所以问题还是差不多的。如果此应用使用 cookie 身份验证,我该如何测试它?
【问题讨论】:
-
什么是
Cookie?这看起来不像 ZF 类。 -
@TimFountain 它只是我创建的一个自定义类,用于存储身份验证所需的一些信息。我在哪里运行
setcookie()。我还用更多信息更新了我的问题。