ZF-7496: Impossible to use Zend_Test_PHPUnit_ControllerTestCase to test redirects issued in preDispatch() hooks
This bug/improvement request is related to ZF-7455 , but goes beyond it as this is more than a documentation problem. Zend_Test_PHPUnit_ControllerTestCase seems to be unable to test redirects generated by preDispatch() hooks in action controllers and in controller plugins. That is to say, you cannot apparently use the redirect assertions provided by Zend_Test to test for redirects issued by preDispatch() hooks and plug-ins.
The default behavior of Zend_Controller_Action_Helper_Redirector is to call exit() after a redirect has been issued. This halts further processing of any code after the redirect. Zend_Test disables this default behavior.
To work around this, Matthew suggests that the correct method of calling the redirect action helper within a controller action is to 'return' so as to abort further processing within the action: http://firstname.lastname@example.org/…
// correct return $this->_helper->redirector('index'); // incorrect $this->_helper->redirector('index');
This practice is sufficient to allow redirects from within actions. However, it seems there is no clean way to use those same assertions on redirects that are performed within a Zend_Controller_Action 's preDispatch() method, or,to test a redirect issued within a preDispatch() method on a Zend_Controller_Plugin that is attached to the controller.
For example, I have an ACL plugin which checks to see whether the user must be logged in to view the requested action. If the user must be logged in, then the preDispatch() method of the plugin calls the redirector helper to redirect to the login controller. However, since Zend_Test has disabled the exit() functionality of the redirector helper, the dispatcher still proceeds to dispatch to the originally requested action. Since that originally requested action expected the user to be logged in, it throws errors of its own. The end result being (I think because I have an error controller specified?) that an HTTP 500 error is thrown and Zend_Test's assertRedirect() methods return false instead of true ...
There has got to be a cleaner way to do this... Surely there are cases where Zend_Test needs to be used for integration testing to confirm that preDispatch() code which issues redirects is playing well with the code in the actions?