Skip to content

Commit

Permalink
Passing date objects to caster handle without an error
Browse files Browse the repository at this point in the history
  • Loading branch information
lkmadushan committed Aug 14, 2024
1 parent e34dabd commit 6fe84f4
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Casts/DateTimeInterfaceCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected function castValue(
$datetime = $formats
->map(fn (string $format) => rescue(fn () => $type::createFromFormat(
$format,
(string) $value,
$value instanceof DateTimeInterface ? $value->format($format) : (string) $value,
isset($this->timeZone) ? new DateTimeZone($this->timeZone) : null
), report: false))
->first(fn ($value) => (bool) $value);
Expand Down
72 changes: 72 additions & 0 deletions tests/Casts/DateTimeInterfaceCastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,78 @@
CreationContextFactory::createFromConfig($class::class)->get()
)
)->toEqual(new DateTimeImmutable('19-05-1994 00:00:00'));

expect(
$caster->cast(
FakeDataStructureFactory::property($class, 'carbon'),
new Carbon('19-05-1994 00:00:00'),
[],
CreationContextFactory::createFromConfig($class::class)->get()
)
)->toEqual(new Carbon('19-05-1994 00:00:00'));

expect(
$caster->cast(
FakeDataStructureFactory::property($class, 'carbonImmutable'),
new CarbonImmutable('19-05-1994 00:00:00'),
[],
CreationContextFactory::createFromConfig($class::class)->get()
)
)->toEqual(new CarbonImmutable('19-05-1994 00:00:00'));

expect(
$caster->cast(
FakeDataStructureFactory::property($class, 'dateTime'),
new DateTime('19-05-1994 00:00:00'),
[],
CreationContextFactory::createFromConfig($class::class)->get()
)
)->toEqual(new DateTime('19-05-1994 00:00:00'));

expect(
$caster->cast(
FakeDataStructureFactory::property($class, 'dateTimeImmutable'),
new DateTimeImmutable('19-05-1994 00:00:00'),
[],
CreationContextFactory::createFromConfig($class::class)->get()
)
)->toEqual(new DateTimeImmutable('19-05-1994 00:00:00'));

expect(
$caster->cast(
FakeDataStructureFactory::property($class, 'carbon'),
new DateTime('19-05-1994 00:00:00'),
[],
CreationContextFactory::createFromConfig($class::class)->get()
)
)->toEqual(new Carbon('19-05-1994 00:00:00'));

expect(
$caster->cast(
FakeDataStructureFactory::property($class, 'carbonImmutable'),
new DateTimeImmutable('19-05-1994 00:00:00'),
[],
CreationContextFactory::createFromConfig($class::class)->get()
)
)->toEqual(new CarbonImmutable('19-05-1994 00:00:00'));

expect(
$caster->cast(
FakeDataStructureFactory::property($class, 'dateTime'),
new Carbon('19-05-1994 00:00:00'),
[],
CreationContextFactory::createFromConfig($class::class)->get()
)
)->toEqual(new DateTime('19-05-1994 00:00:00'));

expect(
$caster->cast(
FakeDataStructureFactory::property($class, 'dateTimeImmutable'),
new CarbonImmutable('19-05-1994 00:00:00'),
[],
CreationContextFactory::createFromConfig($class::class)->get()
)
)->toEqual(new DateTimeImmutable('19-05-1994 00:00:00'));
});

it('fails when it cannot cast a date into the correct format', function () {
Expand Down

0 comments on commit 6fe84f4

Please sign in to comment.