@@ -548,6 +548,131 @@ public function testDenormalizeUntypedStringObject()
548
548
$ this ->assertEquals (new DummyWithStringObject (new DummyString ()), $ actual );
549
549
$ this ->assertEquals ('' , $ actual ->value ->value );
550
550
}
551
+
552
+ public function testProvidingContextCacheKeyGeneratesSameChildContextCacheKey ()
553
+ {
554
+ $ foobar = new Dummy ();
555
+ $ foobar ->foo = new EmptyDummy ();
556
+ $ foobar ->bar = 'bar ' ;
557
+ $ foobar ->baz = 'baz ' ;
558
+ $ data = [
559
+ 'foo ' => [],
560
+ 'bar ' => 'bar ' ,
561
+ 'baz ' => 'baz ' ,
562
+ ];
563
+
564
+ $ normalizer = new class () extends AbstractObjectNormalizerDummy {
565
+ public $ childContextCacheKey ;
566
+
567
+ protected function extractAttributes (object $ object , string $ format = null , array $ context = []): array
568
+ {
569
+ return array_keys ((array ) $ object );
570
+ }
571
+
572
+ protected function getAttributeValue (object $ object , string $ attribute , string $ format = null , array $ context = [])
573
+ {
574
+ return $ object ->{$ attribute };
575
+ }
576
+
577
+ protected function createChildContext (array $ parentContext , string $ attribute , ?string $ format ): array
578
+ {
579
+ $ childContext = parent ::createChildContext ($ parentContext , $ attribute , $ format );
580
+ $ this ->childContextCacheKey = $ childContext ['cache_key ' ];
581
+
582
+ return $ childContext ;
583
+ }
584
+ };
585
+
586
+ $ serializer = new Serializer ([$ normalizer ]);
587
+
588
+ $ serializer ->normalize ($ foobar , null , ['cache_key ' => 'hardcoded ' , 'iri ' => '/dummy/1 ' ]);
589
+ $ firstChildContextCacheKey = $ normalizer ->childContextCacheKey ;
590
+
591
+ $ serializer ->normalize ($ foobar , null , ['cache_key ' => 'hardcoded ' , 'iri ' => '/dummy/2 ' ]);
592
+ $ secondChildContextCacheKey = $ normalizer ->childContextCacheKey ;
593
+
594
+ $ this ->assertSame ($ firstChildContextCacheKey , $ secondChildContextCacheKey );
595
+ }
596
+
597
+ public function testChildContextKeepsOriginalContextCacheKey ()
598
+ {
599
+ $ foobar = new Dummy ();
600
+ $ foobar ->foo = new EmptyDummy ();
601
+ $ foobar ->bar = 'bar ' ;
602
+ $ foobar ->baz = 'baz ' ;
603
+ $ data = [
604
+ 'foo ' => [],
605
+ 'bar ' => 'bar ' ,
606
+ 'baz ' => 'baz ' ,
607
+ ];
608
+
609
+ $ normalizer = new class () extends AbstractObjectNormalizerDummy {
610
+ public $ childContextCacheKey ;
611
+
612
+ protected function extractAttributes (object $ object , string $ format = null , array $ context = []): array
613
+ {
614
+ return array_keys ((array ) $ object );
615
+ }
616
+
617
+ protected function getAttributeValue (object $ object , string $ attribute , string $ format = null , array $ context = [])
618
+ {
619
+ return $ object ->{$ attribute };
620
+ }
621
+
622
+ protected function createChildContext (array $ parentContext , string $ attribute , ?string $ format ): array
623
+ {
624
+ $ childContext = parent ::createChildContext ($ parentContext , $ attribute , $ format );
625
+ $ this ->childContextCacheKey = $ childContext ['cache_key ' ];
626
+
627
+ return $ childContext ;
628
+ }
629
+ };
630
+
631
+ $ serializer = new Serializer ([$ normalizer ]);
632
+ $ serializer ->normalize ($ foobar , null , ['cache_key ' => 'hardcoded ' , 'iri ' => '/dummy/1 ' ]);
633
+
634
+ $ this ->assertSame ('hardcoded-foo ' , $ normalizer ->childContextCacheKey );
635
+ }
636
+
637
+ public function testChildContextCacheKeyStaysFalseWhenOriginalCacheKeyIsFalse ()
638
+ {
639
+ $ foobar = new Dummy ();
640
+ $ foobar ->foo = new EmptyDummy ();
641
+ $ foobar ->bar = 'bar ' ;
642
+ $ foobar ->baz = 'baz ' ;
643
+ $ data = [
644
+ 'foo ' => [],
645
+ 'bar ' => 'bar ' ,
646
+ 'baz ' => 'baz ' ,
647
+ ];
648
+
649
+ $ normalizer = new class () extends AbstractObjectNormalizerDummy {
650
+ public $ childContextCacheKey ;
651
+
652
+ protected function extractAttributes (object $ object , string $ format = null , array $ context = []): array
653
+ {
654
+ return array_keys ((array ) $ object );
655
+ }
656
+
657
+ protected function getAttributeValue (object $ object , string $ attribute , string $ format = null , array $ context = [])
658
+ {
659
+ return $ object ->{$ attribute };
660
+ }
661
+
662
+ protected function createChildContext (array $ parentContext , string $ attribute , ?string $ format ): array
663
+ {
664
+ $ childContext = parent ::createChildContext ($ parentContext , $ attribute , $ format );
665
+ $ this ->childContextCacheKey = $ childContext ['cache_key ' ];
666
+
667
+ return $ childContext ;
668
+ }
669
+ };
670
+
671
+ $ serializer = new Serializer ([$ normalizer ]);
672
+ $ serializer ->normalize ($ foobar , null , ['cache_key ' => false ]);
673
+
674
+ $ this ->assertFalse ($ normalizer ->childContextCacheKey );
675
+ }
551
676
}
552
677
553
678
class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer
0 commit comments