Search:

# CWIS Developers Documentation

- Main Page
- Classes
- Files

- Search for |

- File List
- File Members

# Axis--Date--Test.php

Go to the documentation of this file.

    00001 #!/usr/bin/php
    00002 <?PHP
    00003 # Unit tester for Axis--Date object
    00004
    00005 require_once('Axis--Date--crh.php');
    00006
    00007 # Adds leading zeros to months and days
    00008 function lz($data)
    00009 {
    00010   return (($data<10)?"0":"").$data ;
    00011 }
    00012
    00013
    00014 # FormatDate -- Generate one of the formats that Axis--Date should be able to parse
    00015 # $FmtNo -- a format number :
    00016 # ( 0) 1999-9
    00017 # ( 1) Sep-1999
    00018 # ( 2) Sep 1999
    00019 # ( 3) 199909
    00020 # ( 4) 1999-9-19
    00021 # ( 5) 9-19-1999
    00022 # ( 6) 19-9-1999
    00023 # ( 7) Sep 9 1999
    00024 # ( 8) September 9th, 1999
    00025 # ( 9) 9/19/99
    00026 # (10) 9-19-99
    00027 # (11) 19990919
    00028 # (12) 09-Sep-1999
    00029 # (13) 09 Sep 1999
    00030 # (14) c1999
    00031
    00032 function FormatDate( $FmtNo, $Year, $Month, $Day )
    00033 {
    00034   $MonthNames = array (
    00035      1 => "January",
    00036      2 => "February",
    00037      3 => "March",
    00038      4 => "April",
    00039      5 => "May",
    00040      6 => "June",
    00041      7 => "July",
    00042      8 => "August",
    00043      9 => "September",
    00044     10 => "October",
    00045     11 => "November",
    00046     12 => "December"
    00047   );
    00048
    00049   $LongMonthName  = $MonthNames[$Month];
    00050   $ShortMonthName = substr($LongMonthName,0,3);
    00051
    00052   # Suffix for long-form month numbers
    00053   $Sfx = ($Day % 10) == 1 ? "st" :
    00054          ($Day % 10) == 2 ? "nd" :
    00055          ($Day % 10) == 3 ? "rd" : "th";
    00056   $Values = array (
    00057     $Year."-".$Month,
    00058     $ShortMonthName."-".$Year,
    00059     $ShortMonthName." ".$Year,
    00060     $Year.lz($Month),
    00061     $Year."-".$Month."-".$Day,
    00062     $Month."-".$Day."-".$Year,
    00063     $Day."-".$Month."-".$Year,
    00064     $ShortMonthName." ".$Day." ".$Year,
    00065     $LongMonthName." ".$Day.$Sfx.", ".$Year,
    00066     $Month."/".$Day."/".substr($Year,2),
    00067     $Month."-".$Day."-".substr($Year,2),
    00068     $Year.lz($Month).lz($Day),
    00069     lz($Day)."-".$ShortMonthName."-".$Year,
    00070     lz($Day)." ".$ShortMonthName." ".$Year,
    00071     "c".$Year,
    00072   );
    00073
    00074   return $Values[$FmtNo];
    00075 }
    00076
    00077 $NumberOfFormats = 13;
    00078
    00079 $ExitStatus = 0 ;
    00080
    00081 # We want to test years both above and below 2000
    00082 foreach ( array(1999,2001) as $Year )
    00083 {
    00084   # Test each month, to make sure that none of the long/short name parsing is
    00085   # messed up for a particular month.
    00086   for ( $Month = 1; $Month <= 12; $Month++ )
    00087   {
    00088     # We don't need to test every day of every month
    00089     # 1-4 will have different suffixes in the long-format (1st, 2nd, etc)
    00090     # so those should be tested.
    00091     # We should be testing both single and double-digit dates
    00092     # 1-4,15 should exercise all the important parts.
    00093     foreach( array(1,2,3,4,15,21,22,23) as $Day )
    00094     {
    00095       # Index through each of the formats.
    00096       for ( $Ix=0; $Ix<=$NumberOfFormats; $Ix++)
    00097       {
    00098
    00099         # Try parsing a single date:
    00100         $TestValue = FormatDate( $Ix, $Year, $Month, $Day );
    00101         $TestDate = new Date($TestValue);
    00102         $Parsed = $TestDate->BeginDate();
    00103
    00104         # Construct the expected result
    00105         $Expected = $Year."-".lz($Month)."-".(($Ix<=3)?"00":lz($Day));
    00106         if( $Parsed != $Expected )
    00107         {
    00108           # We expect to fail on D-M-Y when D<=12, because we can't
    00109           # Distinguish that from M-D-Y
    00110           if( $Ix == 6 && $Day <= 12 )
    00111           {
    00112             print("Exfail Parsing '".$TestValue."': "
    00113                   ."Expected '".$Expected."' but got '".$Parsed."'\n");
    00114           }
    00115           else
    00116           {
    00117             $ExitStatus = 1 ;
    00118             print("FAIL Parsing '".$TestValue."': "
    00119                   ."Expected '".$Expected."' but got '".$Parsed."'\n");
    00120           }
    00121         }
    00122
    00123         # Now for a date range
    00124         # Try an end date in each possible format
    00125         # We'll go from the starting date till 2 years later.
    00126         for( $Ix2 = 0; $Ix2<=$NumberOfFormats; $Ix2++ )
    00127         {
    00128           $TestValue = FormatDate( $Ix,  $Year,   $Month, $Day ) . " - " .
    00129                        FormatDate( $Ix2, $Year+2, $Month, $Day );
    00130           $TestDate = new Date($TestValue);
    00131           $Parsed = $TestDate->BeginDate()." - ".$TestDate->EndDate();
    00132
    00133           $Expected =     $Year."-".lz($Month)."-".(($Ix <=3)?"00":lz($Day))." - ".
    00134                       ($Year+2)."-".lz($Month)."-".(($Ix2<=3)?"00":lz($Day));
    00135           if( $Parsed != $Expected )
    00136           {
    00137             if( ($Ix == 6 || $Ix2 == 6 )  && $Day <= 12 )
    00138             {
    00139                 # Again, expected failures on D-M-Y when D<=12
    00140                 print("Exfail Parsing '".$TestValue."': "
    00141                       ."Expected '".$Expected."' but got '".$Parsed."'\n");
    00142             }
    00143             else
    00144             {
    00145                $ExitStatus = 1 ;
    00146                 print("FAIL Parsing '".$TestValue."': "
    00147                       ."Expected '".$Expected."' but got '".$Parsed."'\n");
    00148             }
    00149           }
    00150         }
    00151       }
    00152     }
    00153   }
    00154 }
    00155
    00156 # If any tests failed, exit with an error.
    00157 exit ($ExitStatus) ;
    00158 ?>

Copyright 2009 Internet Scout