views:

48

answers:

1

I am trying to write tests for a date helper class. I am wondering what is best practice for this.
Here is an example in PHP.

    public static function get_date($offset = 0){
        $date = mktime(0, 0, 0, date('m')  , date('d') + $offset, date('Y'));
        return array(
            'day'   => date('D',$date),
            'month' => date('M',$date),
            'year'  => date('Y',$date)
        );
    }

    public static function today(){
        return self::get_date();
    }

    public static function tomorrow(){
        return self::get_date(1);
    }

    public static function yesterday(){
        return self::get_date(-1);
    }

So what I am looking for is examples of tests that could test these functions or a new way to write these functions so they are intuitively testable.

I have found examples in Java but they seem pretty inelegant and I really don't know Java, unless you count Actionscript 3 as Java ;)

Solutions in Javascript or Ruby would also be super helpful as examples but my class is written in PHP so that would be ideal.

Thanks!

+2  A: 

I usually do something like this:

<?php

class Date
{
    protected $date;

    public function __construct($date = null)
    {
        $this->date = $date ? $date : time();
    }

    public function today()
    {
        // to something with $this->date
    }
}


class DateTest extends PHPUnit_Framework_TestCase
{
    public function testToday()
    {
        $date = new Date(strtotime('2010-10-17 00:00:00'));
        $this->assertEquals($expectedValue, $date->today());
    }
}

It follows the same principle that date() does, which is to receive a second optional argument, the reference time.

Ionuț G. Stan
I'd like to keep the functions static. Whaddaya think?
pferdefleisch
Static methods are regarded as a no-no in the unit testing world because it makes code that depend on them hard to test. Another thing is that using instance methods, you state in your API that a certain method cannot be used unless the constructor successfully executes. So you can make validations in your constructor and throw Exception accordingly. With static methods you don't have the equivalent of a constructor.
Ionuț G. Stan
I wasn't aware that static methods are frowned upon. I like to use them for my helpers. Oh well. Bye Bye static methods... :( Hello easier test writing :)
pferdefleisch
+1, injecting the date is easiest
orip