From e5ac74bc45ba5665dc039e93f8ec2012d28fa875 Mon Sep 17 00:00:00 2001 From: Andrey Postal Date: Tue, 5 Aug 2025 12:33:35 -0300 Subject: [PATCH] Improve handling of invalid enum values Throws an InvalidEnumValueException when an invalid enum value is provided. --- phpunit.xml.dist | 2 ++ src/Exceptions/InvalidEnumValueException.php | 8 ++++++++ src/SimpleHydrator.php | 10 ++++++++-- tests/HydratorTest.php | 18 +++++++++++++++++- tests/SerializerTest.php | 2 -- 5 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 src/Exceptions/InvalidEnumValueException.php diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 3c60f8a..5746f6c 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -8,6 +8,8 @@ processIsolation= "false" stopOnFailure="false" bootstrap = "tests/bootstrap.php" + + displayDetailsOnTestsThatTriggerWarnings="true" > diff --git a/src/Exceptions/InvalidEnumValueException.php b/src/Exceptions/InvalidEnumValueException.php new file mode 100644 index 0000000..f167a77 --- /dev/null +++ b/src/Exceptions/InvalidEnumValueException.php @@ -0,0 +1,8 @@ +isEnum()) { - return new Payload(data: call_user_func($type.'::tryFrom', $value)); + $data = call_user_func($type.'::tryFrom', $value); + $error = null; + if ($data === null) { + $error = new InvalidEnumValueException("Invalid enum value '{$value}' for type '{$type}'"); + } + return new Payload(data: $data, error: $error); } // Recursively hydrate child/internal value objects diff --git a/tests/HydratorTest.php b/tests/HydratorTest.php index 2fcf5f3..14cc6dd 100644 --- a/tests/HydratorTest.php +++ b/tests/HydratorTest.php @@ -3,6 +3,7 @@ use Andrey\PancakeObject\Attributes\SkipItem; use Andrey\PancakeObject\Attributes\ValueObject; use Andrey\PancakeObject\Attributes\Item; +use Andrey\PancakeObject\Exceptions\InvalidEnumValueException; use Andrey\PancakeObject\Payload; use Andrey\PancakeObject\SimpleHydrator; use PHPUnit\Framework\Attributes\CoversClass; @@ -63,7 +64,6 @@ public function testSimpleHydrate(): void $this->assertFalse($obj->bool); $this->assertNull($obj->nullableInt); - $this->assertInstanceOf(ChildObject::class, $obj->singleChild); $this->assertIsArray($obj->singleChild->andImAnArrayOfInt); $this->assertSame($data['single_child']['and_im_an_array_of_int'], $obj->singleChild->andImAnArrayOfInt); @@ -177,4 +177,20 @@ public function testDefaultValueFromItem(): void $this->assertCount(0, $object->singleChild->andImAnArrayOfInt); $this->assertEquals('default child name', $object->singleChild->iHaveAName); } + + /** + * @throws ReflectionException + */ + public function testInvalidEnumValue(): void + { + $data = [ + 'enum' => 'C', + ]; + + + $this->expectException(InvalidEnumValueException::class); + + $hydrator = new SimpleHydrator(); + $hydrator->hydrate($data, TestObject::class); + } } diff --git a/tests/SerializerTest.php b/tests/SerializerTest.php index 02a6c10..b20ee60 100644 --- a/tests/SerializerTest.php +++ b/tests/SerializerTest.php @@ -50,8 +50,6 @@ public function testSimpleSerialize(): void $this->assertIsArray($data["single_child"]["and_im_an_array_of_int"]); $this->assertSame([4, 5, 6], $data["single_child"]["and_im_an_array_of_int"]); - $this->assertNull($data["nullable_int"]); - $this->assertIsArray($data["array_of_children"]); $this->assertCount(2, $data["array_of_children"]);