|
60 | 60 | use Symfony\Component\Serializer\Tests\Fixtures\DummyMessageNumberOne;
|
61 | 61 | use Symfony\Component\Serializer\Tests\Fixtures\DummyMessageNumberTwo;
|
62 | 62 | use Symfony\Component\Serializer\Tests\Fixtures\DummyObjectWithEnumConstructor;
|
| 63 | +use Symfony\Component\Serializer\Tests\Fixtures\DummyObjectWithUnionEnumConstructor; |
63 | 64 | use Symfony\Component\Serializer\Tests\Fixtures\FalseBuiltInDummy;
|
| 65 | +use Symfony\Component\Serializer\Tests\Fixtures\IntegerBackedEnumDummy; |
64 | 66 | use Symfony\Component\Serializer\Tests\Fixtures\NormalizableTraversableDummy;
|
65 | 67 | use Symfony\Component\Serializer\Tests\Fixtures\Php74Full;
|
66 | 68 | use Symfony\Component\Serializer\Tests\Fixtures\Php80WithPromotedTypedConstructor;
|
| 69 | +use Symfony\Component\Serializer\Tests\Fixtures\StringBackedEnumDummy; |
67 | 70 | use Symfony\Component\Serializer\Tests\Fixtures\TraversableDummy;
|
68 | 71 | use Symfony\Component\Serializer\Tests\Normalizer\TestDenormalizer;
|
69 | 72 | use Symfony\Component\Serializer\Tests\Normalizer\TestNormalizer;
|
@@ -790,6 +793,52 @@ public function testUnionTypeDeserializableWithoutAllowedExtraAttributes()
|
790 | 793 | ]);
|
791 | 794 | }
|
792 | 795 |
|
| 796 | + /** |
| 797 | + * @requires PHP 8.1 |
| 798 | + */ |
| 799 | + public function testEnumUnionTypeDeserializable() |
| 800 | + { |
| 801 | + $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); |
| 802 | + $extractor = new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()]); |
| 803 | + |
| 804 | + $serializer = new Serializer( |
| 805 | + [ |
| 806 | + new BackedEnumNormalizer(), |
| 807 | + new ObjectNormalizer($classMetadataFactory, null, null, $extractor, new ClassDiscriminatorFromClassMetadata($classMetadataFactory)), |
| 808 | + ], |
| 809 | + ['json' => new JsonEncoder()] |
| 810 | + ); |
| 811 | + |
| 812 | + $actual = $serializer->deserialize('{"sub": 200}', DummyObjectWithUnionEnumConstructor::class, 'json'); |
| 813 | + $this->assertEquals(new DummyObjectWithUnionEnumConstructor(IntegerBackedEnumDummy::SUCCESS), $actual); |
| 814 | + |
| 815 | + $actual = $serializer->deserialize('{"sub": "GET"}', DummyObjectWithUnionEnumConstructor::class, 'json'); |
| 816 | + $this->assertEquals(new DummyObjectWithUnionEnumConstructor(StringBackedEnumDummy::GET), $actual); |
| 817 | + } |
| 818 | + |
| 819 | + /** |
| 820 | + * @requires PHP 8.1 |
| 821 | + */ |
| 822 | + public function testEnumUnionTypeDeserializationWithWrongEnum() |
| 823 | + { |
| 824 | + $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); |
| 825 | + $extractor = new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()]); |
| 826 | + |
| 827 | + $serializer = new Serializer( |
| 828 | + [ |
| 829 | + new BackedEnumNormalizer(), |
| 830 | + new ObjectNormalizer($classMetadataFactory, null, null, $extractor, new ClassDiscriminatorFromClassMetadata($classMetadataFactory)), |
| 831 | + ], |
| 832 | + ['json' => new JsonEncoder()] |
| 833 | + ); |
| 834 | + |
| 835 | + try { |
| 836 | + $serializer->deserialize('{"sub": "INVALID"}', DummyObjectWithUnionEnumConstructor::class, 'json'); |
| 837 | + } catch (\Throwable $th) { |
| 838 | + $this->assertInstanceOf(InvalidArgumentException::class, $th); |
| 839 | + } |
| 840 | + } |
| 841 | + |
793 | 842 | /**
|
794 | 843 | * @requires PHP 8.2
|
795 | 844 | */
|
|
0 commit comments