We can use SimpleTest to write program in TDD style.
0. Download SimpleTest.
1. Add the unit testing and report file.
2. Subclass the UnitTestCase
3. Write the function to do the unit testing.
4. Run the test.
Example:
<?php
require_once('simpletest/unit_tester.php');
require_once('simpletest/reporter.php');
require_once "Location.php"; // class to test
class MyTestingClass extends UnitTestCase
{
/*
* Test distance calculation function in Location class.
*/
function TestCalculateDistance()
{
$point1 = array('x'=>0, 'y'=>0);
$point2 = array('x'=>10, 'y'=>10);
$distance = number_format(14.142135623731, 2, '.', '');
$location = new Location();
$retDistance = $location->calculateDistance($point1, $point2); // Function in Location class
$retDistance = number_format($retDistance, 2, '.', '');
$this->assertEqual($distance, $retDistance); // return true if distance is calculated correctly
}
}
$test = new TestLocation();
$test->run(new HtmlReporter()); // run the test
?>