If I have an delayed action in my code then how on Earth can I unit test this?

For example a membership transition?


Totten, who is a complete legend, shared this pearl with me:

there are some examples like: tests/phpunit/Civi/CCase/SequenceListenerTest.php: \CRM_Utils_Time::setTime('2013-11-30 01:00:00'); however, these are the exception rather than the rule. there are lots of places where someone called time() or strtotime() without reading CRM_Utils_Time to find the baseline.

  • I assume that if you encountered such a case of time() or strtotime() being used then a core PR would be welcome? – ErichBSchulz Jan 27 '17 at 12:16
  • a) Yes, please, PR welcome! – Jon G - Megaphone Tech Jan 31 '17 at 15:41
  • b) Ideally, time based actions are written with TDD in mind, and there's a testable function that allows you to pass in a time as an argument. Of course, when we're talking about CiviMember code, that's wishful thinking at best. But it's a good thought for the future. – Jon G - Megaphone Tech Jan 31 '17 at 15:43

You may not need to wait for a delayed action to occur. See if it is possible to test this without any delay.

For example, JobTest::testCallSendReminderLimitTo() and some other scheduled reminder tests actually test a "scheduled" action, by creating the necessary circumstances to have it happen immediately.

A (slow!) integration test might verify that a membership renewal is triggered at twelve months after join, while a group of unit tests might verify each component of that chain (one tests that the membership expiry is set as expected, another tests that the membership expiry rollover happens, another tests that the membership reminder is triggered at rollover).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.