Phpunit: 使用 PHAR 运行时,隔离测试中的“标头已发送”问题

创建于 2014-03-18  ·  26评论  ·  资料来源: sebastianbergmann/phpunit

我有一些代码会发出我无法控制的标头,就像 #720 一样。 正如建议的那样,我在一个单独的进程中运行了测试,并且它在使用由 composer 在 vendor/bin 目录中安装的 phpunit 运行时工作。

但是,当使用 phpunit PHAR 存档运行时它不起作用,“标题”仍然发送,经过一番挖掘,我发现它是第一个 shebang 行“#!/usr/bin/env php”在 stup.php这导致了这个麻烦。

如果 PHAR 通过 'require' 作为库加载,则绝对不需要 shebang 行。

或者,在加载 phar 存档之前,通过在https://github.com/sebastianbergmann/phpunit/blob/master/src/Util/PHP/Template/TestCaseMethod.tpl.dist中调用“ob_start()”来解决此问题,但这不是它应该做的方式。

最有用的评论

如果我在运行测试时不使用--stderr作为参数,我会遇到这个问题......

所有26条评论

@sebastianbergmann有没有机会解决这个问题? 它可以防止 Symfony2 测试套件在使用 phar 时通过

我不知道正确的解决方法是什么。 从 PHAR 存根中删除 shebang 线会使 PHAR 无法使用,不是吗?

@fabpot你知道如何解决这个问题吗?

删除它确实会使phpunit.phar无法直接运行,需要使用php phpunit.phar

因此,正如@techlivezheng所建议的那样,也许使用 phar 包含周围的输出缓冲区可以解决问题。
目前,仅在需要 phar 后才启动输出缓冲区

@sebastianbergmann该修复程序将作为 4.0.19 版本发布,还是仅在 4.1 或 4.2 中可用?

它将在 4.0.19 中。

修复也将在 PHPUnit 3.7.37 中。

你打算什么时候发布呢? 这是为了让我们决定如何处理修复 Travis 构建(运行phpunit --self-update以获取较新的 4.0 phar 或使用 composer 从源代码安装)

PHPUnit 3.7.37 和 PHPUnit 4.0.19 已经发布。

嗨,大家好。

我有 phpUnit 4.4.1,但问题仍然存在。 我尝试了 phar 和 composer bin(分离的进程而不是),但问题仍然存在。

如果分开,我会得到以下异常:
PHP Fatal error: Uncaught exception 'Exception' with message 'Serialization of 'SplFileInfo' is not allowed' in phar:///usr/local/bin/phpunit/phpunit/Util/GlobalState.php:211

如果使用作曲家 bin:

Call stack:
Exception: Serialization of 'SplFileInfo' is not allowed in /path/to/project/vendor/phpunit/phpunit/src/Util/GlobalState.php on line 211

  0.0004     641608   1. {main}() /path/to/project/vendor/phpunit/phpunit/phpunit:0
  0.0161    1666672   2. PHPUnit_TextUI_Command::main() /path/to/project/vendor/phpunit/phpunit/phpunit:62
  0.0161    1667712   3. PHPUnit_TextUI_Command->run() /path/to/project/vendor/phpunit/phpunit/src/TextUI/Command.php:138
  0.4945   14352368   4. PHPUnit_TextUI_TestRunner->doRun() /path/to/project/vendor/phpunit/phpunit/src/TextUI/Command.php:186
  2.0006   17549968   5. PHPUnit_Framework_TestSuite->run() /path/to/project/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:423
  4.7326   22223080   6. PHPUnit_Framework_TestSuite->run() /path/to/project/vendor/phpunit/phpunit/src/Framework/TestSuite.php:751
  9.4326   24608496   7. PHPUnit_Framework_TestSuite->run() /path/to/project/vendor/phpunit/phpunit/src/Framework/TestSuite.php:751
  9.4356   24613072   8. PHPUnit_Framework_TestCase->run() /path/to/project/vendor/phpunit/phpunit/src/Framework/TestSuite.php:751
  9.4394   24695088   9. PHPUnit_Util_GlobalState::getGlobalsAsString() /path/to/project/vendor/phpunit/phpunit/src/Framework/TestCase.php:651
  9.4405   24705776  10. PHPUnit_Util_GlobalState::exportVariable() /path/to/project/vendor/phpunit/phpunit/src/Util/GlobalState.php:185
  9.4405   24705856  11. serialize() /path/to/project/vendor/phpunit/phpunit/src/Util/GlobalState.php:211

如果没有,标题错误:
Cannot modify header information - headers already sent by (output started at phar:///usr/local/bin/phpunit/phpunit/Util/Printer.php:172)

在我的代码中,我使用会话,但如果这很重要,则不使用标题。

有谁知道我该如何解决或避免它?

PS 由于此评论,我的 phpUnit 安装似乎有问题。

PHPUnit 4.5.0 - 无法修改标头信息 - 标头已由(输出开始于 phar:///usr/local/bin/phpunit/phpunit/Util/Printer.php:137)

我还可以确认PHPUnit 4.5.1正在输出Uncaught PHP Exception PHPUnit_Framework_Error_Warning: "Cannot modify header information - headers already sent by (output started at /workspaces/app/tests/vendor/phpunit/phpunit/src/Util/Printer.php:139)"

最新的 PHPunit 4.8.6 也抛出了同样的错误

Cannot modify header information - headers already sent by (output started at phar:///usr/local/zend//tests/phpunit-4.8.5.phar/phar/phpunit/Util/Printer.php:133)

Sebastian Bergmann 和贡献者的 PHPUnit 5.2.0。

ErrorException:无法修改标头信息 - 标头已发送(输出开始于 phar:///usr/bin/phpunit/phpunit/Util/Printer.php:134)

仅仅因为错误消息包含“无法修改标头信息”并不意味着这是同一个问题。

好吧,我的测试没有使用任何与标题相关的东西。 此外,它适用于以前的 php 版本,测试没有改变,但现在它抛出了这个错误。 所以我认为这可能是相关的。

适用于单个测试,例如 --filter=mytest

我也有同样的问题:

PHPUnit 5.5.4 by Sebastian Bergmann and contributors.
Starting test 'ExampleTest::testBasicExample'.
Cannot modify header information - headers already sent by (output started at ../vendor/phpunit/phpunit/src/Util/Printer.php:134)

如果我在运行测试时不使用--stderr作为参数,我会遇到这个问题......

我遇到了与此处描述的相同的问题:Maatwebsite/Laravel-Excel/issues/511

这个问题可以通过使用自定义打印机来绕过,在该打印机中,您将 `$out' 显式设置为 stdout

当我的控制器被重定向到登录页面时,我遇到了这个问题。

使用@victorbstan解决方案修复:

bin/phpunit --stderr

我在 docker 容器 throw PHPStorm中运行Codeception测试时遇到了这个问题。
在相同的测试和配置中直接在 Docker 容器中运行 codecept 以避免这个问题。

Yii2 Alert()小部件无法启动会话

@dynasource STDOUT 将在此处关闭
https://github.com/sebastianbergmann/phpunit/blob/e6e7085fbbd2e25f4ca128ac30c1b0d3dd4ef827/src/Util/Printer.php#L73 -L78
这也将被重新定义

/**
 * For avoid set headers before session start
 * Class PhpStorm_Codeception_ReportPrinter_Redefine
 */
class PhpStorm_Codeception_ReportPrinter_Redefine extends PHPUnit_TextUI_ResultPrinter
{
    /**
     * <strong i="10">@inheritDoc</strong>
     */
    public function __construct(
        $out = null,
        $verbose = false,
        $colors = self::COLOR_DEFAULT,
        $debug = false,
        $numberOfColumns = 80,
        $reverse = false
    ) {
        parent::__construct(STDOUT, $verbose, $colors, $debug, $numberOfColumns, $reverse);
    }

    /**
     * Flush buffer and close output if it's not to a STDOUT stream
     */
    public function flush()
    {
        if ($this->out != STDOUT) {
            parent::flush();
        }
    }
}

Sebastian Bergmann 和贡献者的 PHPUnit 8.5.0。
PHP 致命错误:未捕获无法修改标头信息 - 标头已发送(输出开始于 phar:///usr/local/bin/phpunit/phpunit/Util/Printer.php:99)

啊抱歉.. --stderr 解决我的问题

当我的控制器被重定向到登录页面时,我遇到了这个问题。

使用@victorbstan解决方案修复:

bin/phpunit --stderr

我在测试下载文件请求时遇到了同样的问题。
添加参数后也适用于我。
惊人的!

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

stephen-leavitt-sonyatv-com picture stephen-leavitt-sonyatv-com  ·  4评论

rentalhost picture rentalhost  ·  4评论

sebastianbergmann picture sebastianbergmann  ·  3评论

nicklevett picture nicklevett  ·  4评论

AnmSaiful picture AnmSaiful  ·  4评论