Skip to content

Commit

Permalink
Handle consecutive days opening hours
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Feb 13, 2019
1 parent db4ac8c commit 0c04c99
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 21 deletions.
3 changes: 3 additions & 0 deletions .multi-tester.yml
@@ -0,0 +1,3 @@
cmixin/business-time:
install: default
script: default
13 changes: 10 additions & 3 deletions .travis.yml
Expand Up @@ -5,11 +5,17 @@ php:
- 7.1
- 7.2
- 7.3
- 8.0
- nightly

matrix:
allow_failures:
- php: nightly
include:
- php: 7.3
env: MULTITEST='on'
allow_failures:
- php: 8.0
- php: nightly
fast_finish: true

env:
matrix:
Expand All @@ -21,7 +27,8 @@ before_script:
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source

script:
- phpunit --coverage-text --coverage-clover=coverage.clover
- if [ "$MULTITEST" != "on" ]; then phpunit --coverage-text --coverage-clover=coverage.clover; fi;
- if [ "$MULTITEST" = "on" ]; then vendor/bin/multi-tester; fi;

after_script:
- php vendor/bin/ocular code-coverage:upload --format=php-clover coverage.clover
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -22,6 +22,7 @@
"php": "^7.0"
},
"require-dev": {
"kylekatarnls/multi-tester": "^1.0",
"phpunit/phpunit": "^6.4"
},
"autoload": {
Expand Down
5 changes: 4 additions & 1 deletion src/OpeningHours.php
Expand Up @@ -254,7 +254,10 @@ public function nextOpen(DateTimeInterface $dateTime): DateTimeInterface
$openingHoursForDay = $this->forDate($dateTime);

$nextOpen = $openingHoursForDay->nextOpen(Time::fromDateTime($dateTime));
var_dump($openingHoursForDay, $dateTime, Time::fromDateTime($dateTime), $nextOpen);
}

if ($dateTime->format('H:i') === '00:00' && $this->isOpenAt((clone $dateTime)->modify('-1 second'))) {
return $this->nextOpen($dateTime->modify('+1 minute'));
}

$nextDateTime = $nextOpen->toDateTime();
Expand Down
4 changes: 2 additions & 2 deletions src/OpeningHoursForDay.php
Expand Up @@ -109,7 +109,7 @@ function ($timeRange) use ($time) {

protected function findNextOpenInWorkingHours(Time $time, TimeRange $timeRange)
{
if ($timeRange->containsTime($time) && next($timeRange) !== $timeRange) {
if ($timeRange->containsTime($time) && $timeRange->start()->isAfter($time) && next($timeRange) !== $timeRange) {
return next($timeRange);
}
}
Expand All @@ -120,7 +120,7 @@ protected function findNextOpenInFreeTime(Time $time, TimeRange $timeRange, Time
TimeRange::fromString($prevTimeRange->end().'-'.$timeRange->start()) :
TimeRange::fromString('00:00-'.$timeRange->start());

if ($timeOffRange->containsTime($time) || $timeOffRange->start()->isSame($time)) {
if ($timeOffRange->containsTime($time) && $timeRange->start()->isAfter($time) || $timeOffRange->start()->isSame($time)) {
return $timeRange->start();
}

Expand Down
27 changes: 12 additions & 15 deletions tests/OpeningHoursTest.php
Expand Up @@ -263,10 +263,7 @@ public function it_can_prioritize_exceptions_by_giving_full_dates_priority()
$this->assertTrue($openingHours->isClosedAt($closedOnNewYearDay2019));
}

/**
* @group i
* @test
*/
/** @test */
public function it_can_handle_consecutive_open_hours()
{
$openingHours = OpeningHours::create([
Expand All @@ -288,17 +285,17 @@ public function it_can_handle_consecutive_open_hours()
$this->assertEquals('2019-02-06 03:00:00', $openingHours->nextClose($monday)->format('Y-m-d H:i:s'));
$this->assertEquals('2019-02-06 09:00:00', $openingHours->nextOpen($monday)->format('Y-m-d H:i:s'));

$monday = new DateTime('2019-02-06 09:00:00');
$this->assertTrue($openingHours->isOpenAt($monday));
$this->assertFalse($openingHours->isClosedAt($monday));
$this->assertEquals('2019-02-06 03:00:00', $openingHours->nextClose($monday)->format('Y-m-d H:i:s'));
$this->assertEquals('2019-02-06 09:00:00', $openingHours->nextOpen($monday)->format('Y-m-d H:i:s'));

$monday = new DateTimeImmutable('2019-02-06 09:00:00');
$this->assertTrue($openingHours->isOpenAt($monday));
$this->assertFalse($openingHours->isClosedAt($monday));
$this->assertEquals('2019-02-06 03:00:00', $openingHours->nextClose($monday)->format('Y-m-d H:i:s'));
$this->assertEquals('2019-02-06 09:00:00', $openingHours->nextOpen($monday)->format('Y-m-d H:i:s'));
$wednesday = new DateTime('2019-02-06 09:00:00');
$this->assertTrue($openingHours->isOpenAt($wednesday));
$this->assertFalse($openingHours->isClosedAt($wednesday));
$this->assertEquals('2019-02-07 00:00:00', $openingHours->nextClose($wednesday)->format('Y-m-d H:i:s'));
$this->assertEquals('2019-02-08 00:00:00', $openingHours->nextOpen($wednesday)->format('Y-m-d H:i:s'));

$wednesday = new DateTimeImmutable('2019-02-06 09:00:00');
$this->assertTrue($openingHours->isOpenAt($wednesday));
$this->assertFalse($openingHours->isClosedAt($wednesday));
$this->assertEquals('2019-02-07 00:00:00', $openingHours->nextClose($wednesday)->format('Y-m-d H:i:s'));
$this->assertEquals('2019-02-08 00:00:00', $openingHours->nextOpen($wednesday)->format('Y-m-d H:i:s'));
}

/** @test */
Expand Down

0 comments on commit 0c04c99

Please sign in to comment.