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