Refactoring

This commit is contained in:
simshaun 2014-01-14 23:38:07 -05:00
parent 60deaec32f
commit 72e4f89991
28 changed files with 1610 additions and 1093 deletions

6
CHANGELOG-0.2.txt Normal file
View File

@ -0,0 +1,6 @@
- Moved RecurrenceRuleTransformer::FREQ_* constants to `Frequency::*`
- Renamed `RecurrenceRule` to `Rule`
- Renamed `RecurrenceRuleTransformer` to `RuleTransformer`
- Renamed `TransformerConfig` to `RuleTransformerConfig`
- Renamed `RuleTransformer::setTransformerConfig` to `RuleTransformer::setConfig`
- Organized tests so they are easier to find and add to.

View File

@ -40,8 +40,8 @@ Demo
```php
$timezone = 'America/New_York';
$startDate = new \DateTime('2013-06-12 20:00:00', new \DateTimeZone($timezone));
$rule = new \Recurr\RecurrenceRule('FREQ=MONTHLY;COUNT=5', $startDate, $timezone);
$transformer = new \Recurr\RecurrenceRuleTransformer($rule);
$rule = new \Recurr\Rule('FREQ=MONTHLY;COUNT=5', $startDate, $timezone);
$transformer = new \Recurr\RuleTransformer($rule);
print_r($transformer->getComputedArray());
```
@ -55,12 +55,12 @@ Warnings
```php
$timezone = 'America/New_York';
$startDate = new \DateTime('2013-01-31 20:00:00', new \DateTimeZone($timezone));
$rule = new \Recurr\RecurrenceRule('FREQ=MONTHLY;COUNT=5', $startDate, $timezone);
$transformer = new \Recurr\RecurrenceRuleTransformer($rule);
$rule = new \Recurr\Rule('FREQ=MONTHLY;COUNT=5', $startDate, $timezone);
$transformer = new \Recurr\RuleTransformer($rule);
$transformerConfig = new \Recurr\TransformerConfig();
$transformerConfig = new \Recurr\RuleTransformerConfig();
$transformerConfig->enableLastDayOfMonthFix();
$transformer->setTransformerConfig($transformerConfig);
$transformer->setConfig($transformerConfig);
print_r($transformer->getComputedArray());

View File

@ -15,7 +15,7 @@ namespace Recurr;
/**
* Class DateInfo is responsible for holding information based on a particular
* date that is applicable to a RecurrenceRule.
* date that is applicable to a Rule.
*
* @package Recurr
* @author Shaun Simmons <shaun@envysphere.com>

View File

@ -19,10 +19,10 @@ namespace Recurr;
use Recurr\DaySet;
use Recurr\Time;
use Recurr\RecurrenceRule;
use Recurr\Rule;
/**
* Class DateUtil is responsible for providing utilities applicable to RecurrenceRules.
* Class DateUtil is responsible for providing utilities applicable to Rules.
*
* @package Recurr
* @author Shaun Simmons <shaun@envysphere.com>
@ -107,7 +107,7 @@ class DateUtil
*
* @param \DateTime $dt
* @param \DateTime $start
* @param null|RecurrenceRule $rule
* @param null|Rule $rule
* @param null|DateInfo $dtInfo
*
* @return DaySet
@ -115,7 +115,7 @@ class DateUtil
public static function getDaySetOfWeek(
\DateTime $dt,
\DateTime $start,
RecurrenceRule $rule = null,
Rule $rule = null,
DateInfo $dtInfo = null
)
{
@ -141,33 +141,33 @@ class DateUtil
}
/**
* @param RecurrenceRule $rule
* @param Rule $rule
* @param \DateTime $dt
* @param DateInfo $dtInfo
* @param \DateTime $start
*
* @return DaySet
*/
public static function getDaySet(RecurrenceRule $rule, \DateTime $dt, DateInfo $dtInfo, $start)
public static function getDaySet(Rule $rule, \DateTime $dt, DateInfo $dtInfo, $start)
{
switch ($rule->getFreq()) {
case RecurrenceRule::FREQ_SECONDLY:
case Frequency::SECONDLY:
return self::getDaySetOfDay($dt, $start, $rule, $dtInfo);
break;
case RecurrenceRule::FREQ_MINUTELY:
case Frequency::MINUTELY:
return self::getDaySetOfDay($dt, $start, $rule, $dtInfo);
break;
case RecurrenceRule::FREQ_HOURLY:
case Frequency::HOURLY:
return self::getDaySetOfDay($dt, $start, $rule, $dtInfo);
break;
case RecurrenceRule::FREQ_DAILY:
case Frequency::DAILY:
return self::getDaySetOfDay($dt, $start, $rule, $dtInfo);
break;
case RecurrenceRule::FREQ_WEEKLY:
case Frequency::WEEKLY:
return self::getDaySetOfWeek($dt, $start, $rule, $dtInfo);
case RecurrenceRule::FREQ_MONTHLY:
case Frequency::MONTHLY:
return self::getDaySetOfMonth($dt, $start, $rule, $dtInfo);
case RecurrenceRule::FREQ_YEARLY:
case Frequency::YEARLY:
return self::getDaySetOfYear($dt, $start, $rule, $dtInfo);
}
}
@ -235,12 +235,12 @@ class DateUtil
}
/**
* @param RecurrenceRule $rule
* @param Rule $rule
* @param \DateTime $dt
*
* @return array
*/
public static function getTimeSetOfHour(RecurrenceRule $rule, \DateTime $dt)
public static function getTimeSetOfHour(Rule $rule, \DateTime $dt)
{
$set = array();
@ -266,12 +266,12 @@ class DateUtil
}
/**
* @param RecurrenceRule $rule
* @param Rule $rule
* @param \DateTime $dt
*
* @return array
*/
public static function getTimeSetOfMinute(RecurrenceRule $rule, \DateTime $dt)
public static function getTimeSetOfMinute(Rule $rule, \DateTime $dt)
{
$set = array();
@ -301,16 +301,16 @@ class DateUtil
}
/**
* @param RecurrenceRule $rule
* @param Rule $rule
* @param \DateTime $dt
*
* @return array
*/
public static function getTimeSet(RecurrenceRule $rule, \DateTime $dt)
public static function getTimeSet(Rule $rule, \DateTime $dt)
{
$set = array();
if (null === $rule || $rule->getFreq() >= RecurrenceRule::FREQ_HOURLY) {
if (null === $rule || $rule->getFreq() >= Frequency::HOURLY) {
return $set;
}

25
src/Recurr/Frequency.php Normal file
View File

@ -0,0 +1,25 @@
<?php
/*
* Copyright 2014 Shaun Simmons
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Based on:
* rrule.js - Library for working with recurrence rules for calendar dates.
* Copyright 2010, Jakub Roztocil and Lars Schoning
* https://github.com/jkbr/rrule/blob/master/LICENCE
*/
namespace Recurr;
class Frequency {
const YEARLY = 0;
const MONTHLY = 1;
const WEEKLY = 2;
const DAILY = 3;
const HOURLY = 4;
const MINUTELY = 5;
const SECONDLY = 6;
}

View File

@ -85,16 +85,8 @@ use Recurr\Weekday;
* @package Recurr
* @author Shaun Simmons <shaun@envysphere.com>
*/
class RecurrenceRule
class Rule
{
const FREQ_SECONDLY = 6;
const FREQ_MINUTELY = 5;
const FREQ_HOURLY = 4;
const FREQ_DAILY = 3;
const FREQ_WEEKLY = 2;
const FREQ_MONTHLY = 1;
const FREQ_YEARLY = 0;
public static $freqs = array(
'YEARLY' => 0,
'MONTHLY' => 1,
@ -165,7 +157,7 @@ class RecurrenceRule
protected $bySetPosition;
/**
* Construct a new RecurrenceRule.
* Construct a new Rule.
*
* @param null|string $rrule RRULE string
* @param null|\DateTime $startDate
@ -445,19 +437,19 @@ class RecurrenceRule
* Identifies the type of recurrence rule.
*
* May be one of:
* - RecurrenceRule::FREQ_SECONDLY to specify repeating events based on an
* - Frequency::SECONDLY to specify repeating events based on an
* interval of a second or more.
* - RecurrenceRule::FREQ_MINUTELY to specify repeating events based on an
* - Frequency::MINUTELY to specify repeating events based on an
* interval of a minute or more.
* - RecurrenceRule::FREQ_HOURLY to specify repeating events based on an
* - Frequency::HOURLY to specify repeating events based on an
* interval of an hour or more.
* - RecurrenceRule::FREQ_DAILY to specify repeating events based on an
* - Frequency::DAILY to specify repeating events based on an
* interval of a day or more.
* - RecurrenceRule::FREQ_WEEKLY to specify repeating events based on an
* - Frequency::WEEKLY to specify repeating events based on an
* interval of a week or more.
* - RecurrenceRule::FREQ_MONTHLY to specify repeating events based on an
* - Frequency::MONTHLY to specify repeating events based on an
* interval of a month or more.
* - RecurrenceRule::FREQ_YEAR to specify repeating events based on an
* - Frequency::YEAR to specify repeating events based on an
* interval of a year or more.
*
* @param string $freq Frequency of recurrence.

View File

@ -20,7 +20,7 @@ use Recurr\DateUtil;
use Recurr\Exception\MissingData;
/**
* This class is responsible for transforming a RecurrenceRule in to an array
* This class is responsible for transforming a Rule in to an array
* of \DateTime() objects.
*
* If a recurrence rule is infinitely recurring, a virtual limit is imposed.
@ -28,23 +28,15 @@ use Recurr\Exception\MissingData;
* @package Recurr
* @author Shaun Simmons <shaun@envysphere.com>
*/
class RecurrenceRuleTransformer
class RuleTransformer
{
const FREQ_YEARLY = 0;
const FREQ_MONTHLY = 1;
const FREQ_WEEKLY = 2;
const FREQ_DAILY = 3;
const FREQ_HOURLY = 4;
const FREQ_MINUTELY = 5;
const FREQ_SECONDLY = 6;
/** @var RecurrenceRule */
/** @var Rule */
protected $rule;
/** @var int */
protected $virtualLimit;
/** @var TransformerConfig */
/** @var RuleTransformerConfig */
protected $config;
/**
@ -56,24 +48,24 @@ class RecurrenceRuleTransformer
protected $leapBug = false;
/**
* Construct a new RecurrenceRuleTransformer
* Construct a new RuleTransformer
*
* @param null $recurrenceRule The RecurrenceRule
* @param null $rule The Rule
* @param null $virtualLimit The virtual limit imposed upon infinite recurrence
* @param TransformerConfig $config
* @param RuleTransformerConfig $config
*/
public function __construct($recurrenceRule = null, $virtualLimit = null, TransformerConfig $config = null)
public function __construct($rule = null, $virtualLimit = null, RuleTransformerConfig $config = null)
{
if (null !== $recurrenceRule) {
$this->setRule($recurrenceRule);
if (null !== $rule) {
$this->setRule($rule);
}
if (is_int($virtualLimit)) {
$this->setVirtualLimit($virtualLimit);
}
if (!$config instanceof TransformerConfig) {
$config = new TransformerConfig();
if (!$config instanceof RuleTransformerConfig) {
$config = new RuleTransformerConfig();
}
$this->config = $config;
@ -82,15 +74,15 @@ class RecurrenceRuleTransformer
}
/**
* @param TransformerConfig $config
* @param RuleTransformerConfig $config
*/
public function setTransformerConfig($config)
public function setConfig($config)
{
$this->config = $config;
}
/**
* Transform a RecurrenceRule in to an array of \DateTimes
* Transform a Rule in to an array of \DateTimes
*
* @return array
* @throws MissingData
@ -99,7 +91,7 @@ class RecurrenceRuleTransformer
{
$rule = $this->getRule();
if (null === $rule) {
throw new MissingData('RecurrenceRule has not been set');
throw new MissingData('Rule has not been set');
}
$start = $rule->getStartDate();
@ -137,21 +129,21 @@ class RecurrenceRuleTransformer
if (!(!empty($byWeekNum) || !empty($byYearDay) || !empty($byMonthDay) || !empty($byWeekDay))) {
switch ($freq) {
case RecurrenceRule::FREQ_YEARLY:
case Frequency::YEARLY:
if (empty($byMonth)) {
$byMonth = array($start->format('n'));
}
$byMonthDay = array($startDay);
break;
case RecurrenceRule::FREQ_MONTHLY:
case Frequency::MONTHLY:
if ($startDay > 28) {
$fixLastDayOfMonth = true;
}
$byMonthDay = array($startDay);
break;
case RecurrenceRule::FREQ_WEEKLY:
case Frequency::WEEKLY:
$byWeekDay = array(
new Weekday(
DateUtil::getDayOfWeek($start),
@ -230,21 +222,21 @@ class RecurrenceRuleTransformer
$wDayMaskRel = array();
$timeSet = DateUtil::getTimeSet($rule, $dt);
if ($freq >= RecurrenceRule::FREQ_HOURLY) {
if (($freq >= RecurrenceRule::FREQ_HOURLY && !empty($byHour) && !in_array($hour, $byHour)) ||
($freq >= RecurrenceRule::FREQ_MINUTELY && !empty($byMinute) && !in_array($minute, $byMinute)) ||
($freq >= RecurrenceRule::FREQ_SECONDLY && !empty($bySecond) && !in_array($second, $bySecond)))
if ($freq >= Frequency::HOURLY) {
if (($freq >= Frequency::HOURLY && !empty($byHour) && !in_array($hour, $byHour)) ||
($freq >= Frequency::MINUTELY && !empty($byMinute) && !in_array($minute, $byMinute)) ||
($freq >= Frequency::SECONDLY && !empty($bySecond) && !in_array($second, $bySecond)))
{
$timeSet = array();
} else {
switch ($freq) {
case RecurrenceRule::FREQ_HOURLY:
case Frequency::HOURLY:
$timeSet = DateUtil::getTimeSetOfHour($rule, $dt);
break;
case RecurrenceRule::FREQ_MINUTELY:
case Frequency::MINUTELY:
$timeSet = DateUtil::getTimeSetOfMinute($rule, $dt);
break;
case RecurrenceRule::FREQ_SECONDLY:
case Frequency::SECONDLY:
$timeSet = DateUtil::getTimeSetOfSecond($dt);
break;
}
@ -364,7 +356,7 @@ class RecurrenceRuleTransformer
if (!empty($byWeekDayRel)) {
$ranges = array();
if (RecurrenceRule::FREQ_YEARLY == $freq) {
if (Frequency::YEARLY == $freq) {
if (!empty($byMonth)) {
foreach ($byMonth as $mo) {
$ranges[] = array_slice($dtInfo->mRanges, $mo - 1, 2);
@ -372,7 +364,7 @@ class RecurrenceRuleTransformer
} else {
$ranges[] = array(0, $dtInfo->yearLength);
}
} elseif (RecurrenceRule::FREQ_MONTHLY == $freq) {
} elseif (Frequency::MONTHLY == $freq) {
$ranges[] = array_slice($dtInfo->mRanges, $month - 1, 2);
}
@ -577,13 +569,13 @@ class RecurrenceRuleTransformer
}
switch ($freq) {
case RecurrenceRule::FREQ_YEARLY:
case Frequency::YEARLY:
$year += $rule->getInterval();
$month = $dt->format('n');
$day = $dt->format('j');
$dt->setDate($year, $month, $day);
break;
case RecurrenceRule::FREQ_MONTHLY:
case Frequency::MONTHLY:
$month += $rule->getInterval();
if ($month > 12) {
$delta = floor($month / 12);
@ -597,7 +589,7 @@ class RecurrenceRuleTransformer
}
$dt->setDate($year, $month, 1);
break;
case RecurrenceRule::FREQ_WEEKLY:
case Frequency::WEEKLY:
if ($weekStart > $dtInfo->dayOfWeek) {
$delta = ($dtInfo->dayOfWeek + 1 + (6 - $weekStart)) * -1 +
$rule->getInterval() * 7;
@ -611,20 +603,20 @@ class RecurrenceRuleTransformer
$month = $dt->format('n');
$day = $dt->format('j');
break;
case RecurrenceRule::FREQ_DAILY:
case Frequency::DAILY:
$dt->modify('+'.$rule->getInterval().' day');
$year = $dt->format('Y');
$month = $dt->format('n');
$day = $dt->format('j');
break;
case RecurrenceRule::FREQ_HOURLY:
case Frequency::HOURLY:
$dt->modify('+'.$rule->getInterval().' hours');
$year = $dt->format('Y');
$month = $dt->format('n');
$day = $dt->format('j');
$hour = $dt->format('G');
break;
case RecurrenceRule::FREQ_MINUTELY:
case Frequency::MINUTELY:
$dt->modify('+'.$rule->getInterval().' minutes');
$year = $dt->format('Y');
$month = $dt->format('n');
@ -632,7 +624,7 @@ class RecurrenceRuleTransformer
$hour = $dt->format('G');
$minute = $dt->format('i');
break;
case RecurrenceRule::FREQ_SECONDLY:
case Frequency::SECONDLY:
$dt->modify('+'.$rule->getInterval().' seconds');
$year = $dt->format('Y');
$month = $dt->format('n');
@ -648,9 +640,9 @@ class RecurrenceRuleTransformer
}
/**
* Set the RecurrenceRule
* Set the Rule
*
* @param RecurrenceRule $rule The RecurrenceRule
* @param Rule $rule The Rule
*
* @return $this
*/
@ -662,9 +654,9 @@ class RecurrenceRuleTransformer
}
/**
* Get the RecurrenceRule
* Get the Rule
*
* @return RecurrenceRule
* @return Rule
*/
public function getRule()
{
@ -688,11 +680,11 @@ class RecurrenceRuleTransformer
/**
* Get the virtual limit imposed upon infinitely recurring events.
*
* @param RecurrenceRule $rule
* @param Rule $rule
*
* @return int
*/
public function getVirtualLimit(RecurrenceRule $rule)
public function getVirtualLimit(Rule $rule)
{
return 732;
}

View File

@ -9,7 +9,7 @@
namespace Recurr;
class TransformerConfig {
class RuleTransformerConfig {
protected $lastDayOfMonthFix = false;
/**

View File

@ -1,984 +0,0 @@
<?php
namespace Recurr\Test;
use Recurr\RecurrenceRule;
use Recurr\RecurrenceRuleTransformer;
use Recurr\TransformerConfig;
class RecurrenceRuleTransformerTest extends \PHPUnit_Framework_TestCase
{
/** @var RecurrenceRuleTransformer */
protected $transformer;
protected $timezone = 'America/New_York';
public function setUp()
{
$this->transformer = new RecurrenceRuleTransformer;
}
public function testSecondly()
{
$rule = new RecurrenceRule(
'FREQ=SECONDLY;COUNT=5;',
new \DateTime('2016-02-29 23:58:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2016-02-29 23:58:00'), $computed[0]);
$this->assertEquals(new \DateTime('2016-02-29 23:58:01'), $computed[1]);
$this->assertEquals(new \DateTime('2016-02-29 23:58:02'), $computed[2]);
$this->assertEquals(new \DateTime('2016-02-29 23:58:03'), $computed[3]);
$this->assertEquals(new \DateTime('2016-02-29 23:58:04'), $computed[4]);
}
public function testSecondlyInterval()
{
$rule = new RecurrenceRule(
'FREQ=SECONDLY;COUNT=5;INTERVAL=58;',
new \DateTime('2016-02-29 23:58:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2016-02-29 23:58:00'), $computed[0]);
$this->assertEquals(new \DateTime('2016-02-29 23:58:58'), $computed[1]);
$this->assertEquals(new \DateTime('2016-02-29 23:59:56'), $computed[2]);
$this->assertEquals(new \DateTime('2016-03-01 00:00:54'), $computed[3]);
$this->assertEquals(new \DateTime('2016-03-01 00:01:52'), $computed[4]);
}
public function testMinutely()
{
$rule = new RecurrenceRule(
'FREQ=MINUTELY;COUNT=5;',
new \DateTime('2016-02-29 23:58:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2016-02-29 23:58:00'), $computed[0]);
$this->assertEquals(new \DateTime('2016-02-29 23:59:00'), $computed[1]);
$this->assertEquals(new \DateTime('2016-03-01 00:00:00'), $computed[2]);
$this->assertEquals(new \DateTime('2016-03-01 00:01:00'), $computed[3]);
$this->assertEquals(new \DateTime('2016-03-01 00:02:00'), $computed[4]);
}
public function testMinutelyInterval()
{
$rule = new RecurrenceRule(
'FREQ=MINUTELY;COUNT=5;INTERVAL=58;',
new \DateTime('2013-02-28 23:58:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-02-28 23:58:00'), $computed[0]);
$this->assertEquals(new \DateTime('2013-02-29 00:56:00'), $computed[1]);
$this->assertEquals(new \DateTime('2013-02-29 01:54:00'), $computed[2]);
$this->assertEquals(new \DateTime('2013-02-29 02:52:00'), $computed[3]);
$this->assertEquals(new \DateTime('2013-02-29 03:50:00'), $computed[4]);
}
public function testHourly()
{
$rule = new RecurrenceRule(
'FREQ=HOURLY;COUNT=5;',
new \DateTime('2013-02-28 23:00:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-02-28 23:00:00'), $computed[0]);
$this->assertEquals(new \DateTime('2013-03-01 00:00:00'), $computed[1]);
$this->assertEquals(new \DateTime('2013-03-01 01:00:00'), $computed[2]);
$this->assertEquals(new \DateTime('2013-03-01 02:00:00'), $computed[3]);
$this->assertEquals(new \DateTime('2013-03-01 03:00:00'), $computed[4]);
}
public function testHourlyInterval()
{
$rule = new RecurrenceRule(
'FREQ=HOURLY;COUNT=5;INTERVAL=9;',
new \DateTime('2013-02-28 23:00:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-02-28 23:00:00'), $computed[0]);
$this->assertEquals(new \DateTime('2013-03-01 08:00:00'), $computed[1]);
$this->assertEquals(new \DateTime('2013-03-01 17:00:00'), $computed[2]);
$this->assertEquals(new \DateTime('2013-03-02 02:00:00'), $computed[3]);
$this->assertEquals(new \DateTime('2013-03-02 11:00:00'), $computed[4]);
}
public function testHourlyLeapYear()
{
$rule = new RecurrenceRule(
'FREQ=HOURLY;COUNT=5;',
new \DateTime('2016-02-28 23:00:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2016-02-28 23:00:00'), $computed[0]);
$this->assertEquals(new \DateTime('2016-02-29 00:00:00'), $computed[1]);
$this->assertEquals(new \DateTime('2016-02-29 01:00:00'), $computed[2]);
$this->assertEquals(new \DateTime('2016-02-29 02:00:00'), $computed[3]);
$this->assertEquals(new \DateTime('2016-02-29 03:00:00'), $computed[4]);
}
public function testHourlyCrossingYears()
{
$rule = new RecurrenceRule(
'FREQ=HOURLY;COUNT=5;',
new \DateTime('2013-12-31 22:00:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-12-31 22:00:00'), $computed[0]);
$this->assertEquals(new \DateTime('2013-12-31 23:00:00'), $computed[1]);
$this->assertEquals(new \DateTime('2014-01-01 00:00:00'), $computed[2]);
$this->assertEquals(new \DateTime('2014-01-01 01:00:00'), $computed[3]);
$this->assertEquals(new \DateTime('2014-01-01 02:00:00'), $computed[4]);
}
public function testWeekly()
{
$timezone = 'America/New_York';
$timezoneObj = new \DateTimeZone($timezone);
$rule = new RecurrenceRule(
'FREQ=WEEKLY;COUNT=5;INTERVAL=1',
new \DateTime('2013-06-13 00:00:00', $timezoneObj),
$timezone
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-06-13 00:00:00', $timezoneObj), $computed[0]);
$this->assertEquals(new \DateTime('2013-06-20 00:00:00', $timezoneObj), $computed[1]);
$this->assertEquals(new \DateTime('2013-06-27 00:00:00', $timezoneObj), $computed[2]);
$this->assertEquals(new \DateTime('2013-07-04 00:00:00', $timezoneObj), $computed[3]);
$this->assertEquals(new \DateTime('2013-07-11 00:00:00', $timezoneObj), $computed[4]);
}
public function testWeeklyInterval()
{
$timezone = 'America/New_York';
$timezoneObj = new \DateTimeZone($timezone);
$rule = new RecurrenceRule(
'FREQ=WEEKLY;COUNT=5;INTERVAL=2',
new \DateTime('2013-12-19 00:00:00', $timezoneObj),
$timezone
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-12-19 00:00:00', $timezoneObj), $computed[0]);
$this->assertEquals(new \DateTime('2014-01-02 00:00:00', $timezoneObj), $computed[1]);
$this->assertEquals(new \DateTime('2014-01-16 00:00:00', $timezoneObj), $computed[2]);
$this->assertEquals(new \DateTime('2014-01-30 00:00:00', $timezoneObj), $computed[3]);
$this->assertEquals(new \DateTime('2014-02-13 00:00:00', $timezoneObj), $computed[4]);
}
public function testWeeklyIntervalLeapYear()
{
$timezone = 'America/New_York';
$timezoneObj = new \DateTimeZone($timezone);
$rule = new RecurrenceRule(
'FREQ=WEEKLY;COUNT=7;INTERVAL=2',
new \DateTime('2015-12-21 00:00:00', $timezoneObj),
$timezone
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(7, count($computed));
$this->assertEquals(new \DateTime('2015-12-21 00:00:00', $timezoneObj), $computed[0]);
$this->assertEquals(new \DateTime('2016-01-04 00:00:00', $timezoneObj), $computed[1]);
$this->assertEquals(new \DateTime('2016-01-18 00:00:00', $timezoneObj), $computed[2]);
$this->assertEquals(new \DateTime('2016-02-01 00:00:00', $timezoneObj), $computed[3]);
$this->assertEquals(new \DateTime('2016-02-15 00:00:00', $timezoneObj), $computed[4]);
$this->assertEquals(new \DateTime('2016-02-29 00:00:00', $timezoneObj), $computed[5]);
$this->assertEquals(new \DateTime('2016-03-14 00:00:00', $timezoneObj), $computed[6]);
}
public function testWeeklyIntervalHittingJan1()
{
$timezone = 'America/New_York';
$timezoneObj = new \DateTimeZone($timezone);
$rule = new RecurrenceRule(
'FREQ=WEEKLY;COUNT=3;INTERVAL=2',
new \DateTime('2013-12-18 00:00:00', $timezoneObj),
$timezone
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(3, count($computed));
$this->assertEquals(new \DateTime('2013-12-18 00:00:00', $timezoneObj), $computed[0]);
$this->assertEquals(new \DateTime('2014-01-01 00:00:00', $timezoneObj), $computed[1]);
$this->assertEquals(new \DateTime('2014-01-15 00:00:00', $timezoneObj), $computed[2]);
}
public function testMonthly()
{
$timezone = 'America/New_York';
$timezoneObj = new \DateTimeZone($timezone);
$rule = new RecurrenceRule(
'FREQ=MONTHLY;COUNT=3;INTERVAL=1',
new \DateTime('2013-01-31 00:00:00', $timezoneObj),
$timezone
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(3, count($computed));
$this->assertEquals(new \DateTime('2013-01-31 00:00:00', $timezoneObj), $computed[0]);
$this->assertEquals(new \DateTime('2013-03-31 00:00:00', $timezoneObj), $computed[1]);
$this->assertEquals(new \DateTime('2013-05-31 00:00:00', $timezoneObj), $computed[2]);
}
public function testMonthlyWithLastDayFixEnabled()
{
$rule = new RecurrenceRule(
'FREQ=MONTHLY;COUNT=10',
new \DateTime('2013-11-30')
);
$transformerConfig = new TransformerConfig();
$transformerConfig->enableLastDayOfMonthFix();
$this->transformer->setRule($rule);
$this->transformer->setTransformerConfig($transformerConfig);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(10, count($computed));
$this->assertEquals(new \DateTime('2013-11-30'), $computed[0]);
$this->assertEquals(new \DateTime('2013-12-30'), $computed[1]);
$this->assertEquals(new \DateTime('2014-01-30'), $computed[2]);
$this->assertEquals(new \DateTime('2014-02-28'), $computed[3]);
$this->assertEquals(new \DateTime('2014-03-30'), $computed[4]);
$this->assertEquals(new \DateTime('2014-04-30'), $computed[5]);
$this->assertEquals(new \DateTime('2014-05-30'), $computed[6]);
$this->assertEquals(new \DateTime('2014-06-30'), $computed[7]);
$this->assertEquals(new \DateTime('2014-07-30'), $computed[8]);
$this->assertEquals(new \DateTime('2014-08-30'), $computed[9]);
}
public function testMonthlyWithLastDayFixEnabledOnLeapYear()
{
$rule = new RecurrenceRule(
'FREQ=MONTHLY;COUNT=8',
new \DateTime('2016-01-31')
);
$transformerConfig = new TransformerConfig();
$transformerConfig->enableLastDayOfMonthFix();
$this->transformer->setRule($rule);
$this->transformer->setTransformerConfig($transformerConfig);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(8, count($computed));
$this->assertEquals(new \DateTime('2016-01-31'), $computed[0]);
$this->assertEquals(new \DateTime('2016-02-29'), $computed[1]);
$this->assertEquals(new \DateTime('2016-03-31'), $computed[2]);
$this->assertEquals(new \DateTime('2016-04-30'), $computed[3]);
$this->assertEquals(new \DateTime('2016-05-31'), $computed[4]);
$this->assertEquals(new \DateTime('2016-06-30'), $computed[5]);
$this->assertEquals(new \DateTime('2016-07-31'), $computed[6]);
$this->assertEquals(new \DateTime('2016-08-31'), $computed[7]);
}
public function testYearly()
{
$timezone = new \DateTimeZone('America/New_York');
$rule = new RecurrenceRule(
'FREQ=YEARLY;COUNT=3;INTERVAL=1',
new \DateTime('2013-06-13 00:00:00', $timezone)
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(3, count($computed));
$this->assertEquals(new \DateTime('2013-06-13 00:00:00', $timezone), $computed[0]);
$this->assertEquals(new \DateTime('2014-06-13 00:00:00', $timezone), $computed[1]);
$this->assertEquals(new \DateTime('2015-06-13 00:00:00', $timezone), $computed[2]);
}
public function testYearlyLeapYear()
{
$timezone = new \DateTimeZone('America/New_York');
$rule = new RecurrenceRule(
'FREQ=YEARLY;COUNT=5;INTERVAL=1',
new \DateTime('2016-02-29 00:00:00', $timezone)
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2016-02-29 00:00:00', $timezone), $computed[0]);
$this->assertEquals(new \DateTime('2017-02-28 00:00:00', $timezone), $computed[1]);
$this->assertEquals(new \DateTime('2018-02-28 00:00:00', $timezone), $computed[2]);
$this->assertEquals(new \DateTime('2019-02-28 00:00:00', $timezone), $computed[3]);
$this->assertEquals(new \DateTime('2020-02-29 00:00:00', $timezone), $computed[4]);
}
public function testByMinute()
{
$rule = new RecurrenceRule(
'FREQ=HOURLY;COUNT=5;BYMINUTE=14,15',
new \DateTime('2013-06-12 16:00:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-06-12 16:14:00'), $computed[0]);
$this->assertEquals(new \DateTime('2013-06-12 16:15:00'), $computed[1]);
$this->assertEquals(new \DateTime('2013-06-12 17:14:00'), $computed[2]);
$this->assertEquals(new \DateTime('2013-06-12 17:15:00'), $computed[3]);
$this->assertEquals(new \DateTime('2013-06-12 18:14:00'), $computed[4]);
}
public function testBySecond()
{
$rule = new RecurrenceRule(
'FREQ=HOURLY;COUNT=5;BYSECOND=36,45',
new \DateTime('2013-06-12 16:00:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-06-12 16:00:36'), $computed[0]);
$this->assertEquals(new \DateTime('2013-06-12 16:00:45'), $computed[1]);
$this->assertEquals(new \DateTime('2013-06-12 17:00:36'), $computed[2]);
$this->assertEquals(new \DateTime('2013-06-12 17:00:45'), $computed[3]);
$this->assertEquals(new \DateTime('2013-06-12 18:00:36'), $computed[4]);
}
public function testMinutelyBySecond()
{
$rule = new RecurrenceRule(
'FREQ=MINUTELY;COUNT=5;BYSECOND=36,45',
new \DateTime('2013-06-12 16:00:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-06-12 16:00:36'), $computed[0]);
$this->assertEquals(new \DateTime('2013-06-12 16:00:45'), $computed[1]);
$this->assertEquals(new \DateTime('2013-06-12 16:01:36'), $computed[2]);
$this->assertEquals(new \DateTime('2013-06-12 16:01:45'), $computed[3]);
$this->assertEquals(new \DateTime('2013-06-12 16:02:36'), $computed[4]);
}
public function testByHour()
{
$rule = new RecurrenceRule(
'FREQ=HOURLY;COUNT=5;BYHOUR=14,15',
new \DateTime('2013-06-12 16:00:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-06-13 14:00:00'), $computed[0]);
$this->assertEquals(new \DateTime('2013-06-13 15:00:00'), $computed[1]);
$this->assertEquals(new \DateTime('2013-06-14 14:00:00'), $computed[2]);
$this->assertEquals(new \DateTime('2013-06-14 15:00:00'), $computed[3]);
$this->assertEquals(new \DateTime('2013-06-15 14:00:00'), $computed[4]);
}
public function testByMonth()
{
$rule = new RecurrenceRule(
'FREQ=DAILY;COUNT=4;BYMONTH=2,3',
new \DateTime('2013-02-26')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(4, count($computed));
$this->assertEquals(new \DateTime('2013-02-26'), $computed[0]);
$this->assertEquals(new \DateTime('2013-02-27'), $computed[1]);
$this->assertEquals(new \DateTime('2013-02-28'), $computed[2]);
$this->assertEquals(new \DateTime('2013-03-01'), $computed[3]);
}
public function testByMonthLeapYear()
{
$rule = new RecurrenceRule(
'FREQ=DAILY;COUNT=4;BYMONTH=2,3',
new \DateTime('2016-02-27')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(4, count($computed));
$this->assertEquals(new \DateTime('2016-02-27'), $computed[0]);
$this->assertEquals(new \DateTime('2016-02-28'), $computed[1]);
$this->assertEquals(new \DateTime('2016-02-29'), $computed[2]);
$this->assertEquals(new \DateTime('2016-03-01'), $computed[3]);
}
public function testLastDayOfMonth()
{
$rule = new RecurrenceRule(
'FREQ=MONTHLY;COUNT=5;BYMONTHDAY=28,29,30,31;BYSETPOS=-1',
new \DateTime('2016-01-29')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2016-01-31'), $computed[0]);
$this->assertEquals(new \DateTime('2016-02-29'), $computed[1]);
$this->assertEquals(new \DateTime('2016-03-31'), $computed[2]);
$this->assertEquals(new \DateTime('2016-04-30'), $computed[3]);
$this->assertEquals(new \DateTime('2016-05-31'), $computed[4]);
}
public function testByWeekNumber()
{
$rule = new RecurrenceRule(
'FREQ=YEARLY;COUNT=5;BYWEEKNO=22;WKST=SU',
new \DateTime('2013-05-30')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-05-30'), $computed[0]);
$this->assertEquals(new \DateTime('2013-05-31'), $computed[1]);
$this->assertEquals(new \DateTime('2013-06-01'), $computed[2]);
$this->assertEquals(new \DateTime('2014-05-25'), $computed[3]);
$this->assertEquals(new \DateTime('2014-05-26'), $computed[4]);
}
public function testByWeekNumberNegative()
{
$rule = new RecurrenceRule(
'FREQ=YEARLY;COUNT=5;BYWEEKNO=-44;WKST=TH',
new \DateTime('2013-05-30')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2014-02-27'), $computed[0]);
$this->assertEquals(new \DateTime('2014-02-28'), $computed[1]);
$this->assertEquals(new \DateTime('2014-03-01'), $computed[2]);
$this->assertEquals(new \DateTime('2014-03-02'), $computed[3]);
$this->assertEquals(new \DateTime('2014-03-03'), $computed[4]);
}
public function testByWeekNumberWeek53()
{
$rule = new RecurrenceRule(
'FREQ=YEARLY;COUNT=5;BYWEEKNO=53;WKST=MO',
new \DateTime('2013-05-30')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2015-12-28'), $computed[0]);
$this->assertEquals(new \DateTime('2015-12-29'), $computed[1]);
$this->assertEquals(new \DateTime('2015-12-30'), $computed[2]);
$this->assertEquals(new \DateTime('2015-12-31'), $computed[3]);
$this->assertEquals(new \DateTime('2016-01-01'), $computed[4]);
}
public function testByWeekNumberWeek53Negative()
{
$rule = new RecurrenceRule(
'FREQ=YEARLY;COUNT=5;BYWEEKNO=-53;WKST=MO',
new \DateTime('2008-05-30')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2009-01-01'), $computed[0]);
$this->assertEquals(new \DateTime('2009-01-02'), $computed[1]);
$this->assertEquals(new \DateTime('2009-01-03'), $computed[2]);
$this->assertEquals(new \DateTime('2009-01-04'), $computed[3]);
$this->assertEquals(new \DateTime('2015-01-01'), $computed[4]);
}
public function testByYearDay()
{
$rule = new RecurrenceRule(
'FREQ=YEARLY;COUNT=4;BYYEARDAY=125',
new \DateTime('2013-01-02')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(4, count($computed));
$this->assertEquals(new \DateTime('2013-05-05'), $computed[0]);
$this->assertEquals(new \DateTime('2014-05-05'), $computed[1]);
$this->assertEquals(new \DateTime('2015-05-05'), $computed[2]);
$this->assertEquals(new \DateTime('2016-05-04'), $computed[3]);
}
public function testByYearDayNegative()
{
$rule = new RecurrenceRule(
'FREQ=YEARLY;COUNT=4;BYYEARDAY=-307',
new \DateTime('2013-06-07')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(4, count($computed));
$this->assertEquals(new \DateTime('2014-02-28'), $computed[0]);
$this->assertEquals(new \DateTime('2015-02-28'), $computed[1]);
$this->assertEquals(new \DateTime('2016-02-29'), $computed[2]);
$this->assertEquals(new \DateTime('2017-02-28'), $computed[3]);
}
public function testByMonthDay()
{
$rule = new RecurrenceRule(
'FREQ=MONTHLY;COUNT=5;BYMONTHDAY=28,29,30',
new \DateTime('2013-01-30')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-01-30'), $computed[0]);
$this->assertEquals(new \DateTime('2013-02-28'), $computed[1]);
$this->assertEquals(new \DateTime('2013-03-28'), $computed[2]);
$this->assertEquals(new \DateTime('2013-03-29'), $computed[3]);
$this->assertEquals(new \DateTime('2013-03-30'), $computed[4]);
}
public function testByMonthDayLeapYear()
{
$rule = new RecurrenceRule(
'FREQ=MONTHLY;COUNT=5;BYMONTHDAY=28,29,30',
new \DateTime('2016-01-30')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2016-01-30'), $computed[0]);
$this->assertEquals(new \DateTime('2016-02-28'), $computed[1]);
$this->assertEquals(new \DateTime('2016-02-29'), $computed[2]);
$this->assertEquals(new \DateTime('2016-03-28'), $computed[3]);
$this->assertEquals(new \DateTime('2016-03-29'), $computed[4]);
}
public function testByMonthDayNegative()
{
$rule = new RecurrenceRule(
'FREQ=MONTHLY;COUNT=5;BYMONTHDAY=-10',
new \DateTime('2013-06-07')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-06-21'), $computed[0]);
$this->assertEquals(new \DateTime('2013-07-22'), $computed[1]);
$this->assertEquals(new \DateTime('2013-08-22'), $computed[2]);
$this->assertEquals(new \DateTime('2013-09-21'), $computed[3]);
$this->assertEquals(new \DateTime('2013-10-22'), $computed[4]);
}
public function testByMonthDayPositiveAndNegative()
{
$rule = new RecurrenceRule(
'FREQ=MONTHLY;COUNT=5;BYMONTHDAY=15,-1',
new \DateTime('2013-10-01')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-10-15'), $computed[0]);
$this->assertEquals(new \DateTime('2013-10-31'), $computed[1]);
$this->assertEquals(new \DateTime('2013-11-15'), $computed[2]);
$this->assertEquals(new \DateTime('2013-11-30'), $computed[3]);
$this->assertEquals(new \DateTime('2013-12-15'), $computed[4]);
}
public function testByWeekDay()
{
$rule = new RecurrenceRule(
'FREQ=WEEKLY;COUNT=5;BYDAY=MO,TU',
new \DateTime('2013-01-28')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-01-28'), $computed[0]);
$this->assertEquals(new \DateTime('2013-01-29'), $computed[1]);
$this->assertEquals(new \DateTime('2013-02-04'), $computed[2]);
$this->assertEquals(new \DateTime('2013-02-05'), $computed[3]);
$this->assertEquals(new \DateTime('2013-02-11'), $computed[4]);
}
public function testWeeklyByWeekDayWithInterval2()
{
$rule = new RecurrenceRule(
'FREQ=WEEKLY;COUNT=5;INTERVAL=2;BYDAY=TU,WE;WKST=WE',
new \DateTime('2013-01-23')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-01-23'), $computed[0]);
$this->assertEquals(new \DateTime('2013-01-29'), $computed[1]);
$this->assertEquals(new \DateTime('2013-02-06'), $computed[2]);
$this->assertEquals(new \DateTime('2013-02-12'), $computed[3]);
$this->assertEquals(new \DateTime('2013-02-20'), $computed[4]);
// --------------------------------
$rule = new RecurrenceRule(
'FREQ=WEEKLY;COUNT=5;INTERVAL=2;BYDAY=TU,WE;WKST=SA',
new \DateTime('2013-01-24')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-02-05'), $computed[0]);
$this->assertEquals(new \DateTime('2013-02-06'), $computed[1]);
$this->assertEquals(new \DateTime('2013-02-19'), $computed[2]);
$this->assertEquals(new \DateTime('2013-02-20'), $computed[3]);
$this->assertEquals(new \DateTime('2013-03-05'), $computed[4]);
}
public function testMonthlyByWeekDayRelativeFromStart()
{
$rule = new RecurrenceRule(
'FREQ=MONTHLY;COUNT=5;BYDAY=+1MO',
new \DateTime('2013-06-04')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-07-01'), $computed[0]);
$this->assertEquals(new \DateTime('2013-08-05'), $computed[1]);
$this->assertEquals(new \DateTime('2013-09-02'), $computed[2]);
$this->assertEquals(new \DateTime('2013-10-07'), $computed[3]);
$this->assertEquals(new \DateTime('2013-11-04'), $computed[4]);
}
public function testMonthlyByWeekDayRelativeFromEnd()
{
$rule = new RecurrenceRule(
'FREQ=MONTHLY;COUNT=5;BYDAY=-1MO',
new \DateTime('2013-06-04')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-06-24'), $computed[0]);
$this->assertEquals(new \DateTime('2013-07-29'), $computed[1]);
$this->assertEquals(new \DateTime('2013-08-26'), $computed[2]);
$this->assertEquals(new \DateTime('2013-09-30'), $computed[3]);
$this->assertEquals(new \DateTime('2013-10-28'), $computed[4]);
// -----------------------
$rule = new RecurrenceRule(
'FREQ=MONTHLY;COUNT=5;BYDAY=-5MO',
new \DateTime('2013-06-05')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-07-01'), $computed[0]);
$this->assertEquals(new \DateTime('2013-09-02'), $computed[1]);
$this->assertEquals(new \DateTime('2013-12-02'), $computed[2]);
$this->assertEquals(new \DateTime('2014-03-03'), $computed[3]);
$this->assertEquals(new \DateTime('2014-06-02'), $computed[4]);
}
public function testYearlyByWeekDayRelativeFromStart()
{
$rule = new RecurrenceRule(
'FREQ=YEARLY;COUNT=5;BYDAY=+1MO',
new \DateTime('2013-06-04')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2014-01-06'), $computed[0]);
$this->assertEquals(new \DateTime('2015-01-05'), $computed[1]);
$this->assertEquals(new \DateTime('2016-01-04'), $computed[2]);
$this->assertEquals(new \DateTime('2017-01-02'), $computed[3]);
$this->assertEquals(new \DateTime('2018-01-01'), $computed[4]);
}
public function testYearlyByWeekDayRelativeFromEnd()
{
$rule = new RecurrenceRule(
'FREQ=YEARLY;COUNT=5;BYDAY=-1MO',
new \DateTime('2013-06-04')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-12-30'), $computed[0]);
$this->assertEquals(new \DateTime('2014-12-29'), $computed[1]);
$this->assertEquals(new \DateTime('2015-12-28'), $computed[2]);
$this->assertEquals(new \DateTime('2016-12-26'), $computed[3]);
$this->assertEquals(new \DateTime('2017-12-25'), $computed[4]);
}
public function testMonthlyByWeekDayRelativeFromStartAndEnd()
{
$rule = new RecurrenceRule(
'FREQ=MONTHLY;COUNT=10;BYDAY=-1MO,3FR',
new \DateTime('2013-06-04')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(10, count($computed));
$this->assertEquals(new \DateTime('2013-06-21'), $computed[0]);
$this->assertEquals(new \DateTime('2013-06-24'), $computed[1]);
$this->assertEquals(new \DateTime('2013-07-19'), $computed[2]);
$this->assertEquals(new \DateTime('2013-07-29'), $computed[3]);
$this->assertEquals(new \DateTime('2013-08-16'), $computed[4]);
$this->assertEquals(new \DateTime('2013-08-26'), $computed[5]);
$this->assertEquals(new \DateTime('2013-09-20'), $computed[6]);
$this->assertEquals(new \DateTime('2013-09-30'), $computed[7]);
$this->assertEquals(new \DateTime('2013-10-18'), $computed[8]);
$this->assertEquals(new \DateTime('2013-10-28'), $computed[9]);
}
public function testMonthlyByWeekDayWithSundayStartOfYear()
{
$rule = new RecurrenceRule(
'FREQ=MONTHLY;COUNT=6;BYDAY=MO',
new \DateTime('2017-01-01')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(6, count($computed));
$this->assertEquals(new \DateTime('2017-01-02'), $computed[0]);
$this->assertEquals(new \DateTime('2017-01-09'), $computed[1]);
$this->assertEquals(new \DateTime('2017-01-16'), $computed[2]);
$this->assertEquals(new \DateTime('2017-01-23'), $computed[3]);
$this->assertEquals(new \DateTime('2017-01-30'), $computed[4]);
$this->assertEquals(new \DateTime('2017-02-06'), $computed[5]);
// -----------------------------------
$rule = new RecurrenceRule(
'FREQ=MONTHLY;COUNT=6;BYDAY=-3MO,3FR',
new \DateTime('2017-01-01')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(6, count($computed));
$this->assertEquals(new \DateTime('2017-01-16'), $computed[0]);
$this->assertEquals(new \DateTime('2017-01-20'), $computed[1]);
$this->assertEquals(new \DateTime('2017-02-13'), $computed[2]);
$this->assertEquals(new \DateTime('2017-02-17'), $computed[3]);
$this->assertEquals(new \DateTime('2017-03-13'), $computed[4]);
$this->assertEquals(new \DateTime('2017-03-17'), $computed[5]);
}
public function testBySetPosition()
{
$rule = new RecurrenceRule(
'FREQ=MONTHLY;BYSETPOS=-1;BYDAY=MO,TU,WE,TH,FR;COUNT=5',
new \DateTime('2013-01-24')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-01-31'), $computed[0]);
$this->assertEquals(new \DateTime('2013-02-28'), $computed[1]);
$this->assertEquals(new \DateTime('2013-03-29'), $computed[2]);
$this->assertEquals(new \DateTime('2013-04-30'), $computed[3]);
$this->assertEquals(new \DateTime('2013-05-31'), $computed[4]);
// --------------------------------------
$rule = new RecurrenceRule(
'FREQ=MONTHLY;BYSETPOS=-1;BYDAY=MO,TU,WE,TH,FR;COUNT=5',
new \DateTime('2016-01-24')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2016-01-29'), $computed[0]);
$this->assertEquals(new \DateTime('2016-02-29'), $computed[1]);
$this->assertEquals(new \DateTime('2016-03-31'), $computed[2]);
$this->assertEquals(new \DateTime('2016-04-29'), $computed[3]);
$this->assertEquals(new \DateTime('2016-05-31'), $computed[4]);
// --------------------------------------
$rule = new RecurrenceRule(
'FREQ=MONTHLY;BYSETPOS=1,-1;BYDAY=MO,TU,WE,TH,FR;COUNT=5',
new \DateTime('2016-01-24')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2016-01-29'), $computed[0]);
$this->assertEquals(new \DateTime('2016-02-01'), $computed[1]);
$this->assertEquals(new \DateTime('2016-02-29'), $computed[2]);
$this->assertEquals(new \DateTime('2016-03-01'), $computed[3]);
$this->assertEquals(new \DateTime('2016-03-31'), $computed[4]);
}
public function testBySetPositionWithInterval()
{
$rule = new RecurrenceRule(
'FREQ=MONTHLY;INTERVAL=2;BYDAY=MO;BYSETPOS=2;COUNT=10',
new \DateTime('2013-10-09')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(10, count($computed));
$this->assertEquals(new \DateTime('2013-10-14'), $computed[0]);
$this->assertEquals(new \DateTime('2013-12-09'), $computed[1]);
$this->assertEquals(new \DateTime('2014-02-10'), $computed[2]);
$this->assertEquals(new \DateTime('2014-04-14'), $computed[3]);
$this->assertEquals(new \DateTime('2014-06-09'), $computed[4]);
$this->assertEquals(new \DateTime('2014-08-11'), $computed[5]);
$this->assertEquals(new \DateTime('2014-10-13'), $computed[6]);
$this->assertEquals(new \DateTime('2014-12-08'), $computed[7]);
$this->assertEquals(new \DateTime('2015-02-09'), $computed[8]);
$this->assertEquals(new \DateTime('2015-04-13'), $computed[9]);
}
public function testRfc2445Example()
{
$rule = new RecurrenceRule(
'FREQ=YEARLY;INTERVAL=2;BYMONTH=1;BYDAY=SU;BYHOUR=8,9;COUNT=30',
new \DateTime('1997-01-05 08:30:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(30, count($computed));
$this->assertEquals(new \DateTime('1997-01-05 08:30:00'), $computed[0]);
$this->assertEquals(new \DateTime('1997-01-05 09:30:00'), $computed[1]);
$this->assertEquals(new \DateTime('1997-01-12 08:30:00'), $computed[2]);
$this->assertEquals(new \DateTime('1997-01-12 09:30:00'), $computed[3]);
$this->assertEquals(new \DateTime('1997-01-19 08:30:00'), $computed[4]);
$this->assertEquals(new \DateTime('1997-01-19 09:30:00'), $computed[5]);
$this->assertEquals(new \DateTime('1997-01-26 08:30:00'), $computed[6]);
$this->assertEquals(new \DateTime('1997-01-26 09:30:00'), $computed[7]);
$this->assertEquals(new \DateTime('1999-01-03 08:30:00'), $computed[8]);
$this->assertEquals(new \DateTime('1999-01-03 09:30:00'), $computed[9]);
$this->assertEquals(new \DateTime('1999-01-10 08:30:00'), $computed[10]);
$this->assertEquals(new \DateTime('1999-01-10 09:30:00'), $computed[11]);
$this->assertEquals(new \DateTime('1999-01-17 08:30:00'), $computed[12]);
$this->assertEquals(new \DateTime('1999-01-17 09:30:00'), $computed[13]);
$this->assertEquals(new \DateTime('1999-01-24 08:30:00'), $computed[14]);
$this->assertEquals(new \DateTime('1999-01-24 09:30:00'), $computed[15]);
$this->assertEquals(new \DateTime('1999-01-31 08:30:00'), $computed[16]);
$this->assertEquals(new \DateTime('1999-01-31 09:30:00'), $computed[17]);
$this->assertEquals(new \DateTime('2001-01-07 08:30:00'), $computed[18]);
$this->assertEquals(new \DateTime('2001-01-07 09:30:00'), $computed[19]);
$this->assertEquals(new \DateTime('2001-01-14 08:30:00'), $computed[20]);
$this->assertEquals(new \DateTime('2001-01-14 09:30:00'), $computed[21]);
$this->assertEquals(new \DateTime('2001-01-21 08:30:00'), $computed[22]);
$this->assertEquals(new \DateTime('2001-01-21 09:30:00'), $computed[23]);
$this->assertEquals(new \DateTime('2001-01-28 08:30:00'), $computed[24]);
$this->assertEquals(new \DateTime('2001-01-28 09:30:00'), $computed[25]);
$this->assertEquals(new \DateTime('2003-01-05 08:30:00'), $computed[26]);
$this->assertEquals(new \DateTime('2003-01-05 09:30:00'), $computed[27]);
$this->assertEquals(new \DateTime('2003-01-12 08:30:00'), $computed[28]);
$this->assertEquals(new \DateTime('2003-01-12 09:30:00'), $computed[29]);
}
}

View File

@ -2,18 +2,19 @@
namespace Recurr\Test;
use Recurr\RecurrenceRule;
use Recurr\Frequency;
use Recurr\Rule;
use Recurr\Exception\InvalidArgument;
use Recurr\Exception\InvalidRRule;
class RecurrenceRuleTest extends \PHPUnit_Framework_TestCase
class RuleTest extends \PHPUnit_Framework_TestCase
{
/** @var RecurrenceRule */
/** @var Rule */
protected $rule;
public function setUp()
{
$this->rule = new RecurrenceRule;
$this->rule = new Rule;
}
/**
@ -50,7 +51,7 @@ class RecurrenceRuleTest extends \PHPUnit_Framework_TestCase
$this->rule->createFromString($string);
$this->assertEquals(RecurrenceRule::FREQ_YEARLY, $this->rule->getFreq());
$this->assertEquals(Frequency::YEARLY, $this->rule->getFreq());
$this->assertEquals(2, $this->rule->getCount());
$this->assertEquals(2, $this->rule->getInterval());
$this->assertEquals(array(30), $this->rule->getBySecond());

View File

@ -0,0 +1,18 @@
<?php
namespace Recurr\Test;
use Recurr\RuleTransformer;
class RuleTransformerBase extends \PHPUnit_Framework_TestCase
{
/** @var RuleTransformer */
protected $transformer;
protected $timezone = 'America/New_York';
public function setUp()
{
$this->transformer = new RuleTransformer;
}
}

View File

@ -0,0 +1,70 @@
<?php
namespace Recurr\Test;
use Recurr\Rule;
class RuleTransformerByDayTest extends RuleTransformerBase
{
public function testByDayWeekly()
{
$rule = new Rule(
'FREQ=WEEKLY;COUNT=10;INTERVAL=2;BYDAY=MO,WE,FR',
new \DateTime('1997-09-02 16:00:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(10, count($computed));
$this->assertEquals(new \DateTime('1997-09-03 16:00:00'), $computed[0]);
$this->assertEquals(new \DateTime('1997-09-05 16:00:00'), $computed[1]);
$this->assertEquals(new \DateTime('1997-09-15 16:00:00'), $computed[2]);
$this->assertEquals(new \DateTime('1997-09-17 16:00:00'), $computed[3]);
$this->assertEquals(new \DateTime('1997-09-19 16:00:00'), $computed[4]);
$this->assertEquals(new \DateTime('1997-09-29 16:00:00'), $computed[5]);
$this->assertEquals(new \DateTime('1997-10-01 16:00:00'), $computed[6]);
$this->assertEquals(new \DateTime('1997-10-03 16:00:00'), $computed[7]);
$this->assertEquals(new \DateTime('1997-10-13 16:00:00'), $computed[8]);
$this->assertEquals(new \DateTime('1997-10-15 16:00:00'), $computed[9]);
}
public function testByDayMonthly()
{
$rule = new Rule(
'FREQ=MONTHLY;COUNT=10;BYDAY=WE,TH',
new \DateTime('2014-01-14 16:00:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(10, count($computed));
$this->assertEquals(new \DateTime('2014-01-15 16:00:00'), $computed[0]);
$this->assertEquals(new \DateTime('2014-01-16 16:00:00'), $computed[1]);
$this->assertEquals(new \DateTime('2014-01-22 16:00:00'), $computed[2]);
$this->assertEquals(new \DateTime('2014-01-23 16:00:00'), $computed[3]);
$this->assertEquals(new \DateTime('2014-01-29 16:00:00'), $computed[4]);
$this->assertEquals(new \DateTime('2014-01-30 16:00:00'), $computed[5]);
$this->assertEquals(new \DateTime('2014-02-05 16:00:00'), $computed[6]);
$this->assertEquals(new \DateTime('2014-02-06 16:00:00'), $computed[7]);
$this->assertEquals(new \DateTime('2014-02-12 16:00:00'), $computed[8]);
$this->assertEquals(new \DateTime('2014-02-13 16:00:00'), $computed[9]);
}
public function testByDayYearly()
{
$rule = new Rule(
'FREQ=YEARLY;COUNT=3;BYDAY=20MO',
new \DateTime('1997-05-19 16:00:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(3, count($computed));
$this->assertEquals(new \DateTime('1997-05-19 16:00:00'), $computed[0]);
$this->assertEquals(new \DateTime('1998-05-18 16:00:00'), $computed[1]);
$this->assertEquals(new \DateTime('1999-05-17 16:00:00'), $computed[2]);
}
}

View File

@ -0,0 +1,134 @@
<?php
namespace Recurr\Test;
use Recurr\Rule;
class RuleTransformerByHourTest extends RuleTransformerBase
{
public function testByHourHourly()
{
$rule = new Rule(
'FREQ=HOURLY;COUNT=5;BYHOUR=14,15',
new \DateTime('2013-06-12 16:00:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-06-13 14:00:00'), $computed[0]);
$this->assertEquals(new \DateTime('2013-06-13 15:00:00'), $computed[1]);
$this->assertEquals(new \DateTime('2013-06-14 14:00:00'), $computed[2]);
$this->assertEquals(new \DateTime('2013-06-14 15:00:00'), $computed[3]);
$this->assertEquals(new \DateTime('2013-06-15 14:00:00'), $computed[4]);
}
public function testByHourDaily()
{
$rule = new Rule(
'FREQ=DAILY;COUNT=5;BYHOUR=14,15',
new \DateTime('2013-06-12 16:00:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-06-13 14:00:00'), $computed[0]);
$this->assertEquals(new \DateTime('2013-06-13 15:00:00'), $computed[1]);
$this->assertEquals(new \DateTime('2013-06-14 14:00:00'), $computed[2]);
$this->assertEquals(new \DateTime('2013-06-14 15:00:00'), $computed[3]);
$this->assertEquals(new \DateTime('2013-06-15 14:00:00'), $computed[4]);
}
public function testByHourWeekly()
{
$rule = new Rule(
'FREQ=WEEKLY;COUNT=5;BYHOUR=14,15',
new \DateTime('2013-06-12 16:00:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-06-19 14:00:00'), $computed[0]);
$this->assertEquals(new \DateTime('2013-06-19 15:00:00'), $computed[1]);
$this->assertEquals(new \DateTime('2013-06-26 14:00:00'), $computed[2]);
$this->assertEquals(new \DateTime('2013-06-26 15:00:00'), $computed[3]);
$this->assertEquals(new \DateTime('2013-07-03 14:00:00'), $computed[4]);
}
public function testByHourMonthly()
{
$rule = new Rule(
'FREQ=MONTHLY;COUNT=5;BYHOUR=14,15',
new \DateTime('2013-06-12 16:00:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-07-12 14:00:00'), $computed[0]);
$this->assertEquals(new \DateTime('2013-07-12 15:00:00'), $computed[1]);
$this->assertEquals(new \DateTime('2013-08-12 14:00:00'), $computed[2]);
$this->assertEquals(new \DateTime('2013-08-12 15:00:00'), $computed[3]);
$this->assertEquals(new \DateTime('2013-09-12 14:00:00'), $computed[4]);
}
public function testByHourMonthlyLeapYear()
{
$rule = new Rule(
'FREQ=MONTHLY;COUNT=5;BYHOUR=14,15',
new \DateTime('2016-01-29 12:00:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2016-01-29 14:00:00'), $computed[0]);
$this->assertEquals(new \DateTime('2016-01-29 15:00:00'), $computed[1]);
$this->assertEquals(new \DateTime('2016-02-29 14:00:00'), $computed[2]);
$this->assertEquals(new \DateTime('2016-02-29 15:00:00'), $computed[3]);
$this->assertEquals(new \DateTime('2016-03-29 14:00:00'), $computed[4]);
}
public function testByHourYearly()
{
$rule = new Rule(
'FREQ=YEARLY;COUNT=5;BYHOUR=14,15',
new \DateTime('2013-06-12 16:00:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2014-06-12 14:00:00'), $computed[0]);
$this->assertEquals(new \DateTime('2014-06-12 15:00:00'), $computed[1]);
$this->assertEquals(new \DateTime('2015-06-12 14:00:00'), $computed[2]);
$this->assertEquals(new \DateTime('2015-06-12 15:00:00'), $computed[3]);
$this->assertEquals(new \DateTime('2016-06-12 14:00:00'), $computed[4]);
}
public function testByHourYearlyLeapYear()
{
$rule = new Rule(
'FREQ=YEARLY;COUNT=5;BYHOUR=14,15',
new \DateTime('2016-02-29 12:00:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2016-02-29 14:00:00'), $computed[0]);
$this->assertEquals(new \DateTime('2016-02-29 15:00:00'), $computed[1]);
$this->assertEquals(new \DateTime('2017-02-28 14:00:00'), $computed[2]);
$this->assertEquals(new \DateTime('2017-02-28 15:00:00'), $computed[3]);
$this->assertEquals(new \DateTime('2018-02-28 14:00:00'), $computed[4]);
}
}

View File

@ -0,0 +1,116 @@
<?php
namespace Recurr\Test;
use Recurr\Rule;
class RuleTransformerByMinuteTest extends RuleTransformerBase
{
public function testByMinuteMinutely()
{
$rule = new Rule(
'FREQ=MINUTELY;COUNT=5;BYMINUTE=14,15',
new \DateTime('2013-06-12 16:00:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-06-12 16:14:00'), $computed[0]);
$this->assertEquals(new \DateTime('2013-06-12 16:15:00'), $computed[1]);
$this->assertEquals(new \DateTime('2013-06-12 17:14:00'), $computed[2]);
$this->assertEquals(new \DateTime('2013-06-12 17:15:00'), $computed[3]);
$this->assertEquals(new \DateTime('2013-06-12 18:14:00'), $computed[4]);
}
public function testByMinuteHourly()
{
$rule = new Rule(
'FREQ=HOURLY;COUNT=5;BYMINUTE=14,15',
new \DateTime('2013-06-12 16:00:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-06-12 16:14:00'), $computed[0]);
$this->assertEquals(new \DateTime('2013-06-12 16:15:00'), $computed[1]);
$this->assertEquals(new \DateTime('2013-06-12 17:14:00'), $computed[2]);
$this->assertEquals(new \DateTime('2013-06-12 17:15:00'), $computed[3]);
$this->assertEquals(new \DateTime('2013-06-12 18:14:00'), $computed[4]);
}
public function testByMinuteDaily()
{
$rule = new Rule(
'FREQ=DAILY;COUNT=5;BYMINUTE=14,15',
new \DateTime('2013-06-12 16:00:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-06-12 16:14:00'), $computed[0]);
$this->assertEquals(new \DateTime('2013-06-12 16:15:00'), $computed[1]);
$this->assertEquals(new \DateTime('2013-06-13 16:14:00'), $computed[2]);
$this->assertEquals(new \DateTime('2013-06-13 16:15:00'), $computed[3]);
$this->assertEquals(new \DateTime('2013-06-14 16:14:00'), $computed[4]);
}
public function testByMinuteWeekly()
{
$rule = new Rule(
'FREQ=WEEKLY;COUNT=5;BYMINUTE=14,15',
new \DateTime('2013-06-12 16:00:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-06-12 16:14:00'), $computed[0]);
$this->assertEquals(new \DateTime('2013-06-12 16:15:00'), $computed[1]);
$this->assertEquals(new \DateTime('2013-06-19 16:14:00'), $computed[2]);
$this->assertEquals(new \DateTime('2013-06-19 16:15:00'), $computed[3]);
$this->assertEquals(new \DateTime('2013-06-26 16:14:00'), $computed[4]);
}
public function testByMinuteMonthly()
{
$rule = new Rule(
'FREQ=MONTHLY;COUNT=5;BYMINUTE=14,15',
new \DateTime('2013-06-12 16:00:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-06-12 16:14:00'), $computed[0]);
$this->assertEquals(new \DateTime('2013-06-12 16:15:00'), $computed[1]);
$this->assertEquals(new \DateTime('2013-07-12 16:14:00'), $computed[2]);
$this->assertEquals(new \DateTime('2013-07-12 16:15:00'), $computed[3]);
$this->assertEquals(new \DateTime('2013-08-12 16:14:00'), $computed[4]);
}
public function testByMinuteYearly()
{
$rule = new Rule(
'FREQ=YEARLY;COUNT=5;BYMINUTE=14,15',
new \DateTime('2013-06-12 16:00:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-06-12 16:14:00'), $computed[0]);
$this->assertEquals(new \DateTime('2013-06-12 16:15:00'), $computed[1]);
$this->assertEquals(new \DateTime('2014-06-12 16:14:00'), $computed[2]);
$this->assertEquals(new \DateTime('2014-06-12 16:15:00'), $computed[3]);
$this->assertEquals(new \DateTime('2015-06-12 16:14:00'), $computed[4]);
}
}

View File

@ -0,0 +1,96 @@
<?php
namespace Recurr\Test;
use Recurr\Rule;
class RuleTransformerByMonthDayTest extends RuleTransformerBase
{
public function testByMonthDayMonthly()
{
$rule = new Rule(
'FREQ=MONTHLY;COUNT=5;BYMONTHDAY=28,29,30',
new \DateTime('2013-01-30')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-01-30'), $computed[0]);
$this->assertEquals(new \DateTime('2013-02-28'), $computed[1]);
$this->assertEquals(new \DateTime('2013-03-28'), $computed[2]);
$this->assertEquals(new \DateTime('2013-03-29'), $computed[3]);
$this->assertEquals(new \DateTime('2013-03-30'), $computed[4]);
}
public function testByMonthDayMonthlyNegative()
{
$rule = new Rule(
'FREQ=MONTHLY;COUNT=5;BYMONTHDAY=-10',
new \DateTime('2013-06-07')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-06-21'), $computed[0]);
$this->assertEquals(new \DateTime('2013-07-22'), $computed[1]);
$this->assertEquals(new \DateTime('2013-08-22'), $computed[2]);
$this->assertEquals(new \DateTime('2013-09-21'), $computed[3]);
$this->assertEquals(new \DateTime('2013-10-22'), $computed[4]);
}
public function testByMonthDayMonthlyPositiveAndNegative()
{
$rule = new Rule(
'FREQ=MONTHLY;COUNT=5;BYMONTHDAY=15,-1',
new \DateTime('2013-10-01')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-10-15'), $computed[0]);
$this->assertEquals(new \DateTime('2013-10-31'), $computed[1]);
$this->assertEquals(new \DateTime('2013-11-15'), $computed[2]);
$this->assertEquals(new \DateTime('2013-11-30'), $computed[3]);
$this->assertEquals(new \DateTime('2013-12-15'), $computed[4]);
}
public function testByMonthDayMonthlyLeapYear()
{
$rule = new Rule(
'FREQ=MONTHLY;COUNT=5;BYMONTHDAY=28,29,30',
new \DateTime('2016-01-30')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2016-01-30'), $computed[0]);
$this->assertEquals(new \DateTime('2016-02-28'), $computed[1]);
$this->assertEquals(new \DateTime('2016-02-29'), $computed[2]);
$this->assertEquals(new \DateTime('2016-03-28'), $computed[3]);
$this->assertEquals(new \DateTime('2016-03-29'), $computed[4]);
}
public function testByMonthDayYearlyWithByMonthAndByDay()
{
$rule = new Rule(
'FREQ=YEARLY;COUNT=3;INTERVAL=4;BYMONTH=11;BYDAY=TU;BYMONTHDAY=2,3,4,5,6,7,8',
new \DateTime('1996-11-05 09:00:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(3, count($computed));
$this->assertEquals(new \DateTime('1996-11-05 09:00:00'), $computed[0]);
$this->assertEquals(new \DateTime('2000-11-07 09:00:00'), $computed[1]);
$this->assertEquals(new \DateTime('2004-11-02 09:00:00'), $computed[2]);
}
}

View File

@ -0,0 +1,42 @@
<?php
namespace Recurr\Test;
use Recurr\Rule;
class RuleTransformerByMonthTest extends RuleTransformerBase
{
public function testByMonth()
{
$rule = new Rule(
'FREQ=DAILY;COUNT=4;BYMONTH=2,3',
new \DateTime('2013-02-26')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(4, count($computed));
$this->assertEquals(new \DateTime('2013-02-26'), $computed[0]);
$this->assertEquals(new \DateTime('2013-02-27'), $computed[1]);
$this->assertEquals(new \DateTime('2013-02-28'), $computed[2]);
$this->assertEquals(new \DateTime('2013-03-01'), $computed[3]);
}
public function testByMonthLeapYear()
{
$rule = new Rule(
'FREQ=DAILY;COUNT=4;BYMONTH=2,3',
new \DateTime('2016-02-27')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(4, count($computed));
$this->assertEquals(new \DateTime('2016-02-27'), $computed[0]);
$this->assertEquals(new \DateTime('2016-02-28'), $computed[1]);
$this->assertEquals(new \DateTime('2016-02-29'), $computed[2]);
$this->assertEquals(new \DateTime('2016-03-01'), $computed[3]);
}
}

View File

@ -0,0 +1,134 @@
<?php
namespace Recurr\Test;
use Recurr\Rule;
class RuleTransformerBySecondTest extends RuleTransformerBase
{
public function testBySecondSecondly()
{
$rule = new Rule(
'FREQ=SECONDLY;COUNT=5;BYSECOND=36,45',
new \DateTime('2013-06-12 16:00:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-06-12 16:00:36'), $computed[0]);
$this->assertEquals(new \DateTime('2013-06-12 16:00:45'), $computed[1]);
$this->assertEquals(new \DateTime('2013-06-12 16:01:36'), $computed[2]);
$this->assertEquals(new \DateTime('2013-06-12 16:01:45'), $computed[3]);
$this->assertEquals(new \DateTime('2013-06-12 16:02:36'), $computed[4]);
}
public function testBySecondMinutely()
{
$rule = new Rule(
'FREQ=MINUTELY;COUNT=5;BYSECOND=36,45',
new \DateTime('2013-06-12 16:00:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-06-12 16:00:36'), $computed[0]);
$this->assertEquals(new \DateTime('2013-06-12 16:00:45'), $computed[1]);
$this->assertEquals(new \DateTime('2013-06-12 16:01:36'), $computed[2]);
$this->assertEquals(new \DateTime('2013-06-12 16:01:45'), $computed[3]);
$this->assertEquals(new \DateTime('2013-06-12 16:02:36'), $computed[4]);
}
public function testBySecondHourly()
{
$rule = new Rule(
'FREQ=HOURLY;COUNT=5;BYSECOND=36,45',
new \DateTime('2013-06-12 16:00:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-06-12 16:00:36'), $computed[0]);
$this->assertEquals(new \DateTime('2013-06-12 16:00:45'), $computed[1]);
$this->assertEquals(new \DateTime('2013-06-12 17:00:36'), $computed[2]);
$this->assertEquals(new \DateTime('2013-06-12 17:00:45'), $computed[3]);
$this->assertEquals(new \DateTime('2013-06-12 18:00:36'), $computed[4]);
}
public function testBySecondDaily()
{
$rule = new Rule(
'FREQ=DAILY;COUNT=5;BYSECOND=36,45',
new \DateTime('2013-06-12 16:00:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-06-12 16:00:36'), $computed[0]);
$this->assertEquals(new \DateTime('2013-06-12 16:00:45'), $computed[1]);
$this->assertEquals(new \DateTime('2013-06-13 16:00:36'), $computed[2]);
$this->assertEquals(new \DateTime('2013-06-13 16:00:45'), $computed[3]);
$this->assertEquals(new \DateTime('2013-06-14 16:00:36'), $computed[4]);
}
public function testBySecondWeekly()
{
$rule = new Rule(
'FREQ=WEEKLY;COUNT=5;BYSECOND=36,45',
new \DateTime('2013-06-12 16:00:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-06-12 16:00:36'), $computed[0]);
$this->assertEquals(new \DateTime('2013-06-12 16:00:45'), $computed[1]);
$this->assertEquals(new \DateTime('2013-06-19 16:00:36'), $computed[2]);
$this->assertEquals(new \DateTime('2013-06-19 16:00:45'), $computed[3]);
$this->assertEquals(new \DateTime('2013-06-26 16:00:36'), $computed[4]);
}
public function testBySecondMonthly()
{
$rule = new Rule(
'FREQ=MONTHLY;COUNT=5;BYSECOND=36,45',
new \DateTime('2013-06-12 16:00:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-06-12 16:00:36'), $computed[0]);
$this->assertEquals(new \DateTime('2013-06-12 16:00:45'), $computed[1]);
$this->assertEquals(new \DateTime('2013-07-12 16:00:36'), $computed[2]);
$this->assertEquals(new \DateTime('2013-07-12 16:00:45'), $computed[3]);
$this->assertEquals(new \DateTime('2013-08-12 16:00:36'), $computed[4]);
}
public function testBySecondYearly()
{
$rule = new Rule(
'FREQ=YEARLY;COUNT=5;BYSECOND=36,45',
new \DateTime('2013-06-12 16:00:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-06-12 16:00:36'), $computed[0]);
$this->assertEquals(new \DateTime('2013-06-12 16:00:45'), $computed[1]);
$this->assertEquals(new \DateTime('2014-06-12 16:00:36'), $computed[2]);
$this->assertEquals(new \DateTime('2014-06-12 16:00:45'), $computed[3]);
$this->assertEquals(new \DateTime('2015-06-12 16:00:36'), $computed[4]);
}
}

View File

@ -0,0 +1,83 @@
<?php
namespace Recurr\Test;
use Recurr\Rule;
class RuleTransformerBySetPositionTest extends RuleTransformerBase
{
public function testBySetPosition()
{
$rule = new Rule(
'FREQ=MONTHLY;BYSETPOS=-1;BYDAY=MO,TU,WE,TH,FR;COUNT=5',
new \DateTime('2013-01-24')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-01-31'), $computed[0]);
$this->assertEquals(new \DateTime('2013-02-28'), $computed[1]);
$this->assertEquals(new \DateTime('2013-03-29'), $computed[2]);
$this->assertEquals(new \DateTime('2013-04-30'), $computed[3]);
$this->assertEquals(new \DateTime('2013-05-31'), $computed[4]);
// --------------------------------------
$rule = new Rule(
'FREQ=MONTHLY;BYSETPOS=-1;BYDAY=MO,TU,WE,TH,FR;COUNT=5',
new \DateTime('2016-01-24')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2016-01-29'), $computed[0]);
$this->assertEquals(new \DateTime('2016-02-29'), $computed[1]);
$this->assertEquals(new \DateTime('2016-03-31'), $computed[2]);
$this->assertEquals(new \DateTime('2016-04-29'), $computed[3]);
$this->assertEquals(new \DateTime('2016-05-31'), $computed[4]);
// --------------------------------------
$rule = new Rule(
'FREQ=MONTHLY;BYSETPOS=1,-1;BYDAY=MO,TU,WE,TH,FR;COUNT=5',
new \DateTime('2016-01-24')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2016-01-29'), $computed[0]);
$this->assertEquals(new \DateTime('2016-02-01'), $computed[1]);
$this->assertEquals(new \DateTime('2016-02-29'), $computed[2]);
$this->assertEquals(new \DateTime('2016-03-01'), $computed[3]);
$this->assertEquals(new \DateTime('2016-03-31'), $computed[4]);
}
public function testBySetPositionWithInterval()
{
$rule = new Rule(
'FREQ=MONTHLY;INTERVAL=2;BYDAY=MO;BYSETPOS=2;COUNT=10',
new \DateTime('2013-10-09')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(10, count($computed));
$this->assertEquals(new \DateTime('2013-10-14'), $computed[0]);
$this->assertEquals(new \DateTime('2013-12-09'), $computed[1]);
$this->assertEquals(new \DateTime('2014-02-10'), $computed[2]);
$this->assertEquals(new \DateTime('2014-04-14'), $computed[3]);
$this->assertEquals(new \DateTime('2014-06-09'), $computed[4]);
$this->assertEquals(new \DateTime('2014-08-11'), $computed[5]);
$this->assertEquals(new \DateTime('2014-10-13'), $computed[6]);
$this->assertEquals(new \DateTime('2014-12-08'), $computed[7]);
$this->assertEquals(new \DateTime('2015-02-09'), $computed[8]);
$this->assertEquals(new \DateTime('2015-04-13'), $computed[9]);
}
}

View File

@ -0,0 +1,210 @@
<?php
namespace Recurr\Test;
use Recurr\Rule;
class RuleTransformerByWeekDayTest extends RuleTransformerBase
{
public function testByWeekDay()
{
$rule = new Rule(
'FREQ=WEEKLY;COUNT=5;BYDAY=MO,TU',
new \DateTime('2013-01-28')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-01-28'), $computed[0]);
$this->assertEquals(new \DateTime('2013-01-29'), $computed[1]);
$this->assertEquals(new \DateTime('2013-02-04'), $computed[2]);
$this->assertEquals(new \DateTime('2013-02-05'), $computed[3]);
$this->assertEquals(new \DateTime('2013-02-11'), $computed[4]);
}
public function testByWeekDayWeeklyWithInterval2()
{
$rule = new Rule(
'FREQ=WEEKLY;COUNT=5;INTERVAL=2;BYDAY=TU,WE;WKST=WE',
new \DateTime('2013-01-23')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-01-23'), $computed[0]);
$this->assertEquals(new \DateTime('2013-01-29'), $computed[1]);
$this->assertEquals(new \DateTime('2013-02-06'), $computed[2]);
$this->assertEquals(new \DateTime('2013-02-12'), $computed[3]);
$this->assertEquals(new \DateTime('2013-02-20'), $computed[4]);
// --------------------------------
$rule = new Rule(
'FREQ=WEEKLY;COUNT=5;INTERVAL=2;BYDAY=TU,WE;WKST=SA',
new \DateTime('2013-01-24')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-02-05'), $computed[0]);
$this->assertEquals(new \DateTime('2013-02-06'), $computed[1]);
$this->assertEquals(new \DateTime('2013-02-19'), $computed[2]);
$this->assertEquals(new \DateTime('2013-02-20'), $computed[3]);
$this->assertEquals(new \DateTime('2013-03-05'), $computed[4]);
}
public function testByWeekDayMonthlyRelativeFromStart()
{
$rule = new Rule(
'FREQ=MONTHLY;COUNT=5;BYDAY=+1MO',
new \DateTime('2013-06-04')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-07-01'), $computed[0]);
$this->assertEquals(new \DateTime('2013-08-05'), $computed[1]);
$this->assertEquals(new \DateTime('2013-09-02'), $computed[2]);
$this->assertEquals(new \DateTime('2013-10-07'), $computed[3]);
$this->assertEquals(new \DateTime('2013-11-04'), $computed[4]);
}
public function testByWeekDayMonthlyRelativeFromEnd()
{
$rule = new Rule(
'FREQ=MONTHLY;COUNT=5;BYDAY=-1MO',
new \DateTime('2013-06-04')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-06-24'), $computed[0]);
$this->assertEquals(new \DateTime('2013-07-29'), $computed[1]);
$this->assertEquals(new \DateTime('2013-08-26'), $computed[2]);
$this->assertEquals(new \DateTime('2013-09-30'), $computed[3]);
$this->assertEquals(new \DateTime('2013-10-28'), $computed[4]);
// -----------------------
$rule = new Rule(
'FREQ=MONTHLY;COUNT=5;BYDAY=-5MO',
new \DateTime('2013-06-05')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-07-01'), $computed[0]);
$this->assertEquals(new \DateTime('2013-09-02'), $computed[1]);
$this->assertEquals(new \DateTime('2013-12-02'), $computed[2]);
$this->assertEquals(new \DateTime('2014-03-03'), $computed[3]);
$this->assertEquals(new \DateTime('2014-06-02'), $computed[4]);
}
public function testByWeekDayMonthlyRelativeFromStartAndEnd()
{
$rule = new Rule(
'FREQ=MONTHLY;COUNT=10;BYDAY=-1MO,3FR',
new \DateTime('2013-06-04')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(10, count($computed));
$this->assertEquals(new \DateTime('2013-06-21'), $computed[0]);
$this->assertEquals(new \DateTime('2013-06-24'), $computed[1]);
$this->assertEquals(new \DateTime('2013-07-19'), $computed[2]);
$this->assertEquals(new \DateTime('2013-07-29'), $computed[3]);
$this->assertEquals(new \DateTime('2013-08-16'), $computed[4]);
$this->assertEquals(new \DateTime('2013-08-26'), $computed[5]);
$this->assertEquals(new \DateTime('2013-09-20'), $computed[6]);
$this->assertEquals(new \DateTime('2013-09-30'), $computed[7]);
$this->assertEquals(new \DateTime('2013-10-18'), $computed[8]);
$this->assertEquals(new \DateTime('2013-10-28'), $computed[9]);
}
public function testByWeekDayMonthlyWithSundayStartOfYear()
{
$rule = new Rule(
'FREQ=MONTHLY;COUNT=6;BYDAY=MO',
new \DateTime('2017-01-01')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(6, count($computed));
$this->assertEquals(new \DateTime('2017-01-02'), $computed[0]);
$this->assertEquals(new \DateTime('2017-01-09'), $computed[1]);
$this->assertEquals(new \DateTime('2017-01-16'), $computed[2]);
$this->assertEquals(new \DateTime('2017-01-23'), $computed[3]);
$this->assertEquals(new \DateTime('2017-01-30'), $computed[4]);
$this->assertEquals(new \DateTime('2017-02-06'), $computed[5]);
// -----------------------------------
$rule = new Rule(
'FREQ=MONTHLY;COUNT=6;BYDAY=-3MO,3FR',
new \DateTime('2017-01-01')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(6, count($computed));
$this->assertEquals(new \DateTime('2017-01-16'), $computed[0]);
$this->assertEquals(new \DateTime('2017-01-20'), $computed[1]);
$this->assertEquals(new \DateTime('2017-02-13'), $computed[2]);
$this->assertEquals(new \DateTime('2017-02-17'), $computed[3]);
$this->assertEquals(new \DateTime('2017-03-13'), $computed[4]);
$this->assertEquals(new \DateTime('2017-03-17'), $computed[5]);
}
public function testByWeekDayYearlyRelativeFromStart()
{
$rule = new Rule(
'FREQ=YEARLY;COUNT=5;BYDAY=+1MO',
new \DateTime('2013-06-04')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2014-01-06'), $computed[0]);
$this->assertEquals(new \DateTime('2015-01-05'), $computed[1]);
$this->assertEquals(new \DateTime('2016-01-04'), $computed[2]);
$this->assertEquals(new \DateTime('2017-01-02'), $computed[3]);
$this->assertEquals(new \DateTime('2018-01-01'), $computed[4]);
}
public function testByWeekDayYearlyRelativeFromEnd()
{
$rule = new Rule(
'FREQ=YEARLY;COUNT=5;BYDAY=-1MO',
new \DateTime('2013-06-04')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-12-30'), $computed[0]);
$this->assertEquals(new \DateTime('2014-12-29'), $computed[1]);
$this->assertEquals(new \DateTime('2015-12-28'), $computed[2]);
$this->assertEquals(new \DateTime('2016-12-26'), $computed[3]);
$this->assertEquals(new \DateTime('2017-12-25'), $computed[4]);
}
}

View File

@ -0,0 +1,80 @@
<?php
namespace Recurr\Test;
use Recurr\Rule;
class RuleTransformerByWeekNumberTest extends RuleTransformerBase
{
public function testByWeekNumber()
{
$rule = new Rule(
'FREQ=YEARLY;COUNT=5;BYWEEKNO=22;WKST=SU',
new \DateTime('2013-05-30')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-05-30'), $computed[0]);
$this->assertEquals(new \DateTime('2013-05-31'), $computed[1]);
$this->assertEquals(new \DateTime('2013-06-01'), $computed[2]);
$this->assertEquals(new \DateTime('2014-05-25'), $computed[3]);
$this->assertEquals(new \DateTime('2014-05-26'), $computed[4]);
}
public function testByWeekNumberNegative()
{
$rule = new Rule(
'FREQ=YEARLY;COUNT=5;BYWEEKNO=-44;WKST=TH',
new \DateTime('2013-05-30')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2014-02-27'), $computed[0]);
$this->assertEquals(new \DateTime('2014-02-28'), $computed[1]);
$this->assertEquals(new \DateTime('2014-03-01'), $computed[2]);
$this->assertEquals(new \DateTime('2014-03-02'), $computed[3]);
$this->assertEquals(new \DateTime('2014-03-03'), $computed[4]);
}
public function testByWeekNumberWeek53()
{
$rule = new Rule(
'FREQ=YEARLY;COUNT=5;BYWEEKNO=53;WKST=MO',
new \DateTime('2013-05-30')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2015-12-28'), $computed[0]);
$this->assertEquals(new \DateTime('2015-12-29'), $computed[1]);
$this->assertEquals(new \DateTime('2015-12-30'), $computed[2]);
$this->assertEquals(new \DateTime('2015-12-31'), $computed[3]);
$this->assertEquals(new \DateTime('2016-01-01'), $computed[4]);
}
public function testByWeekNumberWeek53Negative()
{
$rule = new Rule(
'FREQ=YEARLY;COUNT=5;BYWEEKNO=-53;WKST=MO',
new \DateTime('2008-05-30')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2009-01-01'), $computed[0]);
$this->assertEquals(new \DateTime('2009-01-02'), $computed[1]);
$this->assertEquals(new \DateTime('2009-01-03'), $computed[2]);
$this->assertEquals(new \DateTime('2009-01-04'), $computed[3]);
$this->assertEquals(new \DateTime('2015-01-01'), $computed[4]);
}
}

View File

@ -0,0 +1,42 @@
<?php
namespace Recurr\Test;
use Recurr\Rule;
class RuleTransformerByYearDayTest extends RuleTransformerBase
{
public function testByYearDay()
{
$rule = new Rule(
'FREQ=YEARLY;COUNT=4;BYYEARDAY=125',
new \DateTime('2013-01-02')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(4, count($computed));
$this->assertEquals(new \DateTime('2013-05-05'), $computed[0]);
$this->assertEquals(new \DateTime('2014-05-05'), $computed[1]);
$this->assertEquals(new \DateTime('2015-05-05'), $computed[2]);
$this->assertEquals(new \DateTime('2016-05-04'), $computed[3]);
}
public function testByYearDayNegative()
{
$rule = new Rule(
'FREQ=YEARLY;COUNT=4;BYYEARDAY=-307',
new \DateTime('2013-06-07')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(4, count($computed));
$this->assertEquals(new \DateTime('2014-02-28'), $computed[0]);
$this->assertEquals(new \DateTime('2015-02-28'), $computed[1]);
$this->assertEquals(new \DateTime('2016-02-29'), $computed[2]);
$this->assertEquals(new \DateTime('2017-02-28'), $computed[3]);
}
}

View File

@ -0,0 +1,80 @@
<?php
namespace Recurr\Test;
use Recurr\Rule;
class RuleTransformerHoursTest extends RuleTransformerBase
{
public function testHourly()
{
$rule = new Rule(
'FREQ=HOURLY;COUNT=5;',
new \DateTime('2013-02-28 23:00:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-02-28 23:00:00'), $computed[0]);
$this->assertEquals(new \DateTime('2013-03-01 00:00:00'), $computed[1]);
$this->assertEquals(new \DateTime('2013-03-01 01:00:00'), $computed[2]);
$this->assertEquals(new \DateTime('2013-03-01 02:00:00'), $computed[3]);
$this->assertEquals(new \DateTime('2013-03-01 03:00:00'), $computed[4]);
}
public function testHourlyInterval()
{
$rule = new Rule(
'FREQ=HOURLY;COUNT=5;INTERVAL=9;',
new \DateTime('2013-02-28 23:00:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-02-28 23:00:00'), $computed[0]);
$this->assertEquals(new \DateTime('2013-03-01 08:00:00'), $computed[1]);
$this->assertEquals(new \DateTime('2013-03-01 17:00:00'), $computed[2]);
$this->assertEquals(new \DateTime('2013-03-02 02:00:00'), $computed[3]);
$this->assertEquals(new \DateTime('2013-03-02 11:00:00'), $computed[4]);
}
public function testHourlyLeapYear()
{
$rule = new Rule(
'FREQ=HOURLY;COUNT=5;',
new \DateTime('2016-02-28 23:00:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2016-02-28 23:00:00'), $computed[0]);
$this->assertEquals(new \DateTime('2016-02-29 00:00:00'), $computed[1]);
$this->assertEquals(new \DateTime('2016-02-29 01:00:00'), $computed[2]);
$this->assertEquals(new \DateTime('2016-02-29 02:00:00'), $computed[3]);
$this->assertEquals(new \DateTime('2016-02-29 03:00:00'), $computed[4]);
}
public function testHourlyCrossingYears()
{
$rule = new Rule(
'FREQ=HOURLY;COUNT=5;',
new \DateTime('2013-12-31 22:00:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-12-31 22:00:00'), $computed[0]);
$this->assertEquals(new \DateTime('2013-12-31 23:00:00'), $computed[1]);
$this->assertEquals(new \DateTime('2014-01-01 00:00:00'), $computed[2]);
$this->assertEquals(new \DateTime('2014-01-01 01:00:00'), $computed[3]);
$this->assertEquals(new \DateTime('2014-01-01 02:00:00'), $computed[4]);
}
}

View File

@ -0,0 +1,44 @@
<?php
namespace Recurr\Test;
use Recurr\Rule;
class RuleTransformerMinutesTest extends RuleTransformerBase
{
public function testMinutely()
{
$rule = new Rule(
'FREQ=MINUTELY;COUNT=5;',
new \DateTime('2016-02-29 23:58:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2016-02-29 23:58:00'), $computed[0]);
$this->assertEquals(new \DateTime('2016-02-29 23:59:00'), $computed[1]);
$this->assertEquals(new \DateTime('2016-03-01 00:00:00'), $computed[2]);
$this->assertEquals(new \DateTime('2016-03-01 00:01:00'), $computed[3]);
$this->assertEquals(new \DateTime('2016-03-01 00:02:00'), $computed[4]);
}
public function testMinutelyInterval()
{
$rule = new Rule(
'FREQ=MINUTELY;COUNT=5;INTERVAL=58;',
new \DateTime('2013-02-28 23:58:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-02-28 23:58:00'), $computed[0]);
$this->assertEquals(new \DateTime('2013-02-29 00:56:00'), $computed[1]);
$this->assertEquals(new \DateTime('2013-02-29 01:54:00'), $computed[2]);
$this->assertEquals(new \DateTime('2013-02-29 02:52:00'), $computed[3]);
$this->assertEquals(new \DateTime('2013-02-29 03:50:00'), $computed[4]);
}
}

View File

@ -0,0 +1,99 @@
<?php
namespace Recurr\Test;
use Recurr\Rule;
use Recurr\RuleTransformerConfig;
class RuleTransformerMonthsTest extends RuleTransformerBase
{
public function testMonthly()
{
$timezone = 'America/New_York';
$timezoneObj = new \DateTimeZone($timezone);
$rule = new Rule(
'FREQ=MONTHLY;COUNT=3;INTERVAL=1',
new \DateTime('2013-01-31 00:00:00', $timezoneObj),
$timezone
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(3, count($computed));
$this->assertEquals(new \DateTime('2013-01-31 00:00:00', $timezoneObj), $computed[0]);
$this->assertEquals(new \DateTime('2013-03-31 00:00:00', $timezoneObj), $computed[1]);
$this->assertEquals(new \DateTime('2013-05-31 00:00:00', $timezoneObj), $computed[2]);
}
public function testMonthlyWithLastDayFixEnabled()
{
$rule = new Rule(
'FREQ=MONTHLY;COUNT=10',
new \DateTime('2013-11-30')
);
$transformerConfig = new RuleTransformerConfig();
$transformerConfig->enableLastDayOfMonthFix();
$this->transformer->setRule($rule);
$this->transformer->setConfig($transformerConfig);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(10, count($computed));
$this->assertEquals(new \DateTime('2013-11-30'), $computed[0]);
$this->assertEquals(new \DateTime('2013-12-30'), $computed[1]);
$this->assertEquals(new \DateTime('2014-01-30'), $computed[2]);
$this->assertEquals(new \DateTime('2014-02-28'), $computed[3]);
$this->assertEquals(new \DateTime('2014-03-30'), $computed[4]);
$this->assertEquals(new \DateTime('2014-04-30'), $computed[5]);
$this->assertEquals(new \DateTime('2014-05-30'), $computed[6]);
$this->assertEquals(new \DateTime('2014-06-30'), $computed[7]);
$this->assertEquals(new \DateTime('2014-07-30'), $computed[8]);
$this->assertEquals(new \DateTime('2014-08-30'), $computed[9]);
}
public function testMonthlyWithLastDayFixEnabledOnLeapYear()
{
$rule = new Rule(
'FREQ=MONTHLY;COUNT=8',
new \DateTime('2016-01-31')
);
$transformerConfig = new RuleTransformerConfig();
$transformerConfig->enableLastDayOfMonthFix();
$this->transformer->setRule($rule);
$this->transformer->setConfig($transformerConfig);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(8, count($computed));
$this->assertEquals(new \DateTime('2016-01-31'), $computed[0]);
$this->assertEquals(new \DateTime('2016-02-29'), $computed[1]);
$this->assertEquals(new \DateTime('2016-03-31'), $computed[2]);
$this->assertEquals(new \DateTime('2016-04-30'), $computed[3]);
$this->assertEquals(new \DateTime('2016-05-31'), $computed[4]);
$this->assertEquals(new \DateTime('2016-06-30'), $computed[5]);
$this->assertEquals(new \DateTime('2016-07-31'), $computed[6]);
$this->assertEquals(new \DateTime('2016-08-31'), $computed[7]);
}
public function testLastDayOfMonth()
{
$rule = new Rule(
'FREQ=MONTHLY;COUNT=5;BYMONTHDAY=28,29,30,31;BYSETPOS=-1',
new \DateTime('2016-01-29')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2016-01-31'), $computed[0]);
$this->assertEquals(new \DateTime('2016-02-29'), $computed[1]);
$this->assertEquals(new \DateTime('2016-03-31'), $computed[2]);
$this->assertEquals(new \DateTime('2016-04-30'), $computed[3]);
$this->assertEquals(new \DateTime('2016-05-31'), $computed[4]);
}
}

View File

@ -0,0 +1,44 @@
<?php
namespace Recurr\Test;
use Recurr\Rule;
class RuleTransformerSecondlyTest extends RuleTransformerBase
{
public function testSecondly()
{
$rule = new Rule(
'FREQ=SECONDLY;COUNT=5;',
new \DateTime('2016-02-29 23:58:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2016-02-29 23:58:00'), $computed[0]);
$this->assertEquals(new \DateTime('2016-02-29 23:58:01'), $computed[1]);
$this->assertEquals(new \DateTime('2016-02-29 23:58:02'), $computed[2]);
$this->assertEquals(new \DateTime('2016-02-29 23:58:03'), $computed[3]);
$this->assertEquals(new \DateTime('2016-02-29 23:58:04'), $computed[4]);
}
public function testSecondlyInterval()
{
$rule = new Rule(
'FREQ=SECONDLY;COUNT=5;INTERVAL=58;',
new \DateTime('2016-02-29 23:58:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2016-02-29 23:58:00'), $computed[0]);
$this->assertEquals(new \DateTime('2016-02-29 23:58:58'), $computed[1]);
$this->assertEquals(new \DateTime('2016-02-29 23:59:56'), $computed[2]);
$this->assertEquals(new \DateTime('2016-03-01 00:00:54'), $computed[3]);
$this->assertEquals(new \DateTime('2016-03-01 00:01:52'), $computed[4]);
}
}

View File

@ -0,0 +1,51 @@
<?php
namespace Recurr\Test;
use Recurr\Rule;
class RuleTransformerTest extends RuleTransformerBase
{
public function testRfc2445Example()
{
$rule = new Rule(
'FREQ=YEARLY;INTERVAL=2;BYMONTH=1;BYDAY=SU;BYHOUR=8,9;COUNT=30',
new \DateTime('1997-01-05 08:30:00')
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(30, count($computed));
$this->assertEquals(new \DateTime('1997-01-05 08:30:00'), $computed[0]);
$this->assertEquals(new \DateTime('1997-01-05 09:30:00'), $computed[1]);
$this->assertEquals(new \DateTime('1997-01-12 08:30:00'), $computed[2]);
$this->assertEquals(new \DateTime('1997-01-12 09:30:00'), $computed[3]);
$this->assertEquals(new \DateTime('1997-01-19 08:30:00'), $computed[4]);
$this->assertEquals(new \DateTime('1997-01-19 09:30:00'), $computed[5]);
$this->assertEquals(new \DateTime('1997-01-26 08:30:00'), $computed[6]);
$this->assertEquals(new \DateTime('1997-01-26 09:30:00'), $computed[7]);
$this->assertEquals(new \DateTime('1999-01-03 08:30:00'), $computed[8]);
$this->assertEquals(new \DateTime('1999-01-03 09:30:00'), $computed[9]);
$this->assertEquals(new \DateTime('1999-01-10 08:30:00'), $computed[10]);
$this->assertEquals(new \DateTime('1999-01-10 09:30:00'), $computed[11]);
$this->assertEquals(new \DateTime('1999-01-17 08:30:00'), $computed[12]);
$this->assertEquals(new \DateTime('1999-01-17 09:30:00'), $computed[13]);
$this->assertEquals(new \DateTime('1999-01-24 08:30:00'), $computed[14]);
$this->assertEquals(new \DateTime('1999-01-24 09:30:00'), $computed[15]);
$this->assertEquals(new \DateTime('1999-01-31 08:30:00'), $computed[16]);
$this->assertEquals(new \DateTime('1999-01-31 09:30:00'), $computed[17]);
$this->assertEquals(new \DateTime('2001-01-07 08:30:00'), $computed[18]);
$this->assertEquals(new \DateTime('2001-01-07 09:30:00'), $computed[19]);
$this->assertEquals(new \DateTime('2001-01-14 08:30:00'), $computed[20]);
$this->assertEquals(new \DateTime('2001-01-14 09:30:00'), $computed[21]);
$this->assertEquals(new \DateTime('2001-01-21 08:30:00'), $computed[22]);
$this->assertEquals(new \DateTime('2001-01-21 09:30:00'), $computed[23]);
$this->assertEquals(new \DateTime('2001-01-28 08:30:00'), $computed[24]);
$this->assertEquals(new \DateTime('2001-01-28 09:30:00'), $computed[25]);
$this->assertEquals(new \DateTime('2003-01-05 08:30:00'), $computed[26]);
$this->assertEquals(new \DateTime('2003-01-05 09:30:00'), $computed[27]);
$this->assertEquals(new \DateTime('2003-01-12 08:30:00'), $computed[28]);
$this->assertEquals(new \DateTime('2003-01-12 09:30:00'), $computed[29]);
}
}

View File

@ -0,0 +1,96 @@
<?php
namespace Recurr\Test;
use Recurr\Rule;
class RuleTransformerWeeklyTest extends RuleTransformerBase
{
public function testWeekly()
{
$timezone = 'America/New_York';
$timezoneObj = new \DateTimeZone($timezone);
$rule = new Rule(
'FREQ=WEEKLY;COUNT=5;INTERVAL=1',
new \DateTime('2013-06-13 00:00:00', $timezoneObj),
$timezone
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-06-13 00:00:00', $timezoneObj), $computed[0]);
$this->assertEquals(new \DateTime('2013-06-20 00:00:00', $timezoneObj), $computed[1]);
$this->assertEquals(new \DateTime('2013-06-27 00:00:00', $timezoneObj), $computed[2]);
$this->assertEquals(new \DateTime('2013-07-04 00:00:00', $timezoneObj), $computed[3]);
$this->assertEquals(new \DateTime('2013-07-11 00:00:00', $timezoneObj), $computed[4]);
}
public function testWeeklyInterval()
{
$timezone = 'America/New_York';
$timezoneObj = new \DateTimeZone($timezone);
$rule = new Rule(
'FREQ=WEEKLY;COUNT=5;INTERVAL=2',
new \DateTime('2013-12-19 00:00:00', $timezoneObj),
$timezone
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2013-12-19 00:00:00', $timezoneObj), $computed[0]);
$this->assertEquals(new \DateTime('2014-01-02 00:00:00', $timezoneObj), $computed[1]);
$this->assertEquals(new \DateTime('2014-01-16 00:00:00', $timezoneObj), $computed[2]);
$this->assertEquals(new \DateTime('2014-01-30 00:00:00', $timezoneObj), $computed[3]);
$this->assertEquals(new \DateTime('2014-02-13 00:00:00', $timezoneObj), $computed[4]);
}
public function testWeeklyIntervalLeapYear()
{
$timezone = 'America/New_York';
$timezoneObj = new \DateTimeZone($timezone);
$rule = new Rule(
'FREQ=WEEKLY;COUNT=7;INTERVAL=2',
new \DateTime('2015-12-21 00:00:00', $timezoneObj),
$timezone
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(7, count($computed));
$this->assertEquals(new \DateTime('2015-12-21 00:00:00', $timezoneObj), $computed[0]);
$this->assertEquals(new \DateTime('2016-01-04 00:00:00', $timezoneObj), $computed[1]);
$this->assertEquals(new \DateTime('2016-01-18 00:00:00', $timezoneObj), $computed[2]);
$this->assertEquals(new \DateTime('2016-02-01 00:00:00', $timezoneObj), $computed[3]);
$this->assertEquals(new \DateTime('2016-02-15 00:00:00', $timezoneObj), $computed[4]);
$this->assertEquals(new \DateTime('2016-02-29 00:00:00', $timezoneObj), $computed[5]);
$this->assertEquals(new \DateTime('2016-03-14 00:00:00', $timezoneObj), $computed[6]);
}
public function testWeeklyIntervalTouchingJan1()
{
$timezone = 'America/New_York';
$timezoneObj = new \DateTimeZone($timezone);
$rule = new Rule(
'FREQ=WEEKLY;COUNT=3;INTERVAL=2',
new \DateTime('2013-12-18 00:00:00', $timezoneObj),
$timezone
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(3, count($computed));
$this->assertEquals(new \DateTime('2013-12-18 00:00:00', $timezoneObj), $computed[0]);
$this->assertEquals(new \DateTime('2014-01-01 00:00:00', $timezoneObj), $computed[1]);
$this->assertEquals(new \DateTime('2014-01-15 00:00:00', $timezoneObj), $computed[2]);
}
}

View File

@ -0,0 +1,46 @@
<?php
namespace Recurr\Test;
use Recurr\Rule;
class RuleTransformerYearlyTest extends RuleTransformerBase
{
public function testYearly()
{
$timezone = new \DateTimeZone('America/New_York');
$rule = new Rule(
'FREQ=YEARLY;COUNT=3;INTERVAL=1',
new \DateTime('2013-06-13 00:00:00', $timezone)
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(3, count($computed));
$this->assertEquals(new \DateTime('2013-06-13 00:00:00', $timezone), $computed[0]);
$this->assertEquals(new \DateTime('2014-06-13 00:00:00', $timezone), $computed[1]);
$this->assertEquals(new \DateTime('2015-06-13 00:00:00', $timezone), $computed[2]);
}
public function testYearlyLeapYear()
{
$timezone = new \DateTimeZone('America/New_York');
$rule = new Rule(
'FREQ=YEARLY;COUNT=5;INTERVAL=1',
new \DateTime('2016-02-29 00:00:00', $timezone)
);
$this->transformer->setRule($rule);
$computed = $this->transformer->getComputedArray();
$this->assertEquals(5, count($computed));
$this->assertEquals(new \DateTime('2016-02-29 00:00:00', $timezone), $computed[0]);
$this->assertEquals(new \DateTime('2017-02-28 00:00:00', $timezone), $computed[1]);
$this->assertEquals(new \DateTime('2018-02-28 00:00:00', $timezone), $computed[2]);
$this->assertEquals(new \DateTime('2019-02-28 00:00:00', $timezone), $computed[3]);
$this->assertEquals(new \DateTime('2020-02-29 00:00:00', $timezone), $computed[4]);
}
}