Skip to content

Commit

Permalink
Wrap Day ValueError exception into InvalidDayName when calling fromNa…
Browse files Browse the repository at this point in the history
…me()
  • Loading branch information
kylekatarnls committed Nov 25, 2023
1 parent 45999a6 commit 61fb282
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/Day.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Spatie\OpeningHours;

use DateTimeInterface;
use Spatie\OpeningHours\Exceptions\InvalidDayName;
use ValueError;

enum Day: string
{
Expand All @@ -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
Expand Down
9 changes: 7 additions & 2 deletions src/Exceptions/InvalidDayName.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
}
3 changes: 2 additions & 1 deletion tests/OpeningHoursFillTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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']]);
}
Expand Down
16 changes: 16 additions & 0 deletions tests/OpeningHoursTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
{
Expand Down
11 changes: 11 additions & 0 deletions tests/PreciseTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DateTimeImmutable;
use DateTimeZone;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Spatie\OpeningHours\PreciseTime;

Expand Down Expand Up @@ -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());
}
}

0 comments on commit 61fb282

Please sign in to comment.