PHPUnit output causing Zend_Session exceptions
Asked Answered
K

5

27

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.

Kristinkristina answered 31/3, 2011 at 19:21 Comment(0)
U
37

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

Ulick answered 1/4, 2011 at 3:20 Comment(5)
Disregard my previous comment - this actually did work. I had some debugging code still in the PHPUnit core while I was trying to figure this out last night. When I reverted it, the --stderr flag allowed the tests to work. Thanks! This is a workaround, though. If it works on another machine without the --stderr flag, then it should work on mine. From what I can tell, I do not have output buffering disabled. What else can I check?Kristinkristina
It's possible that it's --stderr in newer versions (that would certainly be more standard), but my (somewhat out of date) install of phpunit only supports -stderr. We use -stderr when running all of our tests, due to this session issue. As to what might differ between your environments, I can only recommend comparing the output of php -i on both machines to find configuration differences.Ulick
This works for me too. Unfortunately there doesn't (yet) seem to be an option to put in phpunit.xml for this.Overgrowth
@Overgrowth There is a fix already! github.com/sebastianbergmann/phpunit/issues/657Demoralize
On version 4.6.4 it is --stderr and THANKS that works just great. I have several function that need to set a cookie and this makes it possible to test them.Ramah
W
6

I think better way is use

Zend_Session::$_unitTestEnabled = true;

Using this in my test bootstrap prevents from this error.

Washcloth answered 8/1, 2014 at 15:14 Comment(0)
G
1

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.

Gregorygregrory answered 1/4, 2011 at 3:1 Comment(3)
echo php_sapi_name(); outputs "cli"Kristinkristina
The only other thing I'd say off the top of my head is to make sure that pear config-show | grep php_bin lists the same binary that you're expecting.Gregorygregrory
It points to /usr/local/zend/bin/php, which is the only installation of PHP in my environment.Kristinkristina
B
0

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.

Boni answered 7/2, 2014 at 19:54 Comment(0)
C
0

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.

Canton answered 20/3, 2017 at 13:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.