通过模拟测试PHP应用程序中的MongoDB交互 [英] Test MongoDB Interactions in a PHP Application With Mocking

查看:75
本文介绍了通过模拟测试PHP应用程序中的MongoDB交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与MongoDB和PHPUnit高效配合的最佳实践是什么?我应该(或可以)用来模拟访问MongoDB的对象? PHPUnit模拟器 Phophphecy 制药?

What is the best practice to work efficiently with MongoDB and PHPUnit? What should (or could) I use to mock objects that access MongoDB? PHPUnit Mocker, Mockery, Phrophecy, Phactory?

推荐答案

如果您查看SQL数据库的模拟数据,这里有很多意见.

If you look at mocking data for SQL databases, there are lots of opinions here.

  • 有人建议使用内存中的SQL数据库.
  • 有些人只是嘲笑ORM调用,并假设已测试ORM到DB的部分.
  • 有些人只是使用本地"数据库进行单元测试,而忽略了整个模拟"概念.

由于对SQL缺乏共识,因此您对MongoDB等新数据库的共识甚至更低.

Given the lack of consensus on SQL, it's even less likely that you will find consensus on the new DBs like MongoDB.

我认为这里有一些重要的细节要考虑.

I think there are some important details to consider here.

  • 您是否正在使用某种形式的ORM/ODM?直接是司机吗?
  • 您是否要模拟与数据库的所有通信?您是否要模拟ODM?

如果您只是想模拟与数据库的通信,那么理想的解决方案是MongoDB驱动程序的伪造"实现.这可能是很多工作,因为从未在编写驱动程序时就牢记可模拟性".

If you are just trying to mock communications to DB, then the ideal solution is a "fake" implementation of the MongoDB driver. This is probably a lot of work as the driver was never written with "mockability" in mind.

如果您有ODM,则可以简单地模拟ODM调用,并假定ODM正在进行其工作.理想情况下,ODM应该提供一些可模拟的接口,但这并非总是如此.

If you have an ODM, then you can simply mock the ODM calls and assume the ODM is doing its job. Ideally the ODM should provide some mockable interface, but this is not always the case.

同样,此答案可以归结为您真正打算测试的内容以及您认为是好的单元测试的内容.不幸的是,这些产品大多数仍然是很新的,因此在这个领域几乎没有指导.

Again, this answer comes back down to what you're really planning to test and what you consider as a good unit test. Unfortunately, most of these products are still very new so there is very little guidance in this space.

这篇关于通过模拟测试PHP应用程序中的MongoDB交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆