Zend_Test - Setting redirect in Controller Plugin

2019-03-30 08:11发布

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.

2条回答
forever°为你锁心
2楼-- · 2019-03-30 09:01

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.

查看更多
【Aperson】
3楼-- · 2019-03-30 09:11

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() );
查看更多
登录 后发表回答