views:

29

answers:

2

Hi

I have been trying to use PHPUnit to test an application. I have it all working, but cannot test redirects.

My redirects are occurring inside an Acl Controller Plugin, not inside an Action in a Controller.

I have changed them to use the suggested format of

$r = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
$r->gotoSimple("index", "index", "default");

But this fails in the tests, the response body is empty and I get errors like

Zend_Dom_Exception: Cannot query; no document registered

If I then change the test so that the dispatch method does not result in gotoSimple() being called then the test runs correctly.

How am I supposed to do a redirect in my application so that it runs correctly with Zend_Test's response object?

The Zend docs cover this in about two lines, which I have tried and it fails.

Thanks.

+1  A: 

To test that redirect has occurred, you need to add

$this->assertRedirectTo( 'index' );

after running $this->dispatch();

You cannot query the response body, since it's empty in case of redirect (that's where your exception comes from).
You can always check what the response actually looks like with

print_r( $this->getResponse() );
Vika
Ah. That makes sense. Wish I had known that before - so the test does not follow the redirect?
jakenoble
It doesn't overwrite the response object with the content obtained from redirect - if it's what you mean
Vika
Right - now I know that I can get on. Something that the docs fail to mention!
jakenoble
+1  A: 

Make sure, your actions return anything after redirections, because Zend_Test_PHPUnit disables redirects, so the code after redirect is executed as well.

$r = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
$r->gotoSimple("index", "index", "default");
return;

or

$r = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
return $r->gotoSimple("index", "index", "default");

To test the redirect itself, you may use assertRedirect* assertions.

Read the above manual, because there are important notes about action hooks.

takeshin
I always return after any type of redirect, I think my issue stems from the fact I thought that the test would return the content of the redirect. Now I know it doesn't well is well.
jakenoble