12
12
namespace Symfony \Component \Serializer \Tests \Normalizer ;
13
13
14
14
use Doctrine \Common \Annotations \AnnotationReader ;
15
+ use Symfony \Component \PropertyInfo \Extractor \PhpDocExtractor ;
15
16
use Symfony \Component \PropertyInfo \Extractor \ReflectionExtractor ;
17
+ use Symfony \Component \PropertyInfo \PropertyInfoExtractor ;
16
18
use Symfony \Component \Serializer \Exception \UnexpectedValueException ;
17
19
use Symfony \Component \Serializer \NameConverter \CamelCaseToSnakeCaseNameConverter ;
20
+ use Symfony \Component \Serializer \Normalizer \ArrayDenormalizer ;
18
21
use Symfony \Component \Serializer \Normalizer \DateTimeNormalizer ;
19
22
use Symfony \Component \Serializer \Normalizer \ObjectNormalizer ;
20
23
use Symfony \Component \Serializer \Serializer ;
@@ -569,13 +572,21 @@ public function testThrowUnexpectedValueException()
569
572
570
573
public function testDenomalizeRecursive ()
571
574
{
572
- $ normalizer = new ObjectNormalizer (null , null , null , new ReflectionExtractor ());
573
- $ serializer = new Serializer (array (new DateTimeNormalizer (), $ normalizer ));
575
+ $ extractor = new PropertyInfoExtractor (array (), array (new PhpDocExtractor (), new ReflectionExtractor ()));
576
+ $ normalizer = new ObjectNormalizer (null , null , null , $ extractor );
577
+ $ serializer = new Serializer (array (new ArrayDenormalizer (), new DateTimeNormalizer (), $ normalizer ));
578
+
579
+ $ obj = $ serializer ->denormalize (array (
580
+ 'inner ' => array ('foo ' => 'foo ' , 'bar ' => 'bar ' ),
581
+ 'date ' => '1988/01/21 ' ,
582
+ 'inners ' => array (array ('foo ' => 1 ), array ('foo ' => 2 )),
583
+ ), ObjectOuter::class);
574
584
575
- $ obj = $ serializer ->denormalize (array ('inner ' => array ('foo ' => 'foo ' , 'bar ' => 'bar ' ), 'date ' => '1988/01/21 ' ), ObjectOuter::class);
576
585
$ this ->assertEquals ('foo ' , $ obj ->getInner ()->foo );
577
586
$ this ->assertEquals ('bar ' , $ obj ->getInner ()->bar );
578
587
$ this ->assertEquals ('1988-01-21 ' , $ obj ->getDate ()->format ('Y-m-d ' ));
588
+ $ this ->assertEquals (1 , $ obj ->getInners ()[0 ]->foo );
589
+ $ this ->assertEquals (2 , $ obj ->getInners ()[1 ]->foo );
579
590
}
580
591
581
592
/**
@@ -590,6 +601,19 @@ public function testRejectInvalidType()
590
601
$ serializer ->denormalize (array ('date ' => 'foo ' ), ObjectOuter::class);
591
602
}
592
603
604
+ /**
605
+ * @expectedException UnexpectedValueException
606
+ * @expectedExceptionMessage The type of the key "a" must be "int" ("string" given).
607
+ */
608
+ public function testRejectInvalidKey ()
609
+ {
610
+ $ extractor = new PropertyInfoExtractor (array (), array (new PhpDocExtractor (), new ReflectionExtractor ()));
611
+ $ normalizer = new ObjectNormalizer (null , null , null , $ extractor );
612
+ $ serializer = new Serializer (array (new ArrayDenormalizer (), new DateTimeNormalizer (), $ normalizer ));
613
+
614
+ $ serializer ->denormalize (array ('inners ' => array ('a ' => array ('foo ' => 1 ))), ObjectOuter::class);
615
+ }
616
+
593
617
public function testExtractAttributesRespectsFormat ()
594
618
{
595
619
$ normalizer = new FormatAndContextAwareNormalizer ();
@@ -784,6 +808,11 @@ class ObjectOuter
784
808
private $ inner ;
785
809
private $ date ;
786
810
811
+ /**
812
+ * @var ObjectInner[]
813
+ */
814
+ private $ inners ;
815
+
787
816
public function getInner ()
788
817
{
789
818
return $ this ->inner ;
@@ -803,6 +832,16 @@ public function getDate()
803
832
{
804
833
return $ this ->date ;
805
834
}
835
+
836
+ public function setInners (array $ inners )
837
+ {
838
+ $ this ->inners = $ inners ;
839
+ }
840
+
841
+ public function getInners ()
842
+ {
843
+ return $ this ->inners ;
844
+ }
806
845
}
807
846
808
847
class ObjectInner
0 commit comments