Skip to content

Commit

Permalink
fix: allow negative timestamp to be mapped to a datetime
Browse files Browse the repository at this point in the history
Co-authored-by: Nathan Boiron <github@mopolo.net>
  • Loading branch information
markhuot and Mopolo authored Jun 19, 2023
1 parent 5fa1076 commit d358e83
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Mapper/Object/DateTimeFormatConstructor.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct(string $format, string ...$formats)

/**
* @param class-string<DateTime|DateTimeImmutable> $className
* @param non-empty-string|positive-int $value
* @param non-empty-string|int $value
*/
#[DynamicConstructor]
public function __invoke(string $className, string|int $value): DateTimeInterface
Expand Down
26 changes: 26 additions & 0 deletions tests/Integration/Mapping/Object/DateTimeMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit d358e83

Please sign in to comment.