Symfony测试

系统教程 行业动态 更新时间:2024-06-14 16:55:57
Symfony测试 - ClockMock / DateTime(Symfony Testing - ClockMock / DateTime)

我正在为基于JSON API的应用程序创建复杂测试寻求帮助。

短问题情景:

测试创建临时实体(用于比较) EntityManagerMock设置为等待持久化($ temporaryEntity) 使用一些参数测试调用实际方法 方法创建实体并尝试保存在模拟的EM中 测试失败,因为时间变化1秒

我们尝试使用ClockMock,但它只适用于名为/ for namespace的类。 我们的方法是在其他命名空间,实体是在下一个创建的。

我们正在寻找解决方案如何临时覆盖PHP平台\ DateTime类 - 或技术如何将ClockMock用于项目中的所有命名空间/类(但仅用于测试)。 我们尝试修改基于Symfony的ClockMock - 但找不到好方法: - /。

有什么办法吗? (我不想使用类似get_declared_class()的东西并使用它调用clockMock :: register - 因为它对于大约15.000次测试的解决速度非常慢: - /。

谢谢大家!

I looking for help with creating complex test for our JSON API based application.

Short problem scenario:

Test create temporary entity (for compare) EntityManagerMock is set to wait for persist($temporaryEntity) Test call real method with some parameters Method create entity and try to save at mocked EM Test fails because time change for 1 second

We tried to use ClockMock but it's work only for class where was called/for namespace. Our method is at other namespace and entity is created in some next.

We looking for solution how to temporary override PHP platform \DateTime class - or technique how to use ClockMock for all namespaces/class in project (but only for tests). We tried to modify Symfony based ClockMock - but good way not found :-/.

Is there any way? (I dont want use something like get_declared_class() and call with it clockMock::register - because it's very slow soloution for about 15.000 tests :-/.

Thanks all!

最满意答案

ClockMock不适用于new \DateTime()但仅适用于基于时间的PHP函数。

从宣布的功能 :

这意味着您不需要对原始代码进行单个更改,除非使用new DateTime() ,必须由DateTime::createFromFormat('U', time())替换以使用DateTime::createFromFormat('U', time()) time()功能。

因此,按照建议尝试:使用带有time函数的named-constructor替换任何DateTime对象创建。

希望这有帮助

The ClockMock doesn't work for new \DateTime() but only on time-based PHP function.

From the announcement of the features:

This means that you don't need to make a single change in your original code, except when using new DateTime(), which must be replaced by DateTime::createFromFormat('U', time()) to use the mocked time() function.

So try as suggested: replace any DateTime object creation with the named-constructor with the time function.

Hope this help

更多推荐

本文发布于:2023-04-10 11:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/ced109d940e0bed646fbcd248cb18baf.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:测试   Symfony

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!