This question already has an answer here:
When using the MakeGood plugin in Eclipse to run a test that sets headers, I get:
Cannot modify header information - headers already sent by (output started at C:\wamp\bin\php\php5.3.8\pear\PHPUnit\Util\Printer.php:173)
This same test works fine when I run it through Phing. I'm assuming that Phing sets output to stderr because when I run the same test from the phpunit command line with the --stderr switch, it works fine. It fails the same way as with MakeGood without the --stderr switch.
Is there a way around this, or a way to set output to stderr in the MakeGood plugin?
Also, this shouldn't make any difference, but this is a Zend Framework project and I've set
Zend_Session::$_unitTestEnabled = true;
in my testing bootstrap.
</div>
The issue is that PHPUnit will print a header to the screen and at that point PHP can't send any more headers.
The work around is to run the test in an isolated process. Here is an example
<?php
class FooTest extends PHPUnit_Framework_TestCase
{
/**
* @runInSeparateProcess
*/
public function testBar()
{
header('Location : http://foo.com');
}
}
This will result in:
$ phpunit FooTest.php
PHPUnit 3.6.10 by Sebastian Bergmann.
.
Time: 1 second, Memory: 9.00Mb
OK (1 test, 0 assertions)
The key is the @runInSeparateProcess annotation.
You can also use the --process-isolation flag when running PHPUnit.
If you are writing code around Zend Framework you should not be using header() directly. You should use Zend_Http_Response.
Also if you are doing MVC level testing I suggest you look at Zend_Test_PHPUnit.