gpt4 book ai didi

php - 使用 PHPUnit,如何在 Zend Framework 2 的 Controller 中模拟 Doctrine Entity Repository 类?

转载 作者:搜寻专家 更新时间:2023-10-31 21:36:07 25 4
gpt4 key购买 nike

我正在关注 guide来自 Zend Framework 2 的单元测试网站。我的“骨架”应用程序与教程略有不同,因为我集成了 Doctrine 2 而不是使用 Zend 的内置 DB 适配器。

在我的 AlbumController.php 的 indexAction() 中,我使用以下片段来检索所有专辑记录:

return new ViewModel(array(
'albums' => $this->getEntityManager()->getRepository('Album\Entity\Album')->findAll()
));

我如何在 IndexControllerTest.php 文件中的 testIndexActionCanBeAccessed() 中模拟它,以便它返回虚拟值?

感谢您的帮助。

最佳答案

创建 EntityManager 的模拟

创建 AlbumRepository 的模拟

创建 AlbumEntity 的模拟

然后使用 PHP UNIT,您需要

create an $entityMock->expects($PHPUnit->once())->method('getRepository')->will($PHPUnit->returnValue($repositoryMock));

create a $repositoryMock->expects($PHPUnit->once())->method('findAll')->will($PHPUnit->returnValue(array($albumEntityMock));

根据您实现 PHPUnit 测试的方式,语法可能会有所不同,但这些步骤应该对您有所帮助。

关于php - 使用 PHPUnit,如何在 Zend Framework 2 的 Controller 中模拟 Doctrine Entity Repository 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19164287/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com