设置HTTP标头以便能够运行测试用例

时间:2021-01-02 01:20:49

I am using phpunit. I want to test my code which basically gets parameters from HTTP headers and use it to perform subsequent operations.

我正在使用phpunit。我想测试我的代码,它基本上从HTTP头获取参数并使用它来执行后续操作。

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 个解决方案

#1


0  

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.

如果您正在使用testdox或类似的东西,则无法使用PHPUnit 3捕获标头。我忘记了哪一个,但是当我上次查看时,其中一个格式化程序被窃听了。

Also, you need to create a bootstrap.php and in it, all it really needs is an ob_start();.

此外,您需要创建一个bootstrap.php,其中所有它真正需要的是一个ob_start();.

Then phpunit -b bootstrap.php UnitTest.php. See if that works.

然后phpunit -b bootstrap.php UnitTest.php。看看是否有效。

If not, if you assign a bounty ot this question, i'll look into it in much more depth.

如果没有,如果你给这个问题一个赏金,我会更深入地研究它。

#1


0  

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.

如果您正在使用testdox或类似的东西,则无法使用PHPUnit 3捕获标头。我忘记了哪一个,但是当我上次查看时,其中一个格式化程序被窃听了。

Also, you need to create a bootstrap.php and in it, all it really needs is an ob_start();.

此外,您需要创建一个bootstrap.php,其中所有它真正需要的是一个ob_start();.

Then phpunit -b bootstrap.php UnitTest.php. See if that works.

然后phpunit -b bootstrap.php UnitTest.php。看看是否有效。

If not, if you assign a bounty ot this question, i'll look into it in much more depth.

如果没有,如果你给这个问题一个赏金,我会更深入地研究它。