2014-10-02 92 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