views:

20

answers:

1

I'm unit testing some extensions of Zend_Controller_Action in SimpleTest. I want to be able to set a redirect url using the set methods of the Redirector action helper, and then use Redirector's redirectAndExit() method to actually redirect later in the process. This process doesn't seem to be working as I expected from reading the documentation and looking at the code in the action controller, redirector and response classes.

Here's a UnitTestCase method I wrote:

  public function testSetGoToUrl() {

    $request    = new Zend_Controller_Request_Http();
    $response   = new Zend_Controller_Response_Http();
    $controller = new App_Zend_Controller_Action($request, $response, array());

    $controller->getHelper('redirector')->setGoToUrl('/');

  }

The App_Zend_Controller_Action class is merely a concrete extension of the abstract class Zend_Controller_Action. All I'm doing here is instantiating the controller and setting the redirection url. The SimpleTest reporter is sending the headers first, as expected. But this test method generates a "headers sent" exception and I don't understand why. I'm not aware of any run() or dispatch() methods being called in this situation.

What's sending the second set of headers?

A: 

While testing you should use Zend_Controller_Response_HttpTestCase and Zend_Controller_Request_HttpTestCase.
Zend_Controller_Response_HttpTestCase::sendHeaders() returns an array of all headers that would be sent instead of executing real header() statements.

These are also used in Zend_Test_PHPUnit_ControllerTestCase.

Benjamin Cremer
That's a good idea, and I just figured out what's going on. Nothing attempts to send the second set of headers; the exception is being generated by the canSendHeaders() method of the response object, which is called when the redirect is set by the response object.Thanks!
Robin Canaday