Zend_Test - Setting redirect in Controller Plugin for PHPUnit

◇◆丶佛笑我妖孽 提交于 2019-12-09 13:42:06

问题


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:


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() );



回答2:


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.



来源:https://stackoverflow.com/questions/3565637/zend-test-setting-redirect-in-controller-plugin-for-phpunit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!