From 0c04c997712896c0927abda03a13bd30b2b7543c Mon Sep 17 00:00:00 2001 From: KyleK Date: Wed, 13 Feb 2019 08:10:21 +0100 Subject: [PATCH] Handle consecutive days opening hours --- .multi-tester.yml | 3 +++ .travis.yml | 13 ++++++++++--- composer.json | 1 + src/OpeningHours.php | 5 ++++- src/OpeningHoursForDay.php | 4 ++-- tests/OpeningHoursTest.php | 27 ++++++++++++--------------- 6 files changed, 32 insertions(+), 21 deletions(-) create mode 100644 .multi-tester.yml diff --git a/.multi-tester.yml b/.multi-tester.yml new file mode 100644 index 0000000..6d55b28 --- /dev/null +++ b/.multi-tester.yml @@ -0,0 +1,3 @@ +cmixin/business-time: + install: default + script: default diff --git a/.travis.yml b/.travis.yml index 779e7ba..b79e8d3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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: @@ -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 diff --git a/composer.json b/composer.json index 75a3e09..a130c95 100644 --- a/composer.json +++ b/composer.json @@ -22,6 +22,7 @@ "php": "^7.0" }, "require-dev": { + "kylekatarnls/multi-tester": "^1.0", "phpunit/phpunit": "^6.4" }, "autoload": { diff --git a/src/OpeningHours.php b/src/OpeningHours.php index ae62360..5d92865 100644 --- a/src/OpeningHours.php +++ b/src/OpeningHours.php @@ -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(); diff --git a/src/OpeningHoursForDay.php b/src/OpeningHoursForDay.php index 35d8bb3..addf20c 100644 --- a/src/OpeningHoursForDay.php +++ b/src/OpeningHoursForDay.php @@ -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); } } @@ -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(); } diff --git a/tests/OpeningHoursTest.php b/tests/OpeningHoursTest.php index 4e49f6e..bdf4d0b 100644 --- a/tests/OpeningHoursTest.php +++ b/tests/OpeningHoursTest.php @@ -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([ @@ -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 */