问题
I am using phpunit. I want to test my code which basically gets parameters from HTTP headers and use it to perform subsequent operations.
But while testing the headers are null.
Is there any way to set headers (may be in bootstrap file) so that when my code accesses parameter it gets that value?
UPDATE : I tried below code as suggested in this 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();
}
}
But gives error :
Action_UserTest::testExecutePut()
Cannot modify header information - headers already sent by (output started at /usr/lib/php/PHPUnit/Util/Printer.php:172)
回答1:
You cannot catch headers using PHPUnit 3 if you're using testdox or something like that. I forget which one, but one of the formatters was bugged when I last looked into it.
Also, you need to create a bootstrap.php and in it, all it really needs is an ob_start();
.
Then phpunit -b bootstrap.php UnitTest.php
. See if that works.
If not, if you assign a bounty ot this question, i'll look into it in much more depth.
来源:https://stackoverflow.com/questions/17762183/setting-http-headers-to-be-able-to-run-test-cases