From 61fb2825ad4b082d946dd5e6501ef282809f4d7c Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Sat, 25 Nov 2023 15:05:44 +0100 Subject: [PATCH] Wrap Day ValueError exception into InvalidDayName when calling fromName() --- src/Day.php | 8 +++++++- src/Exceptions/InvalidDayName.php | 9 +++++++-- tests/OpeningHoursFillTest.php | 3 ++- tests/OpeningHoursTest.php | 16 ++++++++++++++++ tests/PreciseTimeTest.php | 11 +++++++++++ 5 files changed, 43 insertions(+), 4 deletions(-) diff --git a/src/Day.php b/src/Day.php index 2a18c28..cfbc263 100644 --- a/src/Day.php +++ b/src/Day.php @@ -3,6 +3,8 @@ namespace Spatie\OpeningHours; use DateTimeInterface; +use Spatie\OpeningHours\Exceptions\InvalidDayName; +use ValueError; enum Day: string { @@ -21,7 +23,11 @@ public static function onDateTime(DateTimeInterface $dateTime): self public static function fromName(string $day): self { - return self::from(strtolower($day)); + try { + return self::from(strtolower($day)); + } catch (ValueError $exception) { + throw InvalidDayName::invalidDayName($day, $exception); + } } public function toISO(): int diff --git a/src/Exceptions/InvalidDayName.php b/src/Exceptions/InvalidDayName.php index 11f17a2..377d307 100644 --- a/src/Exceptions/InvalidDayName.php +++ b/src/Exceptions/InvalidDayName.php @@ -2,10 +2,15 @@ namespace Spatie\OpeningHours\Exceptions; +use Throwable; + class InvalidDayName extends Exception { - public static function invalidDayName(string $name): self + public static function invalidDayName(string $name, ?Throwable $previous = null): self { - return new self("Day `{$name}` isn't a valid day name. Valid day names are lowercase english words, e.g. `monday`, `thursday`."); + return new self( + "Day `{$name}` isn't a valid day name. Valid day names are lowercase english words, e.g. `monday`, `thursday`.", + previous: $previous, + ); } } diff --git a/tests/OpeningHoursFillTest.php b/tests/OpeningHoursFillTest.php index d93a4cc..e89a4b8 100644 --- a/tests/OpeningHoursFillTest.php +++ b/tests/OpeningHoursFillTest.php @@ -7,6 +7,7 @@ use PHPUnit\Framework\TestCase; use Spatie\OpeningHours\Day; use Spatie\OpeningHours\Exceptions\InvalidDate; +use Spatie\OpeningHours\Exceptions\InvalidDayName; use Spatie\OpeningHours\OpeningHours; use Spatie\OpeningHours\OpeningHoursForDay; use Spatie\OpeningHours\TimeRange; @@ -128,7 +129,7 @@ public function it_handles_day_names_in_a_case_insensitive_manner() /** @test */ public function it_will_throw_an_exception_when_using_an_invalid_day_name() { - $this->expectException(ValueError::class); + $this->expectExceptionObject(InvalidDayName::invalidDayName('mmmmonday')); OpeningHours::create(['mmmmonday' => ['09:00-18:00']]); } diff --git a/tests/OpeningHoursTest.php b/tests/OpeningHoursTest.php index f1f7f12..87dbcb1 100644 --- a/tests/OpeningHoursTest.php +++ b/tests/OpeningHoursTest.php @@ -7,6 +7,7 @@ use DateTimeZone; use InvalidArgumentException; use PHPUnit\Framework\TestCase; +use Spatie\OpeningHours\Day; use Spatie\OpeningHours\Exceptions\InvalidDateRange; use Spatie\OpeningHours\Exceptions\MaximumLimitExceeded; use Spatie\OpeningHours\Exceptions\SearchLimitReached; @@ -953,6 +954,21 @@ public function it_can_determine_that_its_open_now() $this->assertTrue($openingHours->isOpen()); } + /** @test */ + public function it_can_use_day_enum() + { + $openingHours = new class () extends OpeningHours { + public readonly array $days; + + public function __construct() + { + $this->days = $this->readDatesRange(Day::MONDAY); + } + }; + + $this->assertSame(['monday'], $openingHours->days); + } + /** @test */ public function it_can_determine_that_its_closed_now() { diff --git a/tests/PreciseTimeTest.php b/tests/PreciseTimeTest.php index 2d8b652..a4d58aa 100644 --- a/tests/PreciseTimeTest.php +++ b/tests/PreciseTimeTest.php @@ -4,6 +4,7 @@ use DateTimeImmutable; use DateTimeZone; +use InvalidArgumentException; use PHPUnit\Framework\TestCase; use Spatie\OpeningHours\PreciseTime; @@ -69,4 +70,14 @@ public function it_can_output_hours_and_minutes() $this->assertSame(23, $date->hours()); $this->assertSame(32, $date->minutes()); } + + /** @test */ + public function it_cannot_have_date_reference_point() + { + $this->expectExceptionObject(new InvalidArgumentException( + PreciseTime::class.' does not support date reference point', + )); + + PreciseTime::fromString('2022-08-07 23:32:58.123456 America/Toronto', date: new DateTimeImmutable()); + } }