Skip to content

Commit

Permalink
tests: Add RRule test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab committed Jan 27, 2023
1 parent e3f46cc commit 5bf80c5
Showing 1 changed file with 123 additions and 0 deletions.
123 changes: 123 additions & 0 deletions tests/RRuleTest.php
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());
}
}

0 comments on commit 5bf80c5

Please sign in to comment.