-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
<?php | ||
|
||
namespace ipl\Tests\Scheduler; | ||
|
||
use DateTime; | ||
use ipl\Scheduler\RRule; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class RRuleTest extends TestCase | ||
{ | ||
public function testIsDueWithoutEndTime() | ||
{ | ||
$start = new DateTime(); | ||
$rrule = RRule::fromFrequency(RRule::MINUTELY) | ||
->startAt($start); | ||
|
||
$start->setTime($start->format('H'), $start->format('i'), $start->format('s')); | ||
|
||
$this->assertTrue($rrule->isDue((clone $start)->modify('+1 minute'))); | ||
} | ||
|
||
public function testIsDueWithEndTime() | ||
{ | ||
$start = new DateTime(); | ||
$start->setTime($start->format('H'), $start->format('i'), $start->format('s')); | ||
|
||
$end = clone $start; | ||
$end->modify('+2 minute'); | ||
|
||
$rrule = RRule::fromFrequency(RRule::MINUTELY) | ||
->startAt($start) | ||
->endAt($end); | ||
|
||
$this->assertTrue($rrule->isDue((clone $start)->modify('+1 minute'))); | ||
$this->assertFalse($rrule->isDue((clone $end)->modify('+1 minute'))); | ||
} | ||
|
||
public function testIsExpiredWithoutEndTime() | ||
{ | ||
$rrule = RRule::fromFrequency(RRule::MINUTELY) | ||
->startAt(new DateTime()); | ||
|
||
$this->assertFalse($rrule->isExpired(new DateTime('+1 day'))); | ||
} | ||
|
||
public function testIsExpiredWithEndTime() | ||
{ | ||
$rrule = RRule::fromFrequency(RRule::MINUTELY) | ||
->startAt(new DateTime()) | ||
->endAt(new DateTime('+5 minute')); | ||
|
||
$this->assertFalse($rrule->isExpired(new DateTime('+2 minute'))); | ||
$this->assertTrue($rrule->isExpired(new DateTime('+10 minute'))); | ||
} | ||
|
||
public function testGetNextDueWithoutEndTime() | ||
{ | ||
$start = new DateTime(); | ||
$start->setTime($start->format('H'), $start->format('i'), $start->format('s')); | ||
|
||
$rrule = RRule::fromFrequency(RRule::MINUTELY) | ||
->startAt($start); | ||
|
||
$this->assertEquals((clone $start)->modify('+1 minute'), $rrule->getNextDue($start)); | ||
} | ||
|
||
public function testGetNextDueWithEndTime() | ||
{ | ||
$end = new DateTime('+5 minutes'); | ||
$rrule = RRule::fromFrequency(RRule::MINUTELY) | ||
->startAt(new DateTime()) | ||
->endAt($end); | ||
|
||
$this->assertEquals($end, $rrule->getNextDue(new DateTime('+2 hours'))); | ||
} | ||
|
||
public function testGetNextRecurrencesWithDefaultLimit() | ||
{ | ||
$rrule = RRule::fromFrequency(RRule::MINUTELY) | ||
->startAt(new DateTime('-1 hour')); | ||
|
||
$this->assertCount(1, iterator_to_array($rrule->getNextRecurrences(new DateTime()))); | ||
} | ||
|
||
public function testGetNextRecurrencesWithCustomResultSetLimit() | ||
{ | ||
$start = new DateTime('-2 day'); | ||
$rrule = RRule::fromFrequency(RRule::MINUTELY) | ||
->startAt($start); | ||
|
||
$this->assertCount(5, iterator_to_array($rrule->getNextRecurrences(new DateTime(), 5))); | ||
|
||
$start->modify('+2 day'); | ||
$rrule->startAt($start); | ||
|
||
$this->assertCount(5, iterator_to_array($rrule->getNextRecurrences(new DateTime(), 5))); | ||
} | ||
|
||
public function testMonthlyOn31DoesNotSkippFebruaryAndOtherMonths() | ||
{ | ||
$start = new DateTime('31 January this year'); | ||
if ($start < new DateTime()) { | ||
$start->modify('next year'); | ||
} | ||
|
||
$recurrences = RRule::fromFrequency(RRule::MONTHLY) | ||
->startAt($start) | ||
->getNextRecurrences((clone $start)->modify('+5 days')); | ||
|
||
$this->assertEquals($start->modify('last day of next month'), $recurrences->current()); | ||
} | ||
|
||
public function testQuarterlyFrequency() | ||
{ | ||
$start = new DateTime('+1 days'); | ||
$start->setTime($start->format('H'), $start->format('i'), $start->format('s')); | ||
$recurrences = RRule::fromFrequency(RRule::QUARTERLY) | ||
->startAt($start) | ||
->getNextRecurrences(new DateTime('+2 days')); | ||
|
||
$this->assertEquals($start->modify('+3 months'), $recurrences->current()); | ||
} | ||
} |