diff --git a/src/Internal/Hydration/SimpleObjectHydrator.php b/src/Internal/Hydration/SimpleObjectHydrator.php index 8c1788e7e52..16c0c25ccaa 100644 --- a/src/Internal/Hydration/SimpleObjectHydrator.php +++ b/src/Internal/Hydration/SimpleObjectHydrator.php @@ -16,6 +16,7 @@ use function array_search; use function count; use function in_array; +use function is_array; use function key; use function reset; use function sprintf; @@ -143,14 +144,21 @@ protected function hydrateRowData(array $row, array &$result) } if ($value !== null && isset($cacheKeyInfo['enumType'])) { - $originalValue = $value; + $originalValue = $currentValue = $value; try { - $value = $this->buildEnum($originalValue, $cacheKeyInfo['enumType']); + if (! is_array($originalValue)) { + $value = $this->buildEnum($originalValue, $cacheKeyInfo['enumType']); + } else { + $value = []; + foreach ($originalValue as $i => $currentValue) { + $value[$i] = $this->buildEnum($currentValue, $cacheKeyInfo['enumType']); + } + } } catch (ValueError $e) { throw MappingException::invalidEnumValue( $entityName, $cacheKeyInfo['fieldName'], - (string) $originalValue, + (string) $currentValue, $cacheKeyInfo['enumType'], $e ); diff --git a/tests/Tests/ORM/Hydration/SimpleObjectHydratorTest.php b/tests/Tests/ORM/Hydration/SimpleObjectHydratorTest.php index 01504102ad2..f4d3a9ac9fd 100644 --- a/tests/Tests/ORM/Hydration/SimpleObjectHydratorTest.php +++ b/tests/Tests/ORM/Hydration/SimpleObjectHydratorTest.php @@ -7,12 +7,15 @@ use Doctrine\DBAL\Types\Type as DBALType; use Doctrine\ORM\Internal\Hydration\HydrationException; use Doctrine\ORM\Internal\Hydration\SimpleObjectHydrator; +use Doctrine\ORM\Mapping\MappingException; use Doctrine\ORM\Query\ResultSetMapping; use Doctrine\Tests\DbalTypes\GH8565EmployeePayloadType; use Doctrine\Tests\DbalTypes\GH8565ManagerPayloadType; use Doctrine\Tests\Mocks\ArrayResultFactory; use Doctrine\Tests\Models\CMS\CmsAddress; use Doctrine\Tests\Models\Company\CompanyPerson; +use Doctrine\Tests\Models\Enums\Scale; +use Doctrine\Tests\Models\Enums\Unit; use Doctrine\Tests\Models\GH8565\GH8565Employee; use Doctrine\Tests\Models\GH8565\GH8565Manager; use Doctrine\Tests\Models\GH8565\GH8565Person; @@ -155,4 +158,28 @@ public function testWrongValuesShouldNotBeConvertedToPhpValue(): void $result = $hydrator->hydrateAll($stmt, $rsm); self::assertEquals($result[0], $expectedEntity); } + + /** + * @requires PHP 8.1 + */ + public function testNotListedValueInEnumArray(): void + { + $this->expectException(MappingException::class); + $this->expectExceptionMessage('Case "unknown_case" is not listed in enum "Doctrine\Tests\Models\Enums\Unit"'); + $rsm = new ResultSetMapping(); + $rsm->addEntityResult(Scale::class, 's'); + $rsm->addFieldResult('s', 's__id', 'id'); + $rsm->addFieldResult('s', 's__supported_units', 'supportedUnits'); + $rsm->addEnumResult('s__supported_units', Unit::class); + $resultSet = [ + [ + 's__id' => 1, + 's__supported_units' => 'g,m,unknown_case', + ], + ]; + + $stmt = ArrayResultFactory::createFromArray($resultSet); + $hydrator = new SimpleObjectHydrator($this->entityManager); + $hydrator->hydrateAll($stmt, $rsm); + } }