3

I'm currently on the process of unit testing a plugin using the Wordpress unit testing framework.

In one of the functions to be tested,it will send mails to users in a periodic time interval so it means that the PHP script will need to be executed at a set time, say every 2 minutes.

Is there a way to integrate cron functionality with the uni test framework,so if run the command:

phpunit

In the command line, it will run the script every n intervals?

Or is there a way I could simulate cron environment without actually running cron?

Thanks for any tips.

1 Answer 1

1

I'm assuming your referring to a server based cron job. I'm not sure if your familiar with it but you could also use WP Cron. It's an api that built into core. It's a little easier to work with than a true cron job IMHO. It does have a mild caveat however. Which could also be it's strength.

The scheduled events do not fire based on the servers internal clock alone. A user or a visitor of some sort needs to visit the site before any scheduled events are fired.

After the user / visitor visits WP Cron checks it's timers and fires the ones that are due.

http://code.tutsplus.com/articles/insights-into-wp-cron-an-introduction-to-scheduling-tasks-in-wordpress--wp-23119

You could use this to your advantage in your phpunit tests. This could become even a more viable solution if you're also using a headless browser such as selenium web driver http://docs.seleniumhq.org/ to test front end functionality as well as the backend.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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