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 ;
@@ -525,13 +528,21 @@ public function testThrowUnexpectedValueException()
525
528
526
529
public function testDenomalizeRecursive ()
527
530
{
528
- $ normalizer = new ObjectNormalizer (null , null , null , new ReflectionExtractor ());
529
- $ serializer = new Serializer (array (new DateTimeNormalizer (), $ normalizer ));
531
+ $ extractor = new PropertyInfoExtractor (array (), array (new PhpDocExtractor (), new ReflectionExtractor ()));
532
+ $ normalizer = new ObjectNormalizer (null , null , null , $ extractor );
533
+ $ serializer = new Serializer (array (new ArrayDenormalizer (), new DateTimeNormalizer (), $ normalizer ));
534
+
535
+ $ obj = $ serializer ->denormalize (array (
536
+ 'inner ' => array ('foo ' => 'foo ' , 'bar ' => 'bar ' ),
537
+ 'date ' => '1988/01/21 ' ,
538
+ 'inners ' => array (array ('foo ' => 1 ), array ('foo ' => 2 )),
539
+ ), ObjectOuter::class);
530
540
531
- $ obj = $ serializer ->denormalize (array ('inner ' => array ('foo ' => 'foo ' , 'bar ' => 'bar ' ), 'date ' => '1988/01/21 ' ), ObjectOuter::class);
532
541
$ this ->assertEquals ('foo ' , $ obj ->getInner ()->foo );
533
542
$ this ->assertEquals ('bar ' , $ obj ->getInner ()->bar );
534
543
$ this ->assertEquals ('1988-01-21 ' , $ obj ->getDate ()->format ('Y-m-d ' ));
544
+ $ this ->assertEquals (1 , $ obj ->getInners ()[0 ]->foo );
545
+ $ this ->assertEquals (2 , $ obj ->getInners ()[1 ]->foo );
535
546
}
536
547
537
548
/**
@@ -546,6 +557,19 @@ public function testRejectInvalidType()
546
557
$ serializer ->denormalize (array ('date ' => 'foo ' ), ObjectOuter::class);
547
558
}
548
559
560
+ /**
561
+ * @expectedException UnexpectedValueException
562
+ * @expectedExceptionMessage The type of the key "a" must be "int" ("string" given).
563
+ */
564
+ public function testRejectInvalidKey ()
565
+ {
566
+ $ extractor = new PropertyInfoExtractor (array (), array (new PhpDocExtractor (), new ReflectionExtractor ()));
567
+ $ normalizer = new ObjectNormalizer (null , null , null , $ extractor );
568
+ $ serializer = new Serializer (array (new ArrayDenormalizer (), new DateTimeNormalizer (), $ normalizer ));
569
+
570
+ $ serializer ->denormalize (array ('inners ' => array ('a ' => array ('foo ' => 1 ))), ObjectOuter::class);
571
+ }
572
+
549
573
public function testExtractAttributesRespectsFormat ()
550
574
{
551
575
$ normalizer = new FormatAndContextAwareNormalizer ();
@@ -740,6 +764,11 @@ class ObjectOuter
740
764
private $ inner ;
741
765
private $ date ;
742
766
767
+ /**
768
+ * @var ObjectInner[]
769
+ */
770
+ private $ inners ;
771
+
743
772
public function getInner ()
744
773
{
745
774
return $ this ->inner ;
@@ -759,6 +788,16 @@ public function getDate()
759
788
{
760
789
return $ this ->date ;
761
790
}
791
+
792
+ public function setInners (array $ inners )
793
+ {
794
+ $ this ->inners = $ inners ;
795
+ }
796
+
797
+ public function getInners ()
798
+ {
799
+ return $ this ->inners ;
800
+ }
762
801
}
763
802
764
803
class ObjectInner
0 commit comments