Can I “Mock” time in PHPUnit?

后端 未结 11 493
梦谈多话
梦谈多话 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:10

    Disclaimer: I wrote this library.

    You can mock time for test using Clock from ouzo-goodies.

    In code use simply:

    $time = Clock::now();
    

    Then in tests:

    Clock::freeze('2014-01-07 12:34');
    $result = Class::getCurrDate();
    $this->assertEquals('2014-01-07', $result);
    

提交回复
热议问题