Can I “Mock” time in PHPUnit?

后端 未结 11 509
梦谈多话
梦谈多话 2020-11-29 01:53

... not knowing if \'mock\' is the right word.

Anyway, I have an inherited code-base that I\'m trying to write some tests for that are time-based. Trying not to be <

11条回答
  •  孤独总比滥情好
    2020-11-29 02:23

    Simplest solution would be to override PHP time() function and replace it with your own version. However, you cannot replace built-in PHP functions easily (see here).

    Short of that, the only way is to abstract time() call to some class/function of your own that would return the time you need for testing.

    Alternatively, you could run the test system (operating system) in a virtual machine and change the time of the entire virtual computer.

提交回复
热议问题