|
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;
|
@@ -1167,6 +1169,69 @@ public function testCollectDenormalizationErrorsWithConstructor(?ClassMetadataFa
|
1167 | 1169 | $this->assertSame($expected, $exceptionsAsArray);
|
1168 | 1170 | }
|
1169 | 1171 |
|
| 1172 | + /** |
| 1173 | + * @requires PHP 8.1 |
| 1174 | + */ |
| 1175 | + public function testCollectDenormalizationErrorsWithEnumConstructor() |
| 1176 | + { |
| 1177 | + $serializer = new Serializer( |
| 1178 | + [ |
| 1179 | + new BackedEnumNormalizer(), |
| 1180 | + new ObjectNormalizer(), |
| 1181 | + ], |
| 1182 | + ['json' => new JsonEncoder()] |
| 1183 | + ); |
| 1184 | + |
| 1185 | + try { |
| 1186 | + $serializer->deserialize('{"invalid": "GET"}', DummyObjectWithEnumConstructor::class, 'json', [ |
| 1187 | + DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true, |
| 1188 | + ]); |
| 1189 | + } catch (\Throwable $th) { |
| 1190 | + $this->assertInstanceOf(PartialDenormalizationException::class, $th); |
| 1191 | + } |
| 1192 | + |
| 1193 | + $exceptionsAsArray = array_map(function (NotNormalizableValueException $e): array { |
| 1194 | + return [ |
| 1195 | + 'currentType' => $e->getCurrentType(), |
| 1196 | + 'useMessageForUser' => $e->canUseMessageForUser(), |
| 1197 | + 'message' => $e->getMessage(), |
| 1198 | + ]; |
| 1199 | + }, $th->getErrors()); |
| 1200 | + |
| 1201 | + $expected = [ |
| 1202 | + [ |
| 1203 | + 'currentType' => 'array', |
| 1204 | + 'useMessageForUser' => true, |
| 1205 | + 'message' => 'Failed to create object because the class misses the "get" property.', |
| 1206 | + ], |
| 1207 | + ]; |
| 1208 | + |
| 1209 | + $this->assertSame($expected, $exceptionsAsArray); |
| 1210 | + } |
| 1211 | + |
| 1212 | + /** |
| 1213 | + * @requires PHP 8.1 |
| 1214 | + */ |
| 1215 | + public function testNoCollectDenormalizationErrorsWithWrongEnum() |
| 1216 | + { |
| 1217 | + $serializer = new Serializer( |
| 1218 | + [ |
| 1219 | + new BackedEnumNormalizer(), |
| 1220 | + new ObjectNormalizer(), |
| 1221 | + ], |
| 1222 | + ['json' => new JsonEncoder()] |
| 1223 | + ); |
| 1224 | + |
| 1225 | + try { |
| 1226 | + $serializer->deserialize('{"get": "invalid"}', DummyObjectWithEnumConstructor::class, 'json', [ |
| 1227 | + DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true, |
| 1228 | + ]); |
| 1229 | + } catch (\Throwable $th) { |
| 1230 | + $this->assertNotInstanceOf(PartialDenormalizationException::class, $th); |
| 1231 | + $this->assertInstanceOf(InvalidArgumentException::class, $th); |
| 1232 | + } |
| 1233 | + } |
| 1234 | + |
1170 | 1235 | public function provideCollectDenormalizationErrors()
|
1171 | 1236 | {
|
1172 | 1237 | return [
|
|
0 commit comments