如何改变当前时间为PHP的单元测试日期函数 [英] how to change current-time for unit testing date functions in php

查看:160
本文介绍了如何改变当前时间为PHP的单元测试日期函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更改当前时间(即时间()的输出)在PHP的单元测试日期操作类?

how do i change the current time (i.e. the output of time()) in php for unit-testing date-manipulation-class?

推荐答案

最近,我想出了一个解决方案,是伟大的,如果你使用的是PHP 5.3的命名空间。您可以实现一个新的时间()函数,当前的命名空间内,创造你设置在测试返回值的共享资源。然后到时间(任何不合格的调用)将使用新的功能。

I recently came up with a solution that is great if you are using PHP 5.3 namespaces. You can implement a new time() function inside your current namespace and create a shared resource where you set the return value in your tests. Then any unqualified call to time() will use your new function.

有关进一步的阅读我在<一个描述它的细节href=\"https://www.schmengler-se.de/en/2011/03/php-mocking-built-in-functions-like-time-in-unit-tests/\"相对=nofollow>博客

For further reading I described it in detail in my blog

这篇关于如何改变当前时间为PHP的单元测试日期函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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