1 | <?php |
---|
2 | require_once("../htdocs/always.php"); |
---|
3 | require_once("RRule.php"); |
---|
4 | |
---|
5 | header("Content-type: text/plain"); |
---|
6 | |
---|
7 | echo <<<EOTXT |
---|
8 | Testing the RRule Library |
---|
9 | |
---|
10 | EOTXT; |
---|
11 | |
---|
12 | class RRuleTest { |
---|
13 | var $dtstart; |
---|
14 | var $recur; |
---|
15 | var $description; |
---|
16 | var $result_description; |
---|
17 | |
---|
18 | function RRuleTest( $description, $start, $recur, $result_description = null ) { |
---|
19 | $this->description = $description; |
---|
20 | $this->dtstart = $start; |
---|
21 | $this->recur = $recur; |
---|
22 | $this->result_description = $result_description; |
---|
23 | } |
---|
24 | |
---|
25 | function PHPTest() { |
---|
26 | $result = ''; |
---|
27 | $rule = new RRule( new iCalDate($this->dtstart), $this->recur ); |
---|
28 | $i = 0; |
---|
29 | do { |
---|
30 | $date = $rule->GetNext(); |
---|
31 | if ( isset($date) ) { |
---|
32 | if ( ($i++ % 4) == 0 ) $result .= "\n"; |
---|
33 | $result .= " " . $date->Render(); |
---|
34 | } |
---|
35 | } |
---|
36 | while( isset($date) && $i < 30 ); |
---|
37 | return $result; |
---|
38 | } |
---|
39 | |
---|
40 | function SQLTest() { |
---|
41 | $result = ''; |
---|
42 | $sql = "SELECT event_instances::timestamp AS event_date FROM event_instances(?,?) LIMIT 30;"; |
---|
43 | $qry = new AwlQuery($sql, $this->dtstart, $this->recur); |
---|
44 | // printf( "%s\n", $qry->querystring); |
---|
45 | if ( $qry->Exec("test") && $qry->rows > 0 ) { |
---|
46 | $i = 0; |
---|
47 | while( $row = $qry->Fetch() ) { |
---|
48 | if ( ($i++ % 4) == 0 ) $result .= "\n"; |
---|
49 | $result .= " " . $row->event_date; |
---|
50 | } |
---|
51 | } |
---|
52 | return $result; |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | |
---|
57 | $tests = array( |
---|
58 | new RRuleTest( "Daily for 7 days", "20061103T073000", "RRULE:FREQ=DAILY;COUNT=7" ) |
---|
59 | , new RRuleTest( "Weekly for 26 weeks", "20061102T100000", "RRULE:FREQ=WEEKLY;COUNT=26;INTERVAL=1;BYDAY=TH" ) |
---|
60 | , new RRuleTest( "Fortnightly for 28 events", "20061103T160000", "RRULE:FREQ=WEEKLY;INTERVAL=2;UNTIL=20071122T235900" ) |
---|
61 | , new RRuleTest( "3/wk for 5 weeks", "20081101T160000", "RRULE:FREQ=WEEKLY;COUNT=15;INTERVAL=1;BYDAY=MO,WE,FR" ) |
---|
62 | , new RRuleTest( "Monthly forever", "20061104T073000", "RRULE:FREQ=MONTHLY" ) |
---|
63 | , new RRuleTest( "Monthly, on the 1st monday, 2nd wednesday, 3rd friday and last sunday, forever", "20061117T073000", "RRULE:FREQ=MONTHLY;BYDAY=1MO,2WE,3FR,-1SU" ) |
---|
64 | , new RRuleTest( "The last working day of each month", "20061107T113000", "RRULE:FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=-1" ) |
---|
65 | , new RRuleTest( "Every working day", "20081020T103000", "RRULE:FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR" ) |
---|
66 | , new RRuleTest( "Every working day", "20081020T110000", "RRULE:FREQ=DAILY;INTERVAL=1;BYDAY=MO,TU,WE,TH,FR" ) |
---|
67 | , new RRuleTest( "1st Tuesday, 2nd Wednesday, 3rd Thursday & 4th Friday, every March, June, September, October and December", "20081001T133000", "RRULE:FREQ=MONTHLY;INTERVAL=1;BYDAY=1TU,2WE,3TH,4FR;BYMONTH=3,6,9,10,12" ) |
---|
68 | , new RRuleTest( "Every tuesday and friday", "20081017T084500", "RRULE:FREQ=MONTHLY;INTERVAL=1;BYDAY=TU,FR" ) |
---|
69 | , new RRuleTest( "Every tuesday and friday", "20081017T084500", "RRULE:FREQ=WEEKLY;INTERVAL=1;BYDAY=TU,FR" ) |
---|
70 | , new RRuleTest( "Every tuesday and friday", "20081017T084500", "RRULE:FREQ=DAILY;INTERVAL=1;BYDAY=TU,FR" ) |
---|
71 | , new RRuleTest( "Time zone 1", "19700315T030000", "FREQ=YEARLY;INTERVAL=1;BYDAY=3SU;BYMONTH=3" ) |
---|
72 | , new RRuleTest( "Time zone 2", "19700927T020000", "FREQ=YEARLY;INTERVAL=1;BYDAY=-1SU;BYMONTH=9" ) |
---|
73 | , new RRuleTest( "Time zone 3", "19810329T030000", "FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU" ) |
---|
74 | , new RRuleTest( "Time zone 4", "20000404T020000", "FREQ=YEARLY;BYDAY=1SU;BYMONTH=4" ) |
---|
75 | ); |
---|
76 | |
---|
77 | foreach( $tests AS $k => $test ) { |
---|
78 | echo "=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=\n"; |
---|
79 | echo "$test->dtstart - $test->recur\n"; |
---|
80 | echo "$test->description\n"; |
---|
81 | $php_result = $test->PHPTest(); |
---|
82 | /* |
---|
83 | $sql_result = $test->SQLTest(); |
---|
84 | if ( $php_result == $sql_result ) { |
---|
85 | echo "PHP & SQL results are identical :-)\n"; |
---|
86 | } |
---|
87 | else { |
---|
88 | echo "PHP & SQL results differ :-(\n"; |
---|
89 | } |
---|
90 | */ |
---|
91 | echo "PHP Result:\n$php_result\n\n"; |
---|
92 | // echo "SQL Result:\n$sql_result\n\n"; // Still under development |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | exit(0); |
---|