PHPUnit输出导致Zend_Session异常 [英] PHPUnit output causing Zend_Session exceptions

查看:196
本文介绍了PHPUnit输出导致Zend_Session异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了很多与此完全相同的错误:

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

运行应用程序的测试套件时.这是与PHPUnit 3.5.10和PHP 5.3.5一起使用的.

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

没有导致此现象的神秘,意外的空白输出.我确定输出到浏览器的输出"是正在执行的PHPUnit测试的实际输出.如果我打开PHPUnit/Util/Printer.php并用if (strpos($buffer, 'PHPUnit 3.5.10 by Sebastian Bergmann') === false)包裹print $buffer行(有效地停止了PHPUnit输出的第一行),那么我的第一个测试成功了(直到测试用例输出一个点,表明测试成功,则下一个测试失败,因为输出了点.

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.

我已经检查了php.ini,以确认output_buffering已打开并且implicit_flush已关闭,并且它们都已打开.

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

我也尝试将Zend_Session::$_unitTestEnabled = true;添加到我的测试引导程序中,但这没有帮助(而且也没有必要,因为它可以在其他开发人员的机器上以及没有它的CI服务器上运行).

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.

谢谢!

更新:

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

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);
    }
}

测试失败:

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

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

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

推荐答案

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

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

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

这会将phpunit的输出定向到stderr,从而防止其中断HTTP标头的生成.

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

该测试可能在您朋友的计算机上进行,因为他启用了输出缓冲(尽管我不确定在CLI上下文中是否有意义).

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).

这篇关于PHPUnit输出导致Zend_Session异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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