1

如何捕捉ZF2/ZF3中控制器操作的輸出(ViewModel)?如何測試ZF2/ZF3中的動作輸出?

背景:

我正在寫一個Zend框架3應用程序(從ZF2只是遷移)的一些集成測試。我正在使用PHPUnit v6.2.2和Zend \ Test v3.1.0。我想測試從調用路由的那一刻起到保存/檢索數據的那一刻的過程的一部分。這意味着在給方向的所有控制器操作的測試:

  1. 預期(爲了這個,我想打電話給路由/動作和校驗,那麼新的數據庫狀態)得到的數據保存;
  2. 數據按預期檢索(爲此,我想調用路由/操作並檢查操作的輸出)。

第一個方向是明確的:在調用路由之後,我只是簡單地啓動普通數據庫請求並檢查是否有預期的更改。

public function testBuzAction() 
{ 
    $this->dispatch('/foo/bar/buz'); 
    // Here might be optionally some asserts, whether the correct action is called... 
    // Here are the database checks... 
} 

但是對於其他方向,我們需要ViewModel,由操作返回。

public function testBuzAction() 
{ 
    $this->dispatch('/foo/bar/buz'); 
    // Here might be optionally some asserts, whether the correct action is called... 
    // Here is the ViewModel output of the Bar#buzAction() analyzed. 
} 

如何獲得PHPUnit測試中的動作輸出?

回答

1
public function testBuzAction() 
{ 
    $this->dispatch('/foo/bar/buz'); 
    ... 
    $viewModelReturnedByAction = $this->getApplication()->getMvcEvent()->getResult(); 
}