【发布时间】:2013-07-20 12:17:45
【问题描述】:
我正在使用 phpunit。我想测试我的代码,它基本上从 HTTP 标头获取参数并使用它来执行后续操作。
但是在测试时标头为空。
有没有办法设置标头(可能在引导文件中),以便在我的代码访问参数时获得该值?
更新:我按照question 中的建议尝试了以下代码:
class Action_UserTest extends PHPUnit_Framework_TestCase {
/**
* @runInSeparateProcess
*/
public function testBar()
{
header('Location : foo');
}
/**
* @covers Action_User::executePut
* @todo Implement testExecutePut().
*/
public function testExecutePut() {
ob_start();
$this->testBar();
$headers_list = headers_list();
$this->assertNotEmpty($headers_list);
$this->assertContains('Location: foo', $headers_list);
header_remove();
ob_clean();
}
}
但给出错误:
Action_UserTest::testExecutePut()
Cannot modify header information - headers already sent by (output started at /usr/lib/php/PHPUnit/Util/Printer.php:172)
【问题讨论】:
-
我需要知道您使用什么版本的 PHP 来运行这些测试。
标签: php unit-testing phpunit output-buffering