Skip to content

Commit 9bd7242

Browse files
maximecolinHypeMC
authored andcommitted
Introduce testNotListedValueInEnumArray
1 parent fff085b commit 9bd7242

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

src/Internal/Hydration/SimpleObjectHydrator.php

+11-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use function array_search;
1717
use function count;
1818
use function in_array;
19+
use function is_array;
1920
use function key;
2021
use function reset;
2122
use function sprintf;
@@ -143,14 +144,21 @@ protected function hydrateRowData(array $row, array &$result)
143144
}
144145

145146
if ($value !== null && isset($cacheKeyInfo['enumType'])) {
146-
$originalValue = $value;
147+
$originalValue = $currentValue = $value;
147148
try {
148-
$value = $this->buildEnum($originalValue, $cacheKeyInfo['enumType']);
149+
if (! is_array($originalValue)) {
150+
$value = $this->buildEnum($originalValue, $cacheKeyInfo['enumType']);
151+
} else {
152+
$value = [];
153+
foreach ($originalValue as $i => $currentValue) {
154+
$value[$i] = $this->buildEnum($currentValue, $cacheKeyInfo['enumType']);
155+
}
156+
}
149157
} catch (ValueError $e) {
150158
throw MappingException::invalidEnumValue(
151159
$entityName,
152160
$cacheKeyInfo['fieldName'],
153-
(string) $originalValue,
161+
(string) $currentValue,
154162
$cacheKeyInfo['enumType'],
155163
$e
156164
);

tests/Tests/ORM/Hydration/SimpleObjectHydratorTest.php

+27
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@
77
use Doctrine\DBAL\Types\Type as DBALType;
88
use Doctrine\ORM\Internal\Hydration\HydrationException;
99
use Doctrine\ORM\Internal\Hydration\SimpleObjectHydrator;
10+
use Doctrine\ORM\Mapping\MappingException;
1011
use Doctrine\ORM\Query\ResultSetMapping;
1112
use Doctrine\Tests\DbalTypes\GH8565EmployeePayloadType;
1213
use Doctrine\Tests\DbalTypes\GH8565ManagerPayloadType;
1314
use Doctrine\Tests\Mocks\ArrayResultFactory;
1415
use Doctrine\Tests\Models\CMS\CmsAddress;
1516
use Doctrine\Tests\Models\Company\CompanyPerson;
17+
use Doctrine\Tests\Models\Enums\Scale;
18+
use Doctrine\Tests\Models\Enums\Unit;
1619
use Doctrine\Tests\Models\GH8565\GH8565Employee;
1720
use Doctrine\Tests\Models\GH8565\GH8565Manager;
1821
use Doctrine\Tests\Models\GH8565\GH8565Person;
@@ -155,4 +158,28 @@ public function testWrongValuesShouldNotBeConvertedToPhpValue(): void
155158
$result = $hydrator->hydrateAll($stmt, $rsm);
156159
self::assertEquals($result[0], $expectedEntity);
157160
}
161+
162+
/**
163+
* @requires PHP 8.1
164+
*/
165+
public function testNotListedValueInEnumArray(): void
166+
{
167+
$this->expectException(MappingException::class);
168+
$this->expectExceptionMessage('Case "unknown_case" is not listed in enum "Doctrine\Tests\Models\Enums\Unit"');
169+
$rsm = new ResultSetMapping();
170+
$rsm->addEntityResult(Scale::class, 's');
171+
$rsm->addFieldResult('s', 's__id', 'id');
172+
$rsm->addFieldResult('s', 's__supported_units', 'supportedUnits');
173+
$rsm->addEnumResult('s__supported_units', Unit::class);
174+
$resultSet = [
175+
[
176+
's__id' => 1,
177+
's__supported_units' => 'g,m,unknown_case',
178+
],
179+
];
180+
181+
$stmt = ArrayResultFactory::createFromArray($resultSet);
182+
$hydrator = new SimpleObjectHydrator($this->entityManager);
183+
$hydrator->hydrateAll($stmt, $rsm);
184+
}
158185
}

0 commit comments

Comments
 (0)