From 77df321696a1e286ea3757eb479cff798d3d65ef Mon Sep 17 00:00:00 2001 From: Kalpa Perera Date: Wed, 14 Aug 2024 21:05:55 +0530 Subject: [PATCH] Passing an enum to caster handle without an error --- src/Casts/EnumCast.php | 4 ++++ tests/Casts/EnumCastTest.php | 6 +++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Casts/EnumCast.php b/src/Casts/EnumCast.php index fa3cde72e..d7b7fa1a9 100644 --- a/src/Casts/EnumCast.php +++ b/src/Casts/EnumCast.php @@ -36,6 +36,10 @@ protected function castValue( return Uncastable::create(); } + if ($value instanceof $type) { + return $value; + } + /** @var class-string<\BackedEnum> $type */ try { return $type::from($value); diff --git a/tests/Casts/EnumCastTest.php b/tests/Casts/EnumCastTest.php index 219fca57c..bf3aadc27 100644 --- a/tests/Casts/EnumCastTest.php +++ b/tests/Casts/EnumCastTest.php @@ -11,7 +11,7 @@ $this->caster = new EnumCast(); }); -it('can cast enum', function () { +it('can cast enum', function (mixed $value) { $class = new class () { public DummyBackedEnum $enum; }; @@ -19,12 +19,12 @@ expect( $this->caster->cast( FakeDataStructureFactory::property($class, 'enum'), - 'foo', + $value, [], CreationContextFactory::createFromConfig($class::class)->get() ) )->toEqual(DummyBackedEnum::FOO); -}); +})->with(['foo', DummyBackedEnum::FOO]); it('fails when it cannot cast an enum from value', function () { $class = new class () {