From d358e83bf94dd37359ce4d52388cf7c8639d98ec Mon Sep 17 00:00:00 2001 From: Mark Huot Date: Mon, 19 Jun 2023 10:59:08 -0400 Subject: [PATCH] fix: allow negative timestamp to be mapped to a datetime Co-authored-by: Nathan Boiron --- .../Object/DateTimeFormatConstructor.php | 2 +- .../Mapping/Object/DateTimeMappingTest.php | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/Mapper/Object/DateTimeFormatConstructor.php b/src/Mapper/Object/DateTimeFormatConstructor.php index 85bcb057..2e2c53f8 100644 --- a/src/Mapper/Object/DateTimeFormatConstructor.php +++ b/src/Mapper/Object/DateTimeFormatConstructor.php @@ -44,7 +44,7 @@ public function __construct(string $format, string ...$formats) /** * @param class-string $className - * @param non-empty-string|positive-int $value + * @param non-empty-string|int $value */ #[DynamicConstructor] public function __invoke(string $className, string|int $value): DateTimeInterface diff --git a/tests/Integration/Mapping/Object/DateTimeMappingTest.php b/tests/Integration/Mapping/Object/DateTimeMappingTest.php index ae55d125..0fb5bf42 100644 --- a/tests/Integration/Mapping/Object/DateTimeMappingTest.php +++ b/tests/Integration/Mapping/Object/DateTimeMappingTest.php @@ -50,6 +50,32 @@ public function test_default_date_constructor_with_valid_timestamp_format_source self::assertSame('1659688380', $result->format('U')); } + public function test_default_date_constructor_with_timestamp_at_0_source_returns_datetime(): void + { + try { + $result = (new MapperBuilder()) + ->mapper() + ->map(DateTimeInterface::class, 0); + } catch (MappingError $error) { + $this->mappingFail($error); + } + + self::assertSame('0', $result->format('U')); + } + + public function test_default_date_constructor_with_a_negative_timestamp_source_returns_datetime(): void + { + try { + $result = (new MapperBuilder()) + ->mapper() + ->map(DateTimeInterface::class, -1); + } catch (MappingError $error) { + $this->mappingFail($error); + } + + self::assertSame('-1', $result->format('U')); + } + public function test_registered_date_constructor_with_valid_source_returns_datetime(): void { try {