|
36 | 36 | use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
|
37 | 37 | use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
|
38 | 38 | use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
|
| 39 | +use Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer; |
39 | 40 | use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
|
40 | 41 | use Symfony\Component\Serializer\Normalizer\DataUriNormalizer;
|
41 | 42 | use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
|
|
58 | 59 | use Symfony\Component\Serializer\Tests\Fixtures\DummyMessageInterface;
|
59 | 60 | use Symfony\Component\Serializer\Tests\Fixtures\DummyMessageNumberOne;
|
60 | 61 | use Symfony\Component\Serializer\Tests\Fixtures\DummyMessageNumberTwo;
|
| 62 | +use Symfony\Component\Serializer\Tests\Fixtures\DummyObjectWithEnumConstructor; |
61 | 63 | use Symfony\Component\Serializer\Tests\Fixtures\FalseBuiltInDummy;
|
62 | 64 | use Symfony\Component\Serializer\Tests\Fixtures\NormalizableTraversableDummy;
|
63 | 65 | use Symfony\Component\Serializer\Tests\Fixtures\Php74Full;
|
@@ -1173,6 +1175,69 @@ public function testCollectDenormalizationErrorsWithConstructor(?ClassMetadataFa
|
1173 | 1175 | $this->assertSame($expected, $exceptionsAsArray);
|
1174 | 1176 | }
|
1175 | 1177 |
|
| 1178 | + /** |
| 1179 | + * @requires PHP 8.1 |
| 1180 | + */ |
| 1181 | + public function testCollectDenormalizationErrorsWithEnumConstructor() |
| 1182 | + { |
| 1183 | + $serializer = new Serializer( |
| 1184 | + [ |
| 1185 | + new BackedEnumNormalizer(), |
| 1186 | + new ObjectNormalizer(), |
| 1187 | + ], |
| 1188 | + ['json' => new JsonEncoder()] |
| 1189 | + ); |
| 1190 | + |
| 1191 | + try { |
| 1192 | + $serializer->deserialize('{"invalid": "GET"}', DummyObjectWithEnumConstructor::class, 'json', [ |
| 1193 | + DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true, |
| 1194 | + ]); |
| 1195 | + } catch (\Throwable $th) { |
| 1196 | + $this->assertInstanceOf(PartialDenormalizationException::class, $th); |
| 1197 | + } |
| 1198 | + |
| 1199 | + $exceptionsAsArray = array_map(function (NotNormalizableValueException $e): array { |
| 1200 | + return [ |
| 1201 | + 'currentType' => $e->getCurrentType(), |
| 1202 | + 'useMessageForUser' => $e->canUseMessageForUser(), |
| 1203 | + 'message' => $e->getMessage(), |
| 1204 | + ]; |
| 1205 | + }, $th->getErrors()); |
| 1206 | + |
| 1207 | + $expected = [ |
| 1208 | + [ |
| 1209 | + 'currentType' => 'array', |
| 1210 | + 'useMessageForUser' => true, |
| 1211 | + 'message' => 'Failed to create object because the class misses the "get" property.', |
| 1212 | + ], |
| 1213 | + ]; |
| 1214 | + |
| 1215 | + $this->assertSame($expected, $exceptionsAsArray); |
| 1216 | + } |
| 1217 | + |
| 1218 | + /** |
| 1219 | + * @requires PHP 8.1 |
| 1220 | + */ |
| 1221 | + public function testNoCollectDenormalizationErrorsWithWrongEnum() |
| 1222 | + { |
| 1223 | + $serializer = new Serializer( |
| 1224 | + [ |
| 1225 | + new BackedEnumNormalizer(), |
| 1226 | + new ObjectNormalizer(), |
| 1227 | + ], |
| 1228 | + ['json' => new JsonEncoder()] |
| 1229 | + ); |
| 1230 | + |
| 1231 | + try { |
| 1232 | + $serializer->deserialize('{"get": "invalid"}', DummyObjectWithEnumConstructor::class, 'json', [ |
| 1233 | + DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true, |
| 1234 | + ]); |
| 1235 | + } catch (\Throwable $th) { |
| 1236 | + $this->assertNotInstanceOf(PartialDenormalizationException::class, $th); |
| 1237 | + $this->assertInstanceOf(InvalidArgumentException::class, $th); |
| 1238 | + } |
| 1239 | + } |
| 1240 | + |
1176 | 1241 | public function provideCollectDenormalizationErrors()
|
1177 | 1242 | {
|
1178 | 1243 | return [
|
|
0 commit comments