Skip to content

Commit

Permalink
Add tests for ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Nov 11, 2023
1 parent 27b6a9b commit 1b32994
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/OpeningHours.php
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,11 @@ protected function readDatesRange(string $key): iterable

$dashChunks = explode('-', $key);
$chunksCount = count($dashChunks);
$firstChunk = trim($dashChunks[0]);

if ($chunksCount === 2 && preg_match('/^[A-Za-z]+$/', $firstChunk)) {
return $this->daysBetween($firstChunk, trim($dashChunks[1]));
}

if ($chunksCount >= 4) {
$middle = ceil($chunksCount / 2);
Expand All @@ -829,12 +834,28 @@ protected function readDatesRange(string $key): iterable
return [$key];
}

protected function daysBetween(string $start, string $end): DatePeriod
/** @return Generator<string> */
protected function daysBetween(string $start, string $end): Generator
{
$count = count(explode('-', $start));

if ($count === 2) {
// Use an arbitrary leap year
$start = "2024-$start";
$end = "2024-$end";
}

$startDate = new DateTimeImmutable($start);
$endDate = $startDate->modify($end)->modify('+12 hours');

return new DatePeriod($startDate, new DateInterval('P1D'), $endDate);
$format = [
2 => 'm-d',
3 => 'Y-m-d',
][$count] ?? 'l';

foreach (new DatePeriod($startDate, new DateInterval('P1D'), $endDate) as $date) {
yield $date->format($format);
}
}

protected function setOpeningHoursFromStrings(string $day, array $openingHours): void
Expand Down
58 changes: 58 additions & 0 deletions tests/OpeningHoursTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use DateTimeImmutable;
use DateTimeZone;
use PHPUnit\Framework\TestCase;
use Spatie\OpeningHours\Exceptions\InvalidDateRange;
use Spatie\OpeningHours\Exceptions\MaximumLimitExceeded;
use Spatie\OpeningHours\Exceptions\SearchLimitReached;
use Spatie\OpeningHours\OpeningHours;
Expand Down Expand Up @@ -1583,4 +1584,61 @@ public function testSearchWithEmptyHours()

$this->assertSame(0.0, $minutes);
}

public function testRanges()
{
$openingHours = OpeningHours::create([
'monday - wednesday' => ['08:30-12:00', '14:30-16:00'],
'thursday to friday' => ['14:30-18:00'],
'saturday-sunday' => [],
'exceptions' => [
'2016-11-11-2016-11-14' => ['09:00-12:00'],
'11-30-12-01' => ['09:00-14:00'],
'12-24 to 12-26' => [],
'11-10 - 11-12' => ['07:00-10:00'],
],
]);

$this->assertSame([
'monday' => '08:30-12:00,14:30-16:00',
'tuesday' => '08:30-12:00,14:30-16:00',
'wednesday' => '08:30-12:00,14:30-16:00',
'thursday' => '14:30-18:00',
'friday' => '14:30-18:00',
'saturday' => '',
'sunday' => '',
], array_map(
static fn (OpeningHoursForDay $day) => (string) $day,
$openingHours->forWeek(),
));
$this->assertSame('07:00-10:00', (string) $openingHours->forDate(new DateTimeImmutable('2016-11-10 11:00')));
$this->assertSame('09:00-12:00', (string) $openingHours->forDate(new DateTimeImmutable('2016-11-12 11:00')));
$this->assertSame('09:00-14:00', (string) $openingHours->forDate(new DateTimeImmutable('2023-12-01 11:00')));
$this->assertSame('', (string) $openingHours->forDate(new DateTimeImmutable('2024-12-25 11:00')));
}

public function testRangesWeekOverlap()
{
$this->expectException(InvalidDateRange::class);
$this->expectExceptionMessage('Unable to record `tuesday to friday` as it would override `tuesday`.');

OpeningHours::create([
'monday - wednesday' => ['08:30-12:00', '14:30-16:00'],
'tuesday to friday' => ['14:30-18:00'],
]);
}

public function testRangesExceptionOverlap()
{
$this->expectException(InvalidDateRange::class);
$this->expectExceptionMessage('Unable to record `11-10 to 11-12` as it would override `11-11`.');

OpeningHours::create([
'monday - wednesday' => ['08:30-12:00', '14:30-16:00'],
'exceptions' => [
'11-11-11-14' => ['09:00-12:00'],
'11-10 to 11-12' => ['07:00-10:00'],
],
]);
}
}

0 comments on commit 1b32994

Please sign in to comment.