DateRecurDateRangeUnitTest.php in Recurring Dates Field 3.x
File
tests/src/Unit/DateRecurDateRangeUnitTest.php
View source
<?php
namespace Drupal\Tests\date_recur\Unit;
use Drupal\date_recur\DateRange;
use Drupal\Tests\UnitTestCase;
class DateRecurDateRangeUnitTest extends UnitTestCase {
public function testRequiredConstructorArguments() {
$this
->expectException(\ArgumentCountError::class);
$this
->createDateRange();
}
public function testGetters() {
$start = new \DateTime('yesterday');
$end = new \DateTime('tomorrow');
$dateRange = $this
->createDateRange($start, $end);
$this
->assertEquals($start, $dateRange
->getStart());
$this
->assertEquals($end, $dateRange
->getEnd());
$this
->assertNotEquals($dateRange
->getStart(), $dateRange
->getEnd());
}
public function testConstructorReferencesLost() {
$startOriginal = new \DateTime('Monday 12:00:00');
$endOriginal = new \DateTime('Monday 12:00:00');
$start = clone $startOriginal;
$end = clone $endOriginal;
$dateRange = $this
->createDateRange($start, $end);
$start
->modify('+1 year');
$end
->modify('+1 year');
$this
->assertEquals($startOriginal, $dateRange
->getStart());
$this
->assertEquals($endOriginal, $dateRange
->getEnd());
}
public function testGetStartImmutable() {
$original = new \DateTime('Monday 12:00:00');
$dateRange = $this
->createDateRange(clone $original, new \DateTime('Monday 12:00:00'));
$gotten = $dateRange
->getStart();
$gotten
->modify('+1 year');
$gotten
->setTimezone(new \DateTimeZone('Asia/Singapore'));
$this
->assertEquals($original, $dateRange
->getStart());
}
public function testGetEndImmutable() {
$original = new \DateTime('Monday 12:00:00');
$dateRange = $this
->createDateRange(new \DateTime('Monday 12:00:00'), clone $original);
$gotten = $dateRange
->getEnd();
$gotten
->modify('+1 year');
$gotten
->setTimezone(new \DateTimeZone('Asia/Singapore'));
$this
->assertEquals($original, $dateRange
->getEnd());
}
public function testEndAfterStartValidation() {
$start = new \DateTime('Monday 12:00:00');
$end = new \DateTime('Monday 12:00:00');
$this
->createDateRange($start, $end);
$start = new \DateTime('Monday 12:00:00');
$end = new \DateTime('Monday 12:00:01');
$this
->createDateRange($start, $end);
$start = new \DateTime('Monday 12:00:01');
$end = new \DateTime('Monday 12:00:00');
$this
->expectException(\InvalidArgumentException::class);
$this
->expectExceptionMessage('End date must not occur before start date.');
$this
->createDateRange($start, $end);
}
public function testTimezoneValidation() {
$start = new \DateTime('Monday 12:00:00', new \DateTimeZone('Australia/Melbourne'));
$end = new \DateTime('Monday 12:00:00', new \DateTimeZone('Australia/Sydney'));
$this
->expectException(\InvalidArgumentException::class);
$this
->expectExceptionMessage('Provided dates must be the same timezone.');
$this
->createDateRange($start, $end);
}
protected function createDateRange($start, $end) {
return new DateRange($start, $end);
}
}