使用返回标头但不发送标头的Mock或编写 Response 对象,例如
class HttpResponse
{
protected $_status = '200 OK';
protected $_headers = array();
protected $_body = null;
public function setStatus($status)
{
$this->_status = $status;
}
public function getStatus()
{
return $this->_status;
}
public function addHeader($name, $value)
{
$this->_headers[$name] = $value;
return $this;
}
public function getHeaders()
{
return $this->_headers;
}
public function write($data)
{
$this->_body .= $data;
return $this;
}
public function send()
{
header('HTTP/1.0 ' . $this->_status);
foreach ($this->headers as $name => $value) {
header("$name : $value");
}
echo $this->_body;
$this->resetRequest();
}
public function resetRequest()
{
$this->_status = '200 OK';
$this->_headers = array();
$this->_body = null;
}
}
因此,只要您不这样做,send()
您就可以通过吸气剂检查状态。您还可以添加一个__toString()
方法,将整个响应作为字符串返回并对其进行正则表达式,以查看它是否看起来像您期望的那样。