@@ -915,6 +915,116 @@ public function testDenormalizeUntypedStringObject()
915
915
$ this ->assertEquals (new DummyWithStringObject (new DummyString ()), $ actual );
916
916
$ this ->assertEquals ('' , $ actual ->value ->value );
917
917
}
918
+
919
+ public function testProvidingContextCacheKeyGeneratesSameChildContextCacheKey ()
920
+ {
921
+ $ foobar = new Dummy ();
922
+ $ foobar ->foo = new EmptyDummy ();
923
+ $ foobar ->bar = 'bar ' ;
924
+ $ foobar ->baz = 'baz ' ;
925
+
926
+ $ normalizer = new class () extends AbstractObjectNormalizerDummy {
927
+ public $ childContextCacheKey ;
928
+
929
+ protected function extractAttributes (object $ object , string $ format = null , array $ context = []): array
930
+ {
931
+ return array_keys ((array ) $ object );
932
+ }
933
+
934
+ protected function getAttributeValue (object $ object , string $ attribute , string $ format = null , array $ context = [])
935
+ {
936
+ return $ object ->{$ attribute };
937
+ }
938
+
939
+ protected function createChildContext (array $ parentContext , string $ attribute , ?string $ format ): array
940
+ {
941
+ $ childContext = parent ::createChildContext ($ parentContext , $ attribute , $ format );
942
+ $ this ->childContextCacheKey = $ childContext ['cache_key ' ];
943
+
944
+ return $ childContext ;
945
+ }
946
+ };
947
+
948
+ $ serializer = new Serializer ([$ normalizer ]);
949
+
950
+ $ serializer ->normalize ($ foobar , null , ['cache_key ' => 'hardcoded ' , 'iri ' => '/dummy/1 ' ]);
951
+ $ firstChildContextCacheKey = $ normalizer ->childContextCacheKey ;
952
+
953
+ $ serializer ->normalize ($ foobar , null , ['cache_key ' => 'hardcoded ' , 'iri ' => '/dummy/2 ' ]);
954
+ $ secondChildContextCacheKey = $ normalizer ->childContextCacheKey ;
955
+
956
+ $ this ->assertSame ($ firstChildContextCacheKey , $ secondChildContextCacheKey );
957
+ }
958
+
959
+ public function testChildContextKeepsOriginalContextCacheKey ()
960
+ {
961
+ $ foobar = new Dummy ();
962
+ $ foobar ->foo = new EmptyDummy ();
963
+ $ foobar ->bar = 'bar ' ;
964
+ $ foobar ->baz = 'baz ' ;
965
+
966
+ $ normalizer = new class () extends AbstractObjectNormalizerDummy {
967
+ public $ childContextCacheKey ;
968
+
969
+ protected function extractAttributes (object $ object , string $ format = null , array $ context = []): array
970
+ {
971
+ return array_keys ((array ) $ object );
972
+ }
973
+
974
+ protected function getAttributeValue (object $ object , string $ attribute , string $ format = null , array $ context = [])
975
+ {
976
+ return $ object ->{$ attribute };
977
+ }
978
+
979
+ protected function createChildContext (array $ parentContext , string $ attribute , ?string $ format ): array
980
+ {
981
+ $ childContext = parent ::createChildContext ($ parentContext , $ attribute , $ format );
982
+ $ this ->childContextCacheKey = $ childContext ['cache_key ' ];
983
+
984
+ return $ childContext ;
985
+ }
986
+ };
987
+
988
+ $ serializer = new Serializer ([$ normalizer ]);
989
+ $ serializer ->normalize ($ foobar , null , ['cache_key ' => 'hardcoded ' , 'iri ' => '/dummy/1 ' ]);
990
+
991
+ $ this ->assertSame ('hardcoded-foo ' , $ normalizer ->childContextCacheKey );
992
+ }
993
+
994
+ public function testChildContextCacheKeyStaysFalseWhenOriginalCacheKeyIsFalse ()
995
+ {
996
+ $ foobar = new Dummy ();
997
+ $ foobar ->foo = new EmptyDummy ();
998
+ $ foobar ->bar = 'bar ' ;
999
+ $ foobar ->baz = 'baz ' ;
1000
+
1001
+ $ normalizer = new class () extends AbstractObjectNormalizerDummy {
1002
+ public $ childContextCacheKey ;
1003
+
1004
+ protected function extractAttributes (object $ object , string $ format = null , array $ context = []): array
1005
+ {
1006
+ return array_keys ((array ) $ object );
1007
+ }
1008
+
1009
+ protected function getAttributeValue (object $ object , string $ attribute , string $ format = null , array $ context = [])
1010
+ {
1011
+ return $ object ->{$ attribute };
1012
+ }
1013
+
1014
+ protected function createChildContext (array $ parentContext , string $ attribute , ?string $ format ): array
1015
+ {
1016
+ $ childContext = parent ::createChildContext ($ parentContext , $ attribute , $ format );
1017
+ $ this ->childContextCacheKey = $ childContext ['cache_key ' ];
1018
+
1019
+ return $ childContext ;
1020
+ }
1021
+ };
1022
+
1023
+ $ serializer = new Serializer ([$ normalizer ]);
1024
+ $ serializer ->normalize ($ foobar , null , ['cache_key ' => false ]);
1025
+
1026
+ $ this ->assertFalse ($ normalizer ->childContextCacheKey );
1027
+ }
918
1028
}
919
1029
920
1030
class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer
0 commit comments