Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test a scheduling program in php

I have a codeigniter app that schedules events for specific dates and times in a mysql DB, sometimes weeks in advance, with another part that will send email reminders and perform other actions when those dates occur. How would one approach time based testing? Is there a testing suite I could use or read about?

like image 719
user1592380 Avatar asked Jul 19 '13 12:07

user1592380


People also ask

What is the PHP scheduling script?

This PHP scheduling script allows you to manage appointment calendars, add & edit appointments manually, manage work hours, employee details and other scheduling details. The front-end is fully compatible with Mac, tablets, desktops, mobiles and other devices.

How to test a PHP script?

Below are some of the ways in which a PHP script can be tested. 1. Create a file with the following contents. Give the file a name such as myphpInfo.php: 2. Copy the file to the your webservers DocumentRoot directory, for example – /var/www/html.

How do I Check my PHP code for errors?

When you test PHP code locally, you can check your PHP scripts for both functionality and errors. Your best option is using XAMPP through your web browser to run your PHP scripts. If you prefer, there are online services available to find errors within your PHP code. If you choose to use XAMPP, follow the steps below.

How can the PHP tutorials help you?

The PHP tutorials can help you with implementation of your own reservation or scheduling application with a PHP backend. Each tutorial includes a detailed explanation of the scheduling component configuration, UI layout and a REST backend implemented in PHP.


2 Answers

This is something that I pondered for quite a while - and then I realized my problem! I was concentrating on time as if it was something I couldn't control and measure. Time is just like any other calculation that happens in our applications. So, time based testing is easy - we just have to change the way we think about it - and how we construct our application.

First off, the number one thing to do is to separate out the current time generation sequence to a method. This way, we can mock this out later (that is, generate our own method that always returns a predetermined 'time' for testing). This can be as simple as something like...

protected function _getCurrentTime()
{
     return time();
}

What is the benefit: in our code, we will call this to get the current time to do calculations. However, in testing, we can always generate the exact same time.

that's important - we can always generate the exact same time, every time.

Now, I'm not entirely certain how to put this into practice in your particular application, but let me explain a 'fake' application - and see if you can adapt it to your specifications.

Ok, first off, we have this method that puts an entry in our database that is a 'reminder' for 3 weeks from now.

this is the old code

public function addReminderToDB()
{
    $this->_executeSQL('insert into mytable values (1,2,date_add(now(), interval 3 week))');
}

So, how do we test this? It's obviously going to be different each time?

Well, let's break it up. This is our new code...

new code

public function addReminderToDB()
{
    $currentDate = date('m-d-Y h:i:s', $this->_getCurrentTime());
    $sql = "insert into mytable values (1,2,date_add('{$currentDate}', interval 3 week))";
    $this->_executeSQL($sql);
 }

Now, when it comes to testing... you can set up your test db. Next, mock out the method _getCurrentTime() to always return an integer that you know. Then, run the addReminderToDB() (it will use that integer you know). Finally, query the datasource to validate the row inserted has the proper values you'd expect.

I know this isn't exactly spot on with what you're asking, but the question is kind of vague too :-/ Best of luck!

like image 160
Aaron Saray Avatar answered Sep 20 '22 15:09

Aaron Saray


Do you want to run a unit test to verify your code is working, or test a production environment?

If you want to make sure your scheduled tasks are running in production... The one I created writes a log every time it successfully completes execution.

Then we test it by checking if it has run (eg: test if "daily" tasks have run in the 36 hours). If they haven't run, then we alert an administrator.

like image 24
Abhi Beckert Avatar answered Sep 21 '22 15:09

Abhi Beckert