Skip to content

Commit

Permalink
cover failing paths of Clock::at()
Browse files Browse the repository at this point in the history
  • Loading branch information
Baptouuuu committed Nov 23, 2024
1 parent a24f8f6 commit 1c01aae
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
26 changes: 26 additions & 0 deletions proofs/clock.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Innmind\TimeContinuum\{
Clock,
Period,
Format,
};
use Fixtures\Innmind\TimeContinuum\PointInTime;
use Innmind\BlackBox\Set;
Expand Down Expand Up @@ -91,4 +92,29 @@ static function($assert, $point) {
);
},
);

yield proof(
'Clock::at() returns nothing for invalid strings',
given(
Set\Unicode::strings(),
Set\Elements::of(
null,
Format::cookie(),
Format::iso8601(),
Format::rfc1036(),
Format::rfc1123(),
Format::rfc2822(),
Format::rfc822(),
Format::rfc850(),
Format::rss(),
Format::w3c(),
),
),
static fn($assert, $string, $format) => $assert->null(
Clock::live()->at($string, $format)->match(
static fn($point) => $point,
static fn() => null,
),
),
);
};
4 changes: 2 additions & 2 deletions src/Clock/Live.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function at(string $date, Format $format = null): Maybe
try {
/** @psalm-suppress ImpureMethodCall */
$datetime = \DateTimeImmutable::createFromFormat($format->toString(), $date);
} catch (\ValueError) {
} catch (\Throwable) {
/** @var Maybe<PointInTime> */
return Maybe::nothing();
}
Expand All @@ -83,7 +83,7 @@ public function at(string $date, Format $format = null): Maybe
try {
/** @psalm-suppress ImpureMethodCall */
$datetime = new \DateTimeImmutable($date);
} catch (\ValueError) {
} catch (\Throwable) {
/** @var Maybe<PointInTime> */
return Maybe::nothing();
}
Expand Down

0 comments on commit 1c01aae

Please sign in to comment.