PHPUnit 输出导致 Zend_Session 异常

发布于 2024-10-28 13:02:39 字数 1448 浏览 3 评论 0原文

我收到许多与此完全相同的错误:

Zend_Session_Exception: Session must be started before any output has been sent to the browser; output started in /usr/local/zend/share/pear/PHPUnit/Util/Printer.php/173

运行我的应用程序的测试套件时。这是 PHPUnit 3.5.10 和 PHP 5.3.5 的情况。

没有神秘的、意外的空白输出导致这种情况。我已经确定“发送到浏览器的输出”是正在执行的 PHPUnit 测试的实际输出。如果我打开 PHPUnit/Util/Printer.php 并用 if (strpos($buffer, 'PHPUnit 3.5.10 by Sebastian Bergmann') === false 包装 print $buffer 行) (有效地停止 PHPUnit 的第一行输出),然后我的第一个测试成功(直到测试用例输出一个点表明测试成功,然后下一个测试失败,因为该点被输出)。

我团队中的另一位开发人员能够成功运行完整的测试套件,因此我知道这不是应用程序代码的问题。这一定是我的本地环境的某些配置设置或问题。

我已经检查了 php.ini 以验证 output_buffering 已打开且 implicit_flush 已关闭,确实如此。

我还尝试将 Zend_Session::$_unitTestEnabled = true; 添加到我的测试引导程序中,但这没有帮助(并且无论如何都不应该是必要的,因为它可以在其他开发人员的机器上和我们的机器上运行) CI 服务器没有它)。

除了忽略错误之外还有什么建议吗?我从来没有见过这样的事情,真的很茫然。

谢谢!

更新:

为了尝试进一步隔离问题,我通过执行以下测试脚本将 ZF 和我的应用程序排除在外:

<?php

class SessionTest extends PHPUnit_Framework_TestCase
{
    public function testSession()
    {
        session_start();
        $this->assertTrue(true);
    }
}

测试失败:

1) SessionTest::testSession
session_start(): Cannot send session cookie - headers already sent by (output started at /home/mmsa/test.php:1)

但是,完全相同的测试可以在朋友的计算机上进行。 PHP 和 PHPUnit 版本相同。

I am getting numerous errors exactly like this one:

Zend_Session_Exception: Session must be started before any output has been sent to the browser; output started in /usr/local/zend/share/pear/PHPUnit/Util/Printer.php/173

When running my application's test suite. This is with PHPUnit 3.5.10 and PHP 5.3.5.

There is no mysterious, unexpected whitespace output that is causing this. I've determined that the "output being sent to the browser" is the actual output from the PHPUnit tests being executed. If I open up PHPUnit/Util/Printer.php and wrap the print $buffer line with if (strpos($buffer, 'PHPUnit 3.5.10 by Sebastian Bergmann') === false) (effectively stopping the first line of output from PHPUnit), then my first test succeeds (until the test case outputs a dot indicating that the test succeeded, then the next test fails because the dot was output).

Another developer on my team is able to run the full test suite successfully, so I know it's not a problem with the application code. It must be some configuration setting or problem with my local environment.

I've already checked php.ini to verify that output_buffering is turned on and implicit_flush is turned off, and they are.

I've also tried adding Zend_Session::$_unitTestEnabled = true; to my test bootstrap, but that didn't help (and shouldn't be necessary anyway because it works on another developer's machine and on our CI server without it).

Any suggestions besides ignoring the errors? I've never seen anything like this and am truly at a loss.

Thanks!

UPDATE:

To attempt to further isolate the problem, I took ZF and my application out of the equation by executing the following test script:

<?php

class SessionTest extends PHPUnit_Framework_TestCase
{
    public function testSession()
    {
        session_start();
        $this->assertTrue(true);
    }
}

The test fails:

1) SessionTest::testSession
session_start(): Cannot send session cookie - headers already sent by (output started at /home/mmsa/test.php:1)

However, the exact same test works on a friend's machine. Same version of PHP and PHPUnit.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

人心善变 2024-11-04 13:02:39

使用 -stderr 标志运行 phpunit,(较新的版本可能会使用 --stderr 代替)例如,

 phpunit -stderr mytest.php
 # or
 phpunit --stderr mytest.php

这会将 phpunit 的输出定向到 stderr,防止它中断 HTTP 标头生成。

该测试可能在您朋友的机器上运行,因为他启用了输出缓冲(尽管我不确定这在 CLI 上下文中是否相关)。

Run phpunit with the -stderr flag, (newer versions may use --stderr instead) e.g.

 phpunit -stderr mytest.php
 # or
 phpunit --stderr mytest.php

This directs phpunit's output to stderr, preventing it from interrupting HTTP header generation.

It's possible that the test works on your friend's machine because he has output buffering enabled (although I'm not sure if that's relevant in a CLI context).

美胚控场 2024-11-04 13:02:39

我认为更好的方法是使用

Zend_Session::$_unitTestEnabled = true;

在我的测试引导程序中使用它可以防止此错误。

I think better way is use

Zend_Session::$_unitTestEnabled = true;

Using this in my test bootstrap prevents from this error.

ヅ她的身影、若隐若现 2024-11-04 13:02:39

如果您系统上的 PHPUnit 使用的 php 二进制文件是 CGI 而不是 CLI 版本,那么 session_start 确实会尝试设置 cookie,并且您会收到该错误。

您可以通过调用 php_sapi_name 检查以确定您正在使用什么 SAPI。

If the php binary being used by PHPUnit on your system is the CGI instead of the CLI version, then session_start is really going to try to set cookies and you'll get that error.

You can check to make sure what SAPI you're using by calling php_sapi_name.

南汐寒笙箫 2024-11-04 13:02:39

我在另一个项目中遇到了同样的问题,我发现问题是 PHPUnit 导致输出启动得太早,因为它在运行测试之前输出欢迎消息。

bootstrap.php 中添加了以下两行:

ini_set('session.use_cookies', 0);
ini_set('session.cache_limiter', '');

这应该可以防止在测试套件运行之前发送标头。

I had the same problem with another project, and I found that the issue was PHPUnit causing output to start too soon, because it outputs it's welcome message before running your test.

Added the following two lines to bootstrap.php:

ini_set('session.use_cookies', 0);
ini_set('session.cache_limiter', '');

This should prevent the headers from being sent before your test suite runs.

眼眸里的快感 2024-11-04 13:02:39

就像 Makor 所说,你只需要将其添加到你的 bootstrap.php

Zend_Session::$_unitTestEnabled = true;

,在我的例子中,我在我的 bootstrap.php 文件中添加了类似的内容,

if(APPLICATION_ENV == 'testing' && php_sapi_name() == 'cli') {
    Zend_Session::$_unitTestEnabled = true;
}

因此您不必再更改它。

Like Makor said, you just need to add this in your bootstrap.php

Zend_Session::$_unitTestEnabled = true;

In my case, I put something like in my bootstrap.php file

if(APPLICATION_ENV == 'testing' && php_sapi_name() == 'cli') {
    Zend_Session::$_unitTestEnabled = true;
}

So you don't have to change it anymore.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文