0

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.

Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
Luke
  • 768
  • 2
  • 14
  • 33

1 Answers1

2

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.

SamHennessy
  • 4,288
  • 2
  • 18
  • 17
  • thanks for the response. Unfortunately, using the the `@runInSeparateProcess` annotation and/or the --process-isolation flag cause MakeGood to just sit there doing nothing. The only console output is PHPUnit 3.5.15 by Sebastian Bergmann.. I am actually using Zend_Test_PHPUnit. Not sure what you mean by using Zend_Http_Response. Here's my controller action. public function robotsAction() { header('Content-type: text/plain'); $this->_helper->layout()->disableLayout(); } – Luke Mar 20 '12 at 19:45
  • 1
    Try replacing your call to header() with **$this->getResponse()->setHeader('Content-type', 'text/plain');** – SamHennessy Mar 20 '12 at 19:52
  • Thanks a bunch! MakeGood is happy now, and thanks for the tip about Zend_Http_Response. :) – Luke Mar 20 '12 at 19:59