Can I "Mock" time in PHPUnit?

For those of you working with symfony (>= 2.8): Symfony's PHPUnit Bridge includes a ClockMock feature that overrides the built-in methods time, microtime, sleep and usleep.

See: http://symfony.com/doc/2.8/components/phpunit_bridge.html#clock-mocking


I recently came up with another 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.

For further reading I described it in detail in my blog

Tags:

Php

Phpunit