PHP: “Mocking” built-in functions like time() in Unit Tests

Dieser Artikel ist nur auf Englisch erschienen.

12 Replies to “PHP: “Mocking” built-in functions like time() in Unit Tests”

  1. This looks excellent. The cleanest way to mock time that I have seen so far !
    Thanks a lot for the tip 🙂

  2. I liked this strategy so much… I pushed it a little further down the line 🙂

    In this post in programmers stackexchange I was searching for a way to organize my tests.

    I finally implemented a solution following the steps you describe in this page, overriding the DateTime() function. Available here for the curious 🙂

    Thanks again !

  3. I’ve just refactored this method for my CakePHP 3 app in it works a treat. Thank you so much!

  4. “Important Implication: It does not work if you use the global functions with fully qualified names (i.E. \time()) in your test subjects!”

    So, any ideas on how to test when the fully qualified name is used?

  5. Huge help! Made it easy to essentially “disable” the mail() function in our Email class by simply returning true when the mail() function is called.

  6. The problem with this approach is it requires you to make assumptions about the internal workings of a given class, meaning that if you change how a class actually works then your unit tests might break even though the class itself is still fine.

Comments are closed.