phpUnit / ZF1 - 重定向后停止执行代码

时间:2014-10-02 18:56:01

标签: zend-framework phpunit

我正在为ZF1应用程序编写测试,这是我的控制器的初始化:

public function init()
{
    $this->_redirector = $this->_helper->getHelper('Redirector');

    $action = $this->getRequest()->getActionName();
    if(!in_array($action,array('login','logout'))) {
        $auth = Zend_Auth::getInstance();

        if ($auth->hasIdentity()) {
            // Identity exists; get it
            $identity = $auth->getIdentity();
        } else {
            $this->_redirector->gotoUrl('/hr/index/login?redirect='.urlencode($this->getRequest()->getRequestUri()));
        }
    }
    $this->_user = AdminUserQuery::create()->findOneByUsername($identity);
    $this->view->user = $this->_user;
    ...
}

和有问题的测试:

public function testNoAuthGivesLogin() {
    $this->dispatch('/hr');
    $this->assertRedirectTo( '/hr/index/login?redirect=%2Fhr' );
}

简单...基本上如果没有auth,重定向到登录。这在浏览器中有效,但在phpUnit中,它继续执行重定向后的代码,这意味着在它下面设置的属性不存在,特别是$ this-> _user,所以在我的索引操作中,当它调用getId时()$ this-> _user,它会抛出错误

如果我检测到重定向,如果我添加die()或exit()甚至$ this-> _redirector-> setExit(true),我如何告诉phpUnit停止执行代码?它完全停止了phpUnit会话,没有测试运行。

1 个答案:

答案 0 :(得分:0)

您可能会发现代码在浏览器中继续执行,您只是看不到错误。我相信你想要使用gotoUrlAndExit()

相关问题