|
14 | 14 | use Doctrine\Common\Annotations\AnnotationReader;
|
15 | 15 | use PHPUnit\Framework\TestCase;
|
16 | 16 | use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
|
| 17 | +use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor; |
| 18 | +use Symfony\Component\PropertyInfo\PropertyInfoExtractor; |
17 | 19 | use Symfony\Component\PropertyInfo\Type;
|
18 | 20 | use Symfony\Component\Serializer\Annotation\Ignore;
|
19 | 21 | use Symfony\Component\Serializer\Exception\ExtraAttributesException;
|
20 | 22 | use Symfony\Component\Serializer\Exception\InvalidArgumentException;
|
21 | 23 | use Symfony\Component\Serializer\Exception\LogicException;
|
| 24 | +use Symfony\Component\Serializer\Exception\MissingConstructorArgumentsException; |
22 | 25 | use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
|
23 | 26 | use Symfony\Component\Serializer\Mapping\ClassDiscriminatorFromClassMetadata;
|
24 | 27 | use Symfony\Component\Serializer\Mapping\ClassDiscriminatorMapping;
|
|
30 | 33 | use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
|
31 | 34 | use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
|
32 | 35 | use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
|
| 36 | +use Symfony\Component\Serializer\Normalizer\CustomNormalizer; |
33 | 37 | use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
34 | 38 | use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
|
35 | 39 | use Symfony\Component\Serializer\Serializer;
|
|
40 | 44 | use Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummySecondChild;
|
41 | 45 | use Symfony\Component\Serializer\Tests\Fixtures\DummyFirstChildQuux;
|
42 | 46 | use Symfony\Component\Serializer\Tests\Fixtures\DummySecondChildQuux;
|
| 47 | +use Symfony\Component\Serializer\Tests\Fixtures\DummyString; |
| 48 | +use Symfony\Component\Serializer\Tests\Fixtures\DummyWithNotNormalizable; |
| 49 | +use Symfony\Component\Serializer\Tests\Fixtures\DummyWithObjectOrBool; |
| 50 | +use Symfony\Component\Serializer\Tests\Fixtures\DummyWithObjectOrNull; |
| 51 | +use Symfony\Component\Serializer\Tests\Fixtures\DummyWithStringObject; |
43 | 52 |
|
44 | 53 | class AbstractObjectNormalizerTest extends TestCase
|
45 | 54 | {
|
@@ -453,6 +462,60 @@ public function testNormalizeWithIgnoreAnnotationAndPrivateProperties()
|
453 | 462 |
|
454 | 463 | $this->assertSame(['foo' => 'foo'], $serializer->normalize(new ObjectDummyWithIgnoreAnnotationAndPrivateProperty()));
|
455 | 464 | }
|
| 465 | + |
| 466 | + /** |
| 467 | + * @requires PHP 8 |
| 468 | + */ |
| 469 | + public function testDenormalizeUntypedFormat() |
| 470 | + { |
| 471 | + $serializer = new Serializer([new ObjectNormalizer(null, null, null, new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()]))]); |
| 472 | + $actual = $serializer->denormalize(['value' => ''], DummyWithObjectOrNull::class, 'xml'); |
| 473 | + |
| 474 | + $this->assertEquals(new DummyWithObjectOrNull(null), $actual); |
| 475 | + } |
| 476 | + |
| 477 | + /** |
| 478 | + * @requires PHP 8 |
| 479 | + */ |
| 480 | + public function testDenormalizeUntypedFormatNotNormalizable() |
| 481 | + { |
| 482 | + $this->expectException(NotNormalizableValueException::class); |
| 483 | + $serializer = new Serializer([new CustomNormalizer(), new ObjectNormalizer(null, null, null, new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()]))]); |
| 484 | + $serializer->denormalize(['value' => 'test'], DummyWithNotNormalizable::class, 'xml'); |
| 485 | + } |
| 486 | + |
| 487 | + /** |
| 488 | + * @requires PHP 8 |
| 489 | + */ |
| 490 | + public function testDenormalizeUntypedFormatMissingArg() |
| 491 | + { |
| 492 | + $this->expectException(MissingConstructorArgumentsException::class); |
| 493 | + $serializer = new Serializer([new ObjectNormalizer(null, null, null, new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()]))]); |
| 494 | + $serializer->denormalize(['value' => 'invalid'], DummyWithObjectOrNull::class, 'xml'); |
| 495 | + } |
| 496 | + |
| 497 | + /** |
| 498 | + * @requires PHP 8 |
| 499 | + */ |
| 500 | + public function testDenormalizeUntypedFormatScalar() |
| 501 | + { |
| 502 | + $serializer = new Serializer([new ObjectNormalizer(null, null, null, new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()]))]); |
| 503 | + $actual = $serializer->denormalize(['value' => 'false'], DummyWithObjectOrBool::class, 'xml'); |
| 504 | + |
| 505 | + $this->assertEquals(new DummyWithObjectOrBool(false), $actual); |
| 506 | + } |
| 507 | + |
| 508 | + /** |
| 509 | + * @requires PHP 8 |
| 510 | + */ |
| 511 | + public function testDenormalizeUntypedStringObject() |
| 512 | + { |
| 513 | + $serializer = new Serializer([new CustomNormalizer(), new ObjectNormalizer(null, null, null, new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()]))]); |
| 514 | + $actual = $serializer->denormalize(['value' => ''], DummyWithStringObject::class, 'xml'); |
| 515 | + |
| 516 | + $this->assertEquals(new DummyWithStringObject(new DummyString()), $actual); |
| 517 | + $this->assertEquals('', $actual->value->value); |
| 518 | + } |
456 | 519 | }
|
457 | 520 |
|
458 | 521 | class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer
|
|
0 commit comments