Skip to content

Commit

Permalink
Merge pull request #251 from spatie/fix/issue-243-previousOpen-overflow
Browse files Browse the repository at this point in the history
Check both start and end to consider a range before/after
  • Loading branch information
kylekatarnls authored Jun 26, 2024
2 parents e6a47ce + 7fcc8fa commit 48b4532
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Helpers/RangeFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected function findCloseInFreeTime(Time $time, TimeRange $timeRange): ?Time

protected function findPreviousRangeInFreeTime(Time $time, TimeRange $timeRange): ?TimeRange
{
return $time->isAfter($timeRange->end()) ? $timeRange : null;
return $time->isAfter($timeRange->end()) && $time->isAfter($timeRange->start()) ? $timeRange : null;
}

protected function findPreviousOpenInFreeTime(Time $time, TimeRange $timeRange): ?Time
Expand Down
14 changes: 14 additions & 0 deletions tests/OpeningHoursOverflowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,20 @@ public function next_close_with_overflow_immutable()
$this->assertSame('2019-04-22 02:00:00', $previousTimeOpen);
}

#[Test]
public function previous_open_and_close_with_overflow_immutable()
{
$openingHours = OpeningHours::create([
'overflow' => true,
'monday' => ['18:00-05:00'],
'tuesday' => ['18:00-05:00'],
]);
$tuesday = new DateTime('2024-06-11 06:00:00');

$this->assertSame('2024-06-10 18:00', $openingHours->previousOpen($tuesday)->format('Y-m-d H:i'));
$this->assertSame('2024-06-11 05:00', $openingHours->previousClose($tuesday)->format('Y-m-d H:i'));
}

#[Test]
public function overflow_on_simple_ranges()
{
Expand Down

0 comments on commit 48b4532

Please sign in to comment.