【问题标题】:Setting HTTP headers to be able to run test cases设置 HTTP 标头以便能够运行测试用例
【发布时间】: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


【解决方案1】:

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

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

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

如果没有,如果您为这个问题分配了赏金,我会更深入地研究它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-05
    • 1970-01-01
    • 2013-09-11
    • 2013-05-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多