设置HTTP标头以能够运行测试用例 [英] Setting HTTP headers to be able to run test cases

查看:86
本文介绍了设置HTTP标头以能够运行测试用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

更新:我尝试了以下问题:

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)

推荐答案

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

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.

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

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

然后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.

这篇关于设置HTTP标头以能够运行测试用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆