如何在 ZF2/ZF3 中测试动作的输出?



如何在ZF2/ZF3中捕获控制器动作的(ViewModel(输出?

背景:

我正在为 Zend Framework 3 应用程序(刚从 ZF2 迁移(编写一些集成测试。我正在使用PHPUnitv6.2.2和Zend\Testv3.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 测试?

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

最新更新