Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
360 views
in Technique[技术] by (71.8m points)

php - PHPUnit output causing Zend_Session exceptions

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...