From 8de2807d1702a7fef161614a865f273d29e9026a Mon Sep 17 00:00:00 2001 From: rlanvin Date: Wed, 1 Jul 2015 12:23:39 +0300 Subject: [PATCH] More tests, code cleanup --- README.md | 14 +-- composer.json | 2 +- src/RRule.php | 50 +------- tests/RRuleTest.php | 281 ++++++++++++++++++++++++++++++++------------ 4 files changed, 217 insertions(+), 130 deletions(-) diff --git a/README.md b/README.md index 3c6c478..6aaea15 100755 --- a/README.md +++ b/README.md @@ -38,9 +38,6 @@ Complete doc is available in [the wiki](https://github.com/rlanvin/php-rrule/wik ## Installation -This is still a work in progress, use at your own risk! -In particular, HOURLY, MINUTELY and SECONDLY frequencies are not implemented. - The recommended way is to install the lib [through Composer](http://getcomposer.org/). Just add this to your `composer.json` file: @@ -64,24 +61,21 @@ require 'vendor/autoload.php'; ### Alternative method -Since it's a no-nonsense implementation, there is only one class. -So you can just download `src/RRule.php` and require it. +You can just download `src/RRule.php` and require it. ## Note I started this library because I wasn't happy with the existing implementations -in PHP. The ones I tested were slow and/or had a very awkward/verbose API that -I didn't like to use. They were also all missing a generator/iterator, which I -think is key. So I thought it would be a good learning project to port the +in PHP, so I thought it would be a good learning project to port the python-dateutil rrule implementation into PHP. -The Python lib was a bit difficult to understand because the algorithms (very smart by the way), +The Python lib was a bit difficult to understand because the algorithms are not commented and the variables are very opaque (I'm looking at you `lno1wkst`). I tried to comment and explain as much of the algorithm as possible in this PHP port, so feel free to check the code if you're interested. The lib differs from the python version in various aspects, notably in the -respect of the RFC. This version is strictier and will not accept many +respect of the RFC. This version is a bit strictier and will not accept many non-compliant combinations of rule parts, that the python version otherwise accepts. There are also some additional features in this version. diff --git a/composer.json b/composer.json index e766180..bbefddd 100755 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "rlanvin/php-rrule", "type": "library", - "description": "Lightweight RRule implementation (RFC 5545)", + "description": "Lightweight and fast recurrence rules for PHP (RFC 5545)", "keywords": ["rrule","ical","recurrence","recurring","date"], "homepage": "https://github.com/rlanvin/php-rrule", "license": "MIT", diff --git a/src/RRule.php b/src/RRule.php index d94c6ee..7855812 100755 --- a/src/RRule.php +++ b/src/RRule.php @@ -66,20 +66,7 @@ function is_leap_year($year) } /** - * Calculate the Greatest Common Divisor of a and b. - * Unless b==0, the result will have the same sign as b (so that when - * b is divided by it, the result comes out positive). - */ -function gcd($a, $b) -{ - while ($b) { - list($a, $b) = array($b, $a % $b); - } - return $a; -} - -/** - * Implementation of RRULE as defined by RFC 5545. + * Implementation of RRULE as defined by RFC 5545 (iCalendar). * Heavily based on python-dateutil/rrule * * Some useful terms to understand the algorithms and variables naming: @@ -430,9 +417,6 @@ class RRule implements \Iterator, \ArrayAccess } } -// now for the time options -// this gets more complicated - if ( not_empty($parts['BYHOUR']) ) { if ( ! is_array($parts['BYHOUR']) ) { $parts['BYHOUR'] = explode(',',$parts['BYHOUR']); @@ -446,9 +430,7 @@ class RRule implements \Iterator, \ArrayAccess $this->byhour[] = (int) $value; } - if ( $this->freq === self::HOURLY ) { - // do something (__construct_byset) ? - } + sort($this->byhour); } elseif ( $this->freq < self::HOURLY ) { $this->byhour = array((int) $this->dtstart->format('G')); @@ -466,10 +448,7 @@ class RRule implements \Iterator, \ArrayAccess } $this->byminute[] = (int) $value; } - - if ( $this->freq == self::MINUTELY ) { - // do something - } + sort($this->byminute); } elseif ( $this->freq < self::MINUTELY ) { $this->byminute = array((int) $this->dtstart->format('i')); @@ -490,10 +469,7 @@ class RRule implements \Iterator, \ArrayAccess } $this->bysecond[] = (int) $value; } - - if ( $this->freq == self::SECONDLY ) { - // do something - } + sort($this->bysecond); } elseif ( $this->freq < self::SECONDLY ) { $this->bysecond = array((int) $this->dtstart->format('s')); @@ -509,13 +485,10 @@ class RRule implements \Iterator, \ArrayAccess foreach ( $this->byhour as $hour ) { foreach ( $this->byminute as $minute ) { foreach ( $this->bysecond as $second ) { - // fixme another format? $this->timeset[] = array($hour,$minute,$second); } } } - // FIXME sort ?? - // sort($this->timeset); } } @@ -1029,7 +1002,6 @@ class RRule implements \Iterator, \ArrayAccess */ protected function getTimeSet($hour, $minute, $second) { - // echo "getTimeSet($hour,$minute,$second)\n"; switch ( $this->freq ) { case self::HOURLY: $set = array(); @@ -1208,9 +1180,7 @@ class RRule implements \Iterator, \ArrayAccess // calculate the current set $dayset = $this->getDaySet($year, $month, $day, $masks); -// echo"\tWorking with $year-$month-$day set=".json_encode($dayset)."\n"; -// print_r(json_encode($masks)); -// fgets(STDIN); + $filtered_set = array(); foreach ( $dayset as $yearday ) { @@ -1251,7 +1221,6 @@ class RRule implements \Iterator, \ArrayAccess $filtered_set[] = $yearday; } -// echo "\tFiltered set (before BYSETPOS)=".json_encode($filtered_set)."\n"; $dayset = $filtered_set; @@ -1260,7 +1229,6 @@ class RRule implements \Iterator, \ArrayAccess if ( $this->bysetpos && $timeset ) { $filtered_set = array(); foreach ( $this->bysetpos as $pos ) { - // echo "pos = $pos => "; $n = count($timeset); if ( $pos < 0 ) { $pos = $n * count($dayset) + $pos; @@ -1268,14 +1236,9 @@ class RRule implements \Iterator, \ArrayAccess else { $pos = $pos - 1; } -// echo "$pos => "; + $div = (int) ($pos / $n); // daypos $mod = $pos % $n; // timepos - // echo "array index $div / $mod\n"; - // echo "div = $div, mod = $mod\n"; - // echo "dayset[$div] = ",$dayset[$div]," timeset[$mod] = ",json_encode($timeset[$mod]),"\n"; - // fgets(STDIN); - if ( isset($dayset[$div]) && isset($timeset[$mod]) ) { $yearday = $dayset[$div]; $time = $timeset[$mod]; @@ -1294,7 +1257,6 @@ class RRule implements \Iterator, \ArrayAccess sort($filtered_set); $dayset = $filtered_set; } -// echo "\tFiltered set (after BYSETPOS)=".json_encode($filtered_set)."\n"; } // 2. loop, generate a valid date, and return the result (fake "yield") diff --git a/tests/RRuleTest.php b/tests/RRuleTest.php index c147e65..17842ea 100755 --- a/tests/RRuleTest.php +++ b/tests/RRuleTest.php @@ -114,7 +114,7 @@ class RRuleTest extends PHPUnit_Framework_TestCase // todo bysetpos - array(array('BYHOUR' => array(6, 18)),array( + array(array('BYHOUR' => '6,18'),array( date_create('1997-09-02 06:00:00'), date_create('1997-09-02 18:00:00'), date_create('1998-09-02 06:00:00'))), @@ -126,11 +126,11 @@ class RRuleTest extends PHPUnit_Framework_TestCase date_create('1997-09-02 00:00:10'), date_create('1997-09-02 00:00:20'), date_create('1998-09-02 00:00:10'))), - array(array('BYHOUR' => array(6, 18), 'BYMINUTE' => array(15, 30)), array( + array(array('BYHOUR' => '6,18', 'BYMINUTE' => array(15, 30)), array( date_create('1997-09-02 06:15:00'), date_create('1997-09-02 06:30:00'), date_create('1997-09-02 18:15:00'))), - array(array('BYHOUR' => array(6, 18), 'BYSECOND' => array(10, 20)), array( + array(array('BYHOUR' => '6,18', 'BYSECOND' => array(10, 20)), array( date_create('1997-09-02 06:00:10'), date_create('1997-09-02 06:00:20'), date_create('1997-09-02 18:00:10'))), @@ -138,11 +138,11 @@ class RRuleTest extends PHPUnit_Framework_TestCase date_create('1997-09-02 00:15:10'), date_create('1997-09-02 00:15:20'), date_create('1997-09-02 00:30:10'))), - array(array('BYHOUR'=>array(6, 18),'BYMINUTE'=>array(15, 30),'BYSECOND'=>array(10, 20)),array( + array(array('BYHOUR'=>'6,18','BYMINUTE'=>array(15, 30),'BYSECOND'=>array(10, 20)),array( date_create('1997-09-02 06:15:10'), date_create('1997-09-02 06:15:20'), date_create('1997-09-02 06:30:10'))), - array(array('BYMONTHDAY'=>15,'BYHOUR'=>array(6, 18),'BYSETPOS'=>array(3, -3)),array( + array(array('BYMONTHDAY'=>15,'BYHOUR'=>'6,18','BYSETPOS'=>array(3, -3)),array( date_create('1997-11-15 18:00:00'), date_create('1998-02-15 06:00:00'), date_create('1998-11-15 18:00:00'))) @@ -178,11 +178,11 @@ class RRuleTest extends PHPUnit_Framework_TestCase date_create('1997-09-02'),date_create('1997-11-02'),date_create('1998-01-02'))), array(array('INTERVAL'=>18),array( date_create('1997-09-02'),date_create('1999-03-02'),date_create('2000-09-02'))), - array(array('BYMONTH' => array(1, 3)),array( + array(array('BYMONTH' => '1,3'),array( date_create('1998-01-02'),date_create('1998-03-02'),date_create('1999-01-02'))), - array(array('BYMONTHDAY' => array(1, 3)),array( + array(array('BYMONTHDAY' => '1,3'),array( date_create('1997-09-03'),date_create('1997-10-01'),date_create('1997-10-03'))), - array(array('BYMONTHDAY' => array(5, 7), 'BYMONTH' => array(1, 3)), array( + array(array('BYMONTHDAY' => array(5, 7), 'BYMONTH' => '1,3'), array( date_create('1998-01-05'), date_create('1998-01-07'), date_create('1998-03-05'))), array(array('BYDAY' => array('TU', 'TH')), array( date_create('1997-09-02'),date_create('1997-09-04'),date_create('1997-09-09'))), @@ -193,15 +193,15 @@ class RRuleTest extends PHPUnit_Framework_TestCase date_create('1997-09-02'),date_create('1997-09-25'),date_create('1997-10-07'))), array(array('BYDAY' => '3TU,-3TH'),array( date_create('1997-09-11'),date_create('1997-09-16'),date_create('1997-10-16'))), - array(array('BYDAY' => 'TU,TH', 'BYMONTH' => array(1, 3)),array( + array(array('BYDAY' => 'TU,TH', 'BYMONTH' => '1,3'),array( date_create('1998-01-01'),date_create('1998-01-06'),date_create('1998-01-08'))), - array(array('BYMONTH' => array(1, 3), 'BYDAY' => '1TU, -1TH'),array( + array(array('BYMONTH' => '1,3', 'BYDAY' => '1TU, -1TH'),array( date_create('1998-01-06'),date_create('1998-01-29'),date_create('1998-03-03'))), - array(array('BYMONTH' => array(1, 3), 'BYDAY' => '3TU, -3TH'),array( + array(array('BYMONTH' => '1,3', 'BYDAY' => '3TU, -3TH'),array( date_create('1998-01-15'),date_create('1998-01-20'),date_create('1998-03-12'))), - array(array('BYMONTHDAY' => array(1, 3), 'BYDAY' => array('TU', 'TH')), array( + array(array('BYMONTHDAY' => '1,3', 'BYDAY' => array('TU', 'TH')), array( date_create('1998-01-01'),date_create('1998-02-03'),date_create('1998-03-03'))), - array(array('BYMONTH' => array(1, 3), 'BYMONTHDAY' => array(1, 3), 'BYDAY' => array('TU', 'TH')),array( + array(array('BYMONTH' => '1,3', 'BYMONTHDAY' => '1,3', 'BYDAY' => array('TU', 'TH')),array( date_create('1998-01-01'),date_create('1998-03-03'),date_create('2001-03-01'))), // last workday of the month @@ -210,26 +210,26 @@ class RRuleTest extends PHPUnit_Framework_TestCase date_create('1997-10-31'), date_create('1997-11-28'))), - array(array('BYHOUR'=> array(6, 18)),array( + array(array('BYHOUR'=> '6,18'),array( date_create('1997-09-02 06:00:00'),date_create('1997-09-02 18:00:00'),date_create('1997-10-02 06:00:00'))), - array(array('BYMINUTE'=> array(6, 18)),array( + array(array('BYMINUTE'=> '6,18'),array( date_create('1997-09-02 00:06:00'),date_create('1997-09-02 00:18:00'),date_create('1997-10-02 00:06:00'))), - array(array('BYSECOND' => array(6, 18)),array( + array(array('BYSECOND' => '6,18'),array( date_create('1997-09-02 00:00:06'),date_create('1997-09-02 00:00:18'),date_create('1997-10-02 00:00:06'))), - array(array('BYHOUR'=>array(6, 18),'BYMINUTE'=>array(6, 18)),array( + array(array('BYHOUR'=>'6,18','BYMINUTE'=>'6,18'),array( date_create('1997-09-02 06:06:00'),date_create('1997-09-02 06:18:00'),date_create('1997-09-02 18:06:00'))), - array(array('BYHOUR'=>array(6, 18),'BYSECOND'=>array(6, 18)),array( + array(array('BYHOUR'=>'6,18','BYSECOND'=>'6,18'),array( date_create('1997-09-02 06:00:06'),date_create('1997-09-02 06:00:18'),date_create('1997-09-02 18:00:06'))), - array(array('BYMINUTE'=>array(6, 18),'BYSECOND'=>array(6, 18)),array( + array(array('BYMINUTE'=>'6,18','BYSECOND'=>'6,18'),array( date_create('1997-09-02 00:06:06'),date_create('1997-09-02 00:06:18'),date_create('1997-09-02 00:18:06'))), - array(array('BYHOUR'=>array(6, 18),'BYMINUTE'=>array(6, 18),'BYSECOND'=>array(6, 18)),array( + array(array('BYHOUR'=>'6,18','BYMINUTE'=>'6,18','BYSECOND'=>'6,18'),array( date_create('1997-09-02 06:06:06'),date_create('1997-09-02 06:06:18'),date_create('1997-09-02 06:18:06'))), - array(array('BYMONTHDAY'=>array(13, 17),'BYHOUR'=>array(6, 18),'BYSETPOS'=>array(3, -3)),array( + array(array('BYMONTHDAY'=>array(13, 17),'BYHOUR'=>'6,18','BYSETPOS'=>array(3, -3)),array( date_create('1997-09-13 18:00'),date_create('1997-09-17 06:00'),date_create('1997-10-13 18:00'))), // avoid duplicates - array(array('BYMONTHDAY'=>array(13, 17),'BYHOUR'=>array(6, 18),'BYSETPOS'=>array(3, 3, -3)),array( + array(array('BYMONTHDAY'=>array(13, 17),'BYHOUR'=>'6,18','BYSETPOS'=>array(3, 3, -3)),array( date_create('1997-09-13 18:00'),date_create('1997-09-17 06:00'),date_create('1997-10-13 18:00'))), - array(array('BYMONTHDAY'=>array(13, 17),'BYHOUR'=>array(6, 18),'BYSETPOS'=>array(4, -1)),array( + array(array('BYMONTHDAY'=>array(13, 17),'BYHOUR'=>'6,18','BYSETPOS'=>array(4, -1)),array( date_create('1997-09-17 18:00'),date_create('1997-10-17 18:00'),date_create('1997-11-17 18:00'))) ); } @@ -262,7 +262,7 @@ class RRuleTest extends PHPUnit_Framework_TestCase date_create('1997-09-02'),date_create('1997-09-16'),date_create('1997-09-30'))), array(array('interval'=>20),array( date_create('1997-09-02'),date_create('1998-01-20'),date_create('1998-06-09'))), - array(array('bymonth'=>array(1, 3)),array( + array(array('bymonth'=>'1,3'),array( date_create('1998-01-06'),date_create('1998-01-13'),date_create('1998-01-20'))), array(array('byday'=> array('TU', 'TH')),array( date_create('1997-09-02'), date_create('1997-09-04'), date_create('1997-09-09'))), @@ -270,25 +270,33 @@ class RRuleTest extends PHPUnit_Framework_TestCase # This test is interesting, because it crosses the year # boundary in a weekly period to find day '1' as a # valid recurrence. - array(array('bymonth'=>array(1, 3),'byday'=>array('TU', 'TH')),array( + array(array('bymonth'=>'1,3','byday'=>array('TU', 'TH')),array( date_create('1998-01-01'), date_create('1998-01-06'), date_create('1998-01-08'))), - array(array('byhour'=>array(6, 18)),array( + array(array('byhour'=>'6,18'),array( date_create('1997-09-02 06:00:00'),date_create('1997-09-02 18:00:00'),date_create('1997-09-09 06:00:00'))), - array(array('byminute'=>array(6, 18)),array( + array(array('byminute'=>'6,18'),array( date_create('1997-09-02 00:06:00'),date_create('1997-09-02 00:18:00'),date_create('1997-09-09 00:06:00'))), - array(array('bysecond'=> array(6, 18)),array( + array(array('bysecond'=> '6,18'),array( date_create('1997-09-02 00:00:06'),date_create('1997-09-02 00:00:18'),date_create('1997-09-09 00:00:06'))), - array(array('byhour'=> array(6, 18),'byminute'=>array(6, 18)),array( + array(array('byhour'=> '6,18','byminute'=>'6,18'),array( date_create('1997-09-02 06:06:00'),date_create('1997-09-02 06:18:00'),date_create('1997-09-02 18:06:00'))), - // array(array('byhour'=>array(6, 18),'bysecond'=>array(6, 18)),array( - // date_create('1997-09-02'),date_create('1997-09-02'),date_create('1997-09-09'))), - // array(array('byminute'=>array(6, 18),'bysecond'=>array(6, 18)),array( - // date_create('1997-09-02'),date_create('1997-09-02'),date_create('1997-09-02'))), - // array(array('byhour'=>array(6, 18),'byminute'=>array(6, 18),'bysecond'=>array(6, 18)),array( - // date_create('1997-09-02'),date_create('1997-09-02'),date_create('1997-09-02'))), - // array(array('byday'=>array('TU', 'TH'),'byhour'=>array(6, 18),'bysetpos'=>array(3, -3)),array( - // date_create('1997-09-02'),date_create('1997-09-04'),date_create('1997-09-09'))) + array(array('byhour'=>'6,18','bysecond'=>'6,18', 'dtstart' => '1997-09-02 09:00:00'),array( + date_create('1997-09-02 18:00:06'), + date_create('1997-09-02 18:00:18'), + date_create('1997-09-09 06:00:06'))), + array(array('byminute'=>'6,18','bysecond'=>'6,18', 'dtstart' => '1997-09-02 09:00:00'),array( + date_create('1997-09-02 09:06:06'), + date_create('1997-09-02 09:06:18'), + date_create('1997-09-02 09:18:06'))), + array(array('byhour'=>'6,18','byminute'=>'6,18','bysecond'=>'6,18', 'dtstart' => '1997-09-02 09:00:00'),array( + date_create('1997-09-02 18:06:06'), + date_create('1997-09-02 18:06:18'), + date_create('1997-09-02 18:18:06'))), + array(array('byday'=>array('TU', 'TH'),'byhour'=>'6,18','bysetpos'=>array(3, -3), 'dtstart' => '1997-09-02 09:00:00'),array( + date_create('1997-09-02 18:00:00'), + date_create('1997-09-04 06:00:00'), + date_create('1997-09-09 18:00:00'))) ); } /** @@ -319,38 +327,41 @@ class RRuleTest extends PHPUnit_Framework_TestCase date_create('1997-09-02'), date_create('1997-09-04'), date_create('1997-09-06'))), array(array('interval'=>92),array( date_create('1997-09-02'), date_create('1997-12-03'), date_create('1998-03-05'))), - array(array('bymonth'=>array(1, 3)),array( + array(array('bymonth'=>'1,3'),array( date_create('1998-01-01'), date_create('1998-01-02'), date_create('1998-01-03'))), - array(array('bymonthday'=>array(1, 3)),array( + array(array('bymonthday'=>'1,3'),array( date_create('1997-09-03'), date_create('1997-10-01'), date_create('1997-10-03'))), - array(array('bymonth'=>array(1, 3),'bymonthday'=>array(5, 7)),array( + array(array('bymonth'=>'1,3','bymonthday'=>array(5, 7)),array( date_create('1998-01-05'), date_create('1998-01-07'), date_create('1998-03-05'))), array(array('byday'=>array('TU', 'TH')),array( date_create('1997-09-02'), date_create('1997-09-04'), date_create('1997-09-09'))), - array(array('bymonth'=> array(1, 3), 'byday'=> array('TU', 'TH')),array( + array(array('bymonth'=> '1,3', 'byday'=> array('TU', 'TH')),array( date_create('1998-01-01'), date_create('1998-01-06'), date_create('1998-01-08'))), - array(array('bymonthday'=> array(1, 3), 'byday'=>array('TU', 'TH')),array( + array(array('bymonthday'=> '1,3', 'byday'=>array('TU', 'TH')),array( date_create('1998-01-01'), date_create('1998-02-03'), date_create('1998-03-03'))), - array(array('bymonth'=>array(1, 3),'bymonthday'=>array(1, 3),'byday'=>array('TU', 'TH')),array( + array(array('bymonth'=>'1,3','bymonthday'=>'1,3','byday'=>array('TU', 'TH')),array( date_create('1998-01-01'), date_create('1998-03-03'), date_create('2001-03-01'))), // TODO BYSETPOS - array(array('BYHOUR'=> array(6, 18)),array( + + array(array('BYHOUR'=> '6,18'),array( date_create('1997-09-02 06:00:00'),date_create('1997-09-02 18:00:00'),date_create('1997-09-03 06:00:00'))), - array(array('BYMINUTE'=> array(6, 18)),array( + array(array('BYMINUTE'=> '6,18'),array( date_create('1997-09-02 00:06:00'),date_create('1997-09-02 00:18:00'),date_create('1997-09-03 00:06:00'))), - array(array('BYSECOND' => array(6, 18)),array( + array(array('BYSECOND' => '6,18'),array( date_create('1997-09-02 00:00:06'),date_create('1997-09-02 00:00:18'),date_create('1997-09-03 00:00:06'))), - array(array('BYHOUR'=>array(6, 18),'BYMINUTE'=>array(6, 18)),array( + array(array('BYHOUR'=>'6,18','BYMINUTE'=>'6,18'),array( date_create('1997-09-02 06:06:00'),date_create('1997-09-02 06:18:00'),date_create('1997-09-02 18:06:00'))), - array(array('BYHOUR'=>array(6, 18),'BYSECOND'=>array(6, 18)),array( + array(array('BYHOUR'=>'6,18','BYSECOND'=>'6,18'),array( date_create('1997-09-02 06:00:06'),date_create('1997-09-02 06:00:18'),date_create('1997-09-02 18:00:06'))), - array(array('BYMINUTE'=>array(6, 18),'BYSECOND'=>array(6, 18)),array( + array(array('BYMINUTE'=>'6,18','BYSECOND'=>'6,18'),array( date_create('1997-09-02 00:06:06'),date_create('1997-09-02 00:06:18'),date_create('1997-09-02 00:18:06'))), - array(array('BYHOUR'=>array(6, 18),'BYMINUTE'=>array(6, 18),'BYSECOND'=>array(6, 18)),array( + array(array('BYHOUR'=>'6,18','BYMINUTE'=>'6,18','BYSECOND'=>'6,18'),array( date_create('1997-09-02 06:06:06'),date_create('1997-09-02 06:06:18'),date_create('1997-09-02 06:18:06'))), - // array(array('BYMONTHDAY'=>array(13, 17),'BYHOUR'=>array(6, 18),'BYSETPOS'=>array(3, -3)),array( - // date_create('1997-09-13 06:00'),date_create('1997-09-17'),date_create('1997-10-13'))) + array(array('BYHOUR'=>'6,18','byminute' => '15,45', 'BYSETPOS'=>array(3, -3), 'dtstart' => '1997-09-02, 09:00'),array( + date_create('1997-09-02 18:15'), + date_create('1997-09-03 06:45'), + date_create('1997-09-03 18:15'))) ); } @@ -464,11 +475,10 @@ class RRuleTest extends PHPUnit_Framework_TestCase date_create('1997-09-02 18:06:06'), date_create('1997-09-02 18:06:18'), date_create('1997-09-02 18:18:06'))), - // FIXME infinite loop - // array(array('byminute'=>'15, 45','bysecond'=>'15, 45','bysetpos'=>'3, -3'), array( - // date_create('1997-09-02 09:15:45'), - // date_create('1997-09-02 09:45:15'), - // date_create('1997-09-02 10:15:45'))) + array(array('byminute'=>'15, 45','bysecond'=>'15, 45','bysetpos'=>'3, -3'), array( + date_create('1997-09-02 09:15:45'), + date_create('1997-09-02 09:45:15'), + date_create('1997-09-02 10:15:45'))) ); } /** @@ -487,6 +497,9 @@ class RRuleTest extends PHPUnit_Framework_TestCase } } + /** + * MINUTELY rules, mostly taken from the Python test suite + */ public function minutelyRules() { return array( @@ -578,11 +591,10 @@ class RRuleTest extends PHPUnit_Framework_TestCase date_create('1997-09-02 18:06:06'), date_create('1997-09-02 18:06:18'), date_create('1997-09-02 18:18:06'))), - //FIXME - // array(array('bysecond'=>'15, 30, 45','bysetpos'=>'3, -3'),array( - // date_create('1997-09-02 09:00:15'), - // date_create('1997-09-02 09:00:45'), - // date_create('1997-09-02 09:01:15'))) + array(array('bysecond'=>'15, 30, 45','bysetpos'=>'3, -3'),array( + date_create('1997-09-02 09:00:15'), + date_create('1997-09-02 09:00:45'), + date_create('1997-09-02 09:01:15'))) ); } /** @@ -695,12 +707,10 @@ class RRuleTest extends PHPUnit_Framework_TestCase date_create('1997-09-02 18:06:06'), date_create('1997-09-02 18:06:18'), date_create('1997-09-02 18:18:06'))), - // TODO BY SETPOS array(array('bysecond'=>'0','byminute'=>'1','dtstart'=>date_create('2010-03-22 12:01:00')), array( date_create('2010-03-22 12:01:00'), date_create('2010-03-22 13:01:00'), date_create('2010-03-22 14:01:00'))), - ); } /** @@ -1291,7 +1301,7 @@ class RRuleTest extends PHPUnit_Framework_TestCase /** * Rules that generate no occurence, because of a bad combination of BYXXX parts - * An infinite loop is likely to occur if a test fails. + * This tests are here to ensure that the lib will not go into an infinite loop. */ public function rulesWithoutOccurrences() { @@ -1305,7 +1315,7 @@ class RRuleTest extends PHPUnit_Framework_TestCase 'dtstart' => '1997-02-02 09:00:00', 'count' => 1 )), - // Every 2 months, on odd months, starting a even month (impossible) + // Every 2 months, on odd months, starting a even month (nope) array(array( 'freq' => 'monthly', 'interval' => 2, @@ -1313,6 +1323,65 @@ class RRuleTest extends PHPUnit_Framework_TestCase 'dtstart' => '1997-02-02 09:00:00', 'count' => 1 )), + // every 52 weeks, in November, starting in July (not going to happen) + array(array( + 'freq' => 'weekly', + 'interval' => 52, + 'bymonth' => 11, + 'dtstart' => '2015-07-01 09:00:00', + 'count' => 1 + )), + // every 7 days, monday, starting a wednesday (still nope) + array(array( + 'freq' => 'daily', + 'interval' => 7, + 'byday' => 'MO', + 'dtstart' => '2015-07-01 09:00:00', + 'count' => 1 + )), + // every 4 hours, on odd hours, starting an even hour (nein) + array(array( + 'freq' => 'hourly', + 'interval' => 4, + 'byhour' => '7, 11, 15, 19', + 'dtstart'=> '1997-09-02 09:00:00', + 'count' => 1 + )), + array(array( + 'freq' => 'minutely', + 'interval' => 12, + 'byminute' => '10, 11, 25, 39, 50', + 'dtstart'=> '1997-09-02 09:00:00', + 'count' => 1 + )), + array(array( + 'freq' => 'minutely', + 'interval' => 120, + 'byminute' => '10, 12, 14, 16', + 'dtstart'=> '1997-09-02 09:00:00', + 'count' => 1 + )), + array(array( + 'freq' => 'secondly', + 'interval' => 10, + 'bysecond' => '2, 15, 37, 42, 59', + 'dtstart'=> '1997-09-02 09:00:00', + 'count' => 1 + )), + array(array( + 'freq' => 'secondly', + 'interval' => 360, + 'bysecond' => '10, 28, 49', + 'dtstart'=> '1997-09-02 09:00:00', + 'count' => 1 + )), + array(array( + 'freq' => 'secondly', + 'interval' => 43200, + 'bysecond' => '2, 10, 18, 23', + 'dtstart'=> '1997-09-02 09:00:00', + 'count' => 1 + )) ); } /** @@ -1325,8 +1394,8 @@ class RRuleTest extends PHPUnit_Framework_TestCase } /** - * Just some more random rules found here and there. Some of them - * might not bring any additional value to the tests to be honest, but + * Just some more random rules found here and there, with some edges cases. + * Some of them might not bring any additional value to the tests to be honest, but * it's good to test them anyway. */ public function variousRules() @@ -1368,7 +1437,56 @@ class RRuleTest extends PHPUnit_Framework_TestCase array(date_create('1998-12-28 09:00:00'), date_create('2004-12-27 09:00:00'), date_create('2009-12-28 09:00:00')) - ) + ), + // test that occurrences are returned in chronogical order, even when the BYXXX are not in order + array( + array('freq' => 'yearly', 'bymonth' => '2,1', 'count' => 3, 'dtstart' => '2015-07-01 09:00:00'), + array(date_create('2016-01-01 09:00:00'), + date_create('2016-02-01 09:00:00'), + date_create('2017-01-01 09:00:00')) + ), + array( + array('freq' => 'yearly', 'byweekno' => '30,50,40', 'byday' => 'MO', 'count' => 3, 'dtstart' => '2015-07-01 09:00:00'), + array(date_create('2015-07-20 09:00:00'), + date_create('2015-09-28 09:00:00'), + date_create('2015-12-07 09:00:00')) + ), + array( + array('freq' => 'yearly', 'byyearday' => '3,2,1', 'count' => 3, 'dtstart' => '2015-07-01 09:00:00'), + array(date_create('2016-01-01 09:00:00'), + date_create('2016-01-02 09:00:00'), + date_create('2016-01-03 09:00:00')) + ), + array( + array('freq' => 'yearly', 'bymonthday' => '31,30', 'count' => 3, 'dtstart' => '2015-07-01 09:00:00'), + array(date_create('2015-07-30 09:00:00'), + date_create('2015-07-31 09:00:00'), + date_create('2015-08-30 09:00:00')) + ), + array( + array('freq' => 'yearly', 'byday' => 'TU,MO', 'count' => 3, 'dtstart' => '2015-07-01 09:00:00'), + array(date_create('2015-07-06 09:00:00'), + date_create('2015-07-07 09:00:00'), + date_create('2015-07-13 09:00:00')) + ), + array( + array('freq' => 'yearly', 'byhour' => '9,8', 'count' => 3, 'dtstart' => '2015-07-01 09:00:00'), + array(date_create('2015-07-01 09:00:00'), + date_create('2016-07-01 08:00:00'), + date_create('2016-07-01 09:00:00')) + ), + array( + array('freq' => 'yearly', 'byminute' => '30,15', 'count' => 3, 'dtstart' => '2015-07-01 09:00:00'), + array(date_create('2015-07-01 09:15:00'), + date_create('2015-07-01 09:30:00'), + date_create('2016-07-01 09:15:00')) + ), + array( + array('freq' => 'yearly', 'bysecond' => '30,15', 'count' => 3, 'dtstart' => '2015-07-01 09:00:00'), + array(date_create('2015-07-01 09:00:15'), + date_create('2015-07-01 09:00:30'), + date_create('2016-07-01 09:00:15')) + ), ); } @@ -1385,18 +1503,18 @@ class RRuleTest extends PHPUnit_Framework_TestCase } /** - * Test that occursAt doesn't return true for wrong dates + * Test that occursAt doesn't return false positives */ public function notOccurrences() { return array( array( array('FREQ' => 'YEARLY', 'DTSTART' => '1999-09-02'), - array('1999-09-01','1999-09-03') + array('1999-09-01','1999-09-02 12:00:00','1999-09-03') ), array( array('FREQ' => 'YEARLY', 'DTSTART' => '1999-09-02', 'UNTIL' => '2000-09-02'), - array('2001-09-02') + array('2001-09-02', '2000-09-02 12:00:00') ), array( array('FREQ' => 'YEARLY', 'DTSTART' => '1999-09-02', 'COUNT' => 3), @@ -1410,9 +1528,14 @@ class RRuleTest extends PHPUnit_Framework_TestCase array('FREQ' => 'MONTHLY', 'DTSTART' => '1999-09-02', 'INTERVAL' => 2), array('1999-10-02', '1999-12-02') ), - - // todo weekly,daily - + array( + array('FREQ' => 'WEEKLY', 'DTSTART' => '2015-07-01', 'INTERVAL' => 2), + array('2015-07-02', '2015-07-07 23:59:59', '2015-07-08 00:00:01') + ), + array( + array('FREQ' => 'DAILY', 'DTSTART' => '2015-07-01', 'INTERVAL' => 2), + array('2015-07-02', '2015-07-02 23:59:59', '2015-07-03 00:00:01') + ), array( array('freq' => 'hourly', 'dtstart' => '1999-09-02 09:00:00', 'INTERVAL' => 2), array('1999-09-02 10:00:00', '1999-09-02 09:01:01','1999-09-02 12:00:00') @@ -1421,6 +1544,14 @@ class RRuleTest extends PHPUnit_Framework_TestCase array('freq' => 'hourly', 'dtstart' => '1999-09-02 09:00:00', 'INTERVAL' => 5), array('1999-09-03 09:00:00') ), + array( + array('freq' => 'minutely', 'dtstart' => '1999-09-02 09:00:00', 'INTERVAL' => 5), + array('1999-09-02 09:01:00') + ), + array( + array('freq' => 'secondly', 'dtstart' => '1999-09-02 09:00:00', 'INTERVAL' => 5), + array('1999-09-02 09:00:01') + ), ); }