From 44314fad669c4d70387aa01d5883c4f1b66c4e01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ale=C5=A1?= Date: Mon, 19 May 2025 08:57:42 +0200 Subject: [PATCH] datetime exception fix --- src/DateTimes.php | 4 +--- src/Dates.php | 4 +--- tests/DateTimesTest.phpt | 13 +++++++++++++ tests/DatesTest.phpt | 13 +++++++++++++ 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/DateTimes.php b/src/DateTimes.php index ac6bfef..ad42850 100644 --- a/src/DateTimes.php +++ b/src/DateTimes.php @@ -35,9 +35,7 @@ final public static function from( } } - throw new InvalidTypeException( - 'Value ' . $value . ' must be string in ' . DateTimeFormat::DATETIME . ' format' - ); + throw InvalidTypeException::typeError(DateTimeFormat::DATETIME . ' format', $value); } /** diff --git a/src/Dates.php b/src/Dates.php index 19eb209..a171daf 100644 --- a/src/Dates.php +++ b/src/Dates.php @@ -24,9 +24,7 @@ final public static function from( } } - throw new InvalidTypeException( - 'Value ' . $value . ' must be string in ' . DateTimeFormat::DATE . ' format' - ); + throw InvalidTypeException::typeError(DateTimeFormat::DATE . ' format', $value); } /** diff --git a/tests/DateTimesTest.phpt b/tests/DateTimesTest.phpt index a94e3cf..c3a4b3b 100644 --- a/tests/DateTimesTest.phpt +++ b/tests/DateTimesTest.phpt @@ -43,6 +43,19 @@ final class DateTimesTest extends TestCase InvalidTypeException::class ); + Assert::throws( + static function (): void { + DateTimes::from( + [ + 'date' => '1979-02-23 00:00:00.000000', + 'timezone' => 'Europe/Prague', + 'timezone_type' => 3, + ] + ); + }, + InvalidTypeException::class + ); + $d = DateTimes::from('2000-01-01 00:00:00.123456'); Assert::equal('2000-01-01 00:00:00', DateTimeFormatter::format($d)); diff --git a/tests/DatesTest.phpt b/tests/DatesTest.phpt index 7bf2fab..23d096e 100644 --- a/tests/DatesTest.phpt +++ b/tests/DatesTest.phpt @@ -41,6 +41,19 @@ final class DatesTest extends TestCase }, InvalidTypeException::class ); + + Assert::throws( + static function (): void { + Dates::from( + [ + 'date' => '1979-02-23 00:00:00.000000', + 'timezone' => 'Europe/Prague', + 'timezone_type' => 3, + ] + ); + }, + InvalidTypeException::class + ); } public function testExtract(): void