Unit Testing a Plugin functionality with cron

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.

Read More

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.

Related posts

Leave a Reply

1 comment

  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.