Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing date objects to caster handle without an error #842

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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