CWIS Developer Documentation
Axis--Date--Test.php
Go to the documentation of this file.
1 #!/usr/bin/php
2 <?PHP
3 # Unit tester for Axis--Date object
4 
5 require_once('Axis--Date--crh.php');
6 
7 # Adds leading zeros to months and days
8 function lz($data)
9 {
10  return (($data<10)?"0":"").$data ;
11 }
12 
13 
14 # FormatDate -- Generate one of the formats that Axis--Date should be able to parse
15 # $FmtNo -- a format number :
16 # ( 0) 1999-9
17 # ( 1) Sep-1999
18 # ( 2) Sep 1999
19 # ( 3) 199909
20 # ( 4) 1999-9-19
21 # ( 5) 9-19-1999
22 # ( 6) 19-9-1999
23 # ( 7) Sep 9 1999
24 # ( 8) September 9th, 1999
25 # ( 9) 9/19/99
26 # (10) 9-19-99
27 # (11) 19990919
28 # (12) 09-Sep-1999
29 # (13) 09 Sep 1999
30 # (14) c1999
31 
32 function FormatDate( $FmtNo, $Year, $Month, $Day )
33 {
34  $MonthNames = array (
35  1 => "January",
36  2 => "February",
37  3 => "March",
38  4 => "April",
39  5 => "May",
40  6 => "June",
41  7 => "July",
42  8 => "August",
43  9 => "September",
44  10 => "October",
45  11 => "November",
46  12 => "December"
47  );
48 
49  $LongMonthName = $MonthNames[$Month];
50  $ShortMonthName = substr($LongMonthName,0,3);
51 
52  # Suffix for long-form month numbers
53  $Sfx = ($Day % 10) == 1 ? "st" :
54  ($Day % 10) == 2 ? "nd" :
55  ($Day % 10) == 3 ? "rd" : "th";
56  $Values = array (
57  $Year."-".$Month,
58  $ShortMonthName."-".$Year,
59  $ShortMonthName." ".$Year,
60  $Year.lz($Month),
61  $Year."-".$Month."-".$Day,
62  $Month."-".$Day."-".$Year,
63  $Day."-".$Month."-".$Year,
64  $ShortMonthName." ".$Day." ".$Year,
65  $LongMonthName." ".$Day.$Sfx.", ".$Year,
66  $Month."/".$Day."/".substr($Year,2),
67  $Month."-".$Day."-".substr($Year,2),
68  $Year.lz($Month).lz($Day),
69  lz($Day)."-".$ShortMonthName."-".$Year,
70  lz($Day)." ".$ShortMonthName." ".$Year,
71  "c".$Year,
72  );
73 
74  return $Values[$FmtNo];
75 }
76 
78 
80 
81 # We want to test years both above and below 2000
82 foreach ( array(1999,2001) as $Year )
83 {
84  # Test each month, to make sure that none of the long/short name parsing is
85  # messed up for a particular month.
86  for ( $Month = 1; $Month <= 12; $Month++ )
87  {
88  # We don't need to test every day of every month
89  # 1-4 will have different suffixes in the long-format (1st, 2nd, etc)
90  # so those should be tested.
91  # We should be testing both single and double-digit dates
92  # 1-4,15 should exercise all the important parts.
93  foreach( array(1,2,3,4,15,21,22,23) as $Day )
94  {
95  # Index through each of the formats.
96  for ( $Ix=0; $Ix<=$NumberOfFormats; $Ix++)
97  {
98 
99  # Try parsing a single date:
100  $TestValue = FormatDate( $Ix, $Year, $Month, $Day );
101  $TestDate = new Date($TestValue);
102  $Parsed = $TestDate->BeginDate();
103 
104  # Construct the expected result
105  $Expected = $Year."-".lz($Month)."-".(($Ix<=3)?"00":lz($Day));
106  if( $Parsed != $Expected )
107  {
108  # We expect to fail on D-M-Y when D<=12, because we can't
109  # Distinguish that from M-D-Y
110  if( $Ix == 6 && $Day <= 12 )
111  {
112  print("Exfail Parsing '".$TestValue."': "
113  ."Expected '".$Expected."' but got '".$Parsed."'\n");
114  }
115  else
116  {
117  $ExitStatus = 1 ;
118  print("FAIL Parsing '".$TestValue."': "
119  ."Expected '".$Expected."' but got '".$Parsed."'\n");
120  }
121  }
122 
123  # Now for a date range
124  # Try an end date in each possible format
125  # We'll go from the starting date till 2 years later.
126  for( $Ix2 = 0; $Ix2<=$NumberOfFormats; $Ix2++ )
127  {
128  $TestValue = FormatDate( $Ix, $Year, $Month, $Day ) . " - " .
129  FormatDate( $Ix2, $Year+2, $Month, $Day );
130  $TestDate = new Date($TestValue);
131  $Parsed = $TestDate->BeginDate()." - ".$TestDate->EndDate();
132 
133  $Expected = $Year."-".lz($Month)."-".(($Ix <=3)?"00":lz($Day))." - ".
134  ($Year+2)."-".lz($Month)."-".(($Ix2<=3)?"00":lz($Day));
135  if( $Parsed != $Expected )
136  {
137  if( ($Ix == 6 || $Ix2 == 6 ) && $Day <= 12 )
138  {
139  # Again, expected failures on D-M-Y when D<=12
140  print("Exfail Parsing '".$TestValue."': "
141  ."Expected '".$Expected."' but got '".$Parsed."'\n");
142  }
143  else
144  {
145  $ExitStatus = 1 ;
146  print("FAIL Parsing '".$TestValue."': "
147  ."Expected '".$Expected."' but got '".$Parsed."'\n");
148  }
149  }
150  }
151  }
152  }
153  }
154 }
155 
156 # If any tests failed, exit with an error.
157 exit ($ExitStatus) ;
158 ?>
lz($data)
$ExitStatus
Definition: Date.php:18
$NumberOfFormats
FormatDate($FmtNo, $Year, $Month, $Day)