ovo je kod od one moje klase
PHP kôd:
<?php
class MySQL_date
{
// site::http://boonedocks.net/mike/archives/137-Creating-a-Date-Range-Array-with-PHP.html
public static function createDateRangeArray($strDateFrom, $strDateTo)
{
// takes two dates formatted as YYYY-MM-DD and creates an
// inclusive array of the dates between the from and to dates.
// could test validity of dates here but I'm already doing
// that in the main script
$aryRange=array();
$iDateFrom=mktime(1,0,0,substr($strDateFrom,5,2), substr($strDateFrom,8,2),substr($strDateFrom,0,4));
$iDateTo=mktime(1,0,0,substr($strDateTo,5,2), substr($strDateTo,8,2),substr($strDateTo,0,4));
if ($iDateTo>=$iDateFrom) {
array_push($aryRange,date('Y-m-d',$iDateFrom)); // first entry
while ($iDateFrom<$iDateTo) {
$iDateFrom+=86400; // add 24 hours
array_push($aryRange,date('Y-m-d',$iDateFrom));
}
}
return $aryRange;
}
// convert string in format dd.mm.YYYY to MySQL format http://dev.mysql.com/doc/refman/5.0/en/datetime.html YYYY-MM-DD
// example 18.04.2008 to 2008-04-18
public static function ddmmYYYYtoYYYYMMDD($date)
{
$parts = explode(".", $date);
$new_date = $parts[2] . '-' . $parts[1] . '-' . $parts[0];
return $new_date;
}
}
?>
A evo koda za unit test, koristio sam
http://simpletest.sourceforge.net/en...mentation.html
PHP kôd:
<?php
require_once('../simpletest/autorun.php');
require_once('../class/MySQL_date.php');
class TestOf__MySQL_date extends UnitTestCase
{
function test__ddmmYYYYtoYYYYMMDD()
{
$this->assertEqual( MySQL_date::ddmmYYYYtoYYYYMMDD('16.06.2008'), '2008-06-16');
$this->assertEqual( MySQL_date::ddmmYYYYtoYYYYMMDD('01.01.2009'), '2009-01-01');
}
function test__createDateRangeArray()
{
$this->assertEqual( MySQL_date::createDateRangeArray('2008-06-16', '2008-06-16'), array( '2008-06-16' ) );
$this->assertEqual( MySQL_date::createDateRangeArray('2008-06-16', '2008-06-17'), array( '2008-06-16', '2008-06-17' ) );
$result = MySQL_date::createDateRangeArray('2008-06-30', '2008-07-02');
$expectedResult = array( '2008-06-30', '2008-07-01', '2008-07-02' );
if( !$this->assertEqual( $result, $expectedResult ) )
{
$this->dump( $result );
}
$result = MySQL_date::createDateRangeArray('2008-12-30', '2009-01-02');
$expectedResult = array( '2008-12-30', '2008-12-31', '2009-01-01', '2009-01-02' );
if( !$this->assertEqual( $result, $expectedResult ) )
{
$this->dump( $result );
}
$result = MySQL_date::createDateRangeArray('2008-02-27', '2008-03-02');
$expectedResult = array( '2008-02-27', '2008-02-28', '2008-02-29', '2008-03-01' ,'2008-03-02' );
if( !$this->assertEqual( $result, $expectedResult ) )
{
$this->dump( $result );
}
$result = MySQL_date::createDateRangeArray('2009-02-27', '2009-03-02');
$expectedResult = array( '2009-02-27', '2009-02-28', '2009-03-01' ,'2009-03-02' );
if( !$this->assertEqual( $result, $expectedResult ) )
{
$this->dump( $result );
}
}
}
?>
Mogao sam ja to i rucno testirati ali bih sigurno izgubio isto toliko vremena koliko mi je trebalo da napišem taj test(oko 30min i napominjem da je ovo prvi unit test koji sam napisao za PHP, jer inace mislim da bi ovo osoba mogla učiniti za 5-10minuti) i ne bih mogao ponovno pokrenuti taj test kada god poželim.
Po meni je itekako normalo da svaki cod(program, skripta) ima i neki (kod, program) koji ga može automatski testirati.
To izgleda da se možda ne isplati kod malih projektica ili programćica(istina ne pisem nikakav test za linux skripte od par desetaka linija) ali ako ih ikada zatreba israviti onda osoba shvati da je bolje bilo da ih je imala.
Ima ja jedno pitanje:
Sta se pod PHP-om(webom) može koristiti za testiranje iznad unit testa, mislim na testiranje cijele aplikacije...