2014-10-02 29 views
0

的ZF1的应用程序,我写测试,这里是我的控制器的初始化:的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'); 
} 

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

如何在检测到重定向后,如果我添加die()或exit()甚至$ this - > _ redirector- > setExit(真);它会完全停止phpUnit会话,并且不会运行测试。

回答

0

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

+0

nope,它仍然会抛出退出并杀死phpUnit会话 – devilsansclue 2014-10-03 15:57:58