You are here

public function DateRecurRruleUnitTest::testGenerateOccurrences in Recurring Dates Field 3.x

Same name and namespace in other branches
  1. 8.2 tests/src/Unit/DateRecurRruleUnitTest.php \Drupal\Tests\date_recur\Unit\DateRecurRruleUnitTest::testGenerateOccurrences()
  2. 3.0.x tests/src/Unit/DateRecurRruleUnitTest.php \Drupal\Tests\date_recur\Unit\DateRecurRruleUnitTest::testGenerateOccurrences()
  3. 3.1.x tests/src/Unit/DateRecurRruleUnitTest.php \Drupal\Tests\date_recur\Unit\DateRecurRruleUnitTest::testGenerateOccurrences()

Tests list.

@covers ::generateOccurrences

File

tests/src/Unit/DateRecurRruleUnitTest.php, line 74

Class

DateRecurRruleUnitTest
Date recur tests.

Namespace

Drupal\Tests\date_recur\Unit

Code

public function testGenerateOccurrences() {
  $tz = new \DateTimeZone('Africa/Cairo');
  $start = new \DateTime('11pm 7 June 2005', $tz);
  $end = clone $start;
  $end
    ->modify('+2 hours');
  $rule = $this
    ->newRule('FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR', $start, $end);
  $generator = $rule
    ->generateOccurrences();
  $this
    ->assertTrue($generator instanceof \Generator);
  $assertOccurrences = [
    [
      new \DateTime('11pm 7 June 2005', $tz),
      new \DateTime('1am 8 June 2005', $tz),
    ],
    [
      new \DateTime('11pm 8 June 2005', $tz),
      new \DateTime('1am 9 June 2005', $tz),
    ],
    [
      new \DateTime('11pm 9 June 2005', $tz),
      new \DateTime('1am 10 June 2005', $tz),
    ],
    [
      new \DateTime('11pm 10 June 2005', $tz),
      new \DateTime('1am 11 June 2005', $tz),
    ],
    [
      new \DateTime('11pm 13 June 2005', $tz),
      new \DateTime('1am 14 June 2005', $tz),
    ],
    [
      new \DateTime('11pm 14 June 2005', $tz),
      new \DateTime('1am 15 June 2005', $tz),
    ],
    [
      new \DateTime('11pm 15 June 2005', $tz),
      new \DateTime('1am 16 June 2005', $tz),
    ],
  ];

  // Iterate over it a bit, because this is an infinite RRULE it will go
  // forever.
  $iterationCount = 0;
  $maxIterations = count($assertOccurrences);
  foreach ($generator as $occurrence) {
    $this
      ->assertTrue($occurrence instanceof DateRange);
    [
      $assertStart,
      $assertEnd,
    ] = $assertOccurrences[$iterationCount];
    $this
      ->assertTrue($assertStart == $occurrence
      ->getStart());
    $this
      ->assertTrue($assertEnd == $occurrence
      ->getEnd());
    $iterationCount++;
    if ($iterationCount >= $maxIterations) {
      break;
    }
  }
  $this
    ->assertEquals($maxIterations, $iterationCount);
}