1414namespace ApiPlatform \Core \Tests \Api ;
1515
1616use ApiPlatform \Core \Api \IdentifiersExtractor ;
17+ use ApiPlatform \Core \Api \ResourceClassResolverInterface ;
1718use ApiPlatform \Core \Metadata \Property \Factory \PropertyMetadataFactoryInterface ;
1819use ApiPlatform \Core \Metadata \Property \Factory \PropertyNameCollectionFactoryInterface ;
1920use ApiPlatform \Core \Metadata \Property \PropertyMetadata ;
2223use ApiPlatform \Core \Tests \Fixtures \TestBundle \Entity \Dummy ;
2324use ApiPlatform \Core \Tests \Fixtures \TestBundle \Entity \RelatedDummy ;
2425use PHPUnit \Framework \TestCase ;
26+ use Prophecy \Argument ;
2527
2628/**
2729 * @author Antoine Bluchet <soyuka@gmail.com>
@@ -53,7 +55,7 @@ public function testGetIdentifiersFromResourceClass()
5355 {
5456 list ($ propertyNameCollectionFactoryProphecy , $ propertyMetadataFactoryProphecy ) = $ this ->getMetadataFactoryProphecies (Dummy::class, ['id ' ]);
5557
56- $ identifiersExtractor = new IdentifiersExtractor ($ propertyNameCollectionFactoryProphecy ->reveal (), $ propertyMetadataFactoryProphecy ->reveal ());
58+ $ identifiersExtractor = new IdentifiersExtractor ($ propertyNameCollectionFactoryProphecy ->reveal (), $ propertyMetadataFactoryProphecy ->reveal (), null , $ this -> getResourceClassResolver () );
5759
5860 $ this ->assertEquals (['id ' ], $ identifiersExtractor ->getIdentifiersFromResourceClass (Dummy::class));
5961 }
@@ -62,7 +64,7 @@ public function testGetCompositeIdentifiersFromResourceClass()
6264 {
6365 list ($ propertyNameCollectionFactoryProphecy , $ propertyMetadataFactoryProphecy ) = $ this ->getMetadataFactoryProphecies (Dummy::class, ['id ' , 'name ' ]);
6466
65- $ identifiersExtractor = new IdentifiersExtractor ($ propertyNameCollectionFactoryProphecy ->reveal (), $ propertyMetadataFactoryProphecy ->reveal ());
67+ $ identifiersExtractor = new IdentifiersExtractor ($ propertyNameCollectionFactoryProphecy ->reveal (), $ propertyMetadataFactoryProphecy ->reveal (), null , $ this -> getResourceClassResolver () );
6668
6769 $ this ->assertEquals (['id ' , 'name ' ], $ identifiersExtractor ->getIdentifiersFromResourceClass (Dummy::class));
6870 }
@@ -71,31 +73,32 @@ public function testGetIdentifiersFromItem()
7173 {
7274 list ($ propertyNameCollectionFactoryProphecy , $ propertyMetadataFactoryProphecy ) = $ this ->getMetadataFactoryProphecies (Dummy::class, ['id ' ]);
7375
74- $ identifiersExtractor = new IdentifiersExtractor ($ propertyNameCollectionFactoryProphecy ->reveal (), $ propertyMetadataFactoryProphecy ->reveal ());
76+ $ identifiersExtractor = new IdentifiersExtractor ($ propertyNameCollectionFactoryProphecy ->reveal (), $ propertyMetadataFactoryProphecy ->reveal (), null , $ this -> getResourceClassResolver () );
7577
7678 $ dummy = new Dummy ();
7779 $ dummy ->setId (1 );
7880
7981 $ this ->assertEquals (['id ' => 1 ], $ identifiersExtractor ->getIdentifiersFromItem ($ dummy ));
8082 }
8183
82- public function testGetStringableIdentifiersFromItem ()
84+ public function testUseObjectIdentifier ()
8385 {
8486 list ($ propertyNameCollectionFactoryProphecy , $ propertyMetadataFactoryProphecy ) = $ this ->getMetadataFactoryProphecies (Dummy::class, ['id ' ]);
8587
86- $ identifiersExtractor = new IdentifiersExtractor ($ propertyNameCollectionFactoryProphecy ->reveal (), $ propertyMetadataFactoryProphecy ->reveal ());
88+ $ identifiersExtractor = new IdentifiersExtractor ($ propertyNameCollectionFactoryProphecy ->reveal (), $ propertyMetadataFactoryProphecy ->reveal (), null , $ this -> getResourceClassResolver () );
8789
90+ $ uuid = new Uuid ();
8891 $ dummy = new Dummy ();
89- $ dummy ->setId (new Uuid () );
92+ $ dummy ->setId ($ uuid );
9093
91- $ this ->assertEquals (['id ' => ' foo ' ], $ identifiersExtractor ->getIdentifiersFromItem ($ dummy ));
94+ $ this ->assertEquals (['id ' => $ uuid ], $ identifiersExtractor ->getIdentifiersFromItem ($ dummy ));
9295 }
9396
9497 public function testGetCompositeIdentifiersFromItem ()
9598 {
9699 list ($ propertyNameCollectionFactoryProphecy , $ propertyMetadataFactoryProphecy ) = $ this ->getMetadataFactoryProphecies (Dummy::class, ['id ' , 'name ' ]);
97100
98- $ identifiersExtractor = new IdentifiersExtractor ($ propertyNameCollectionFactoryProphecy ->reveal (), $ propertyMetadataFactoryProphecy ->reveal ());
101+ $ identifiersExtractor = new IdentifiersExtractor ($ propertyNameCollectionFactoryProphecy ->reveal (), $ propertyMetadataFactoryProphecy ->reveal (), null , $ this -> getResourceClassResolver () );
99102
100103 $ dummy = new Dummy ();
101104 $ dummy ->setId (1 );
@@ -109,7 +112,7 @@ public function testGetRelatedIdentifiersFromItem()
109112 $ prophecies = $ this ->getMetadataFactoryProphecies (Dummy::class, ['id ' , 'relatedDummy ' ]);
110113 list ($ propertyNameCollectionFactoryProphecy , $ propertyMetadataFactoryProphecy ) = $ this ->getMetadataFactoryProphecies (RelatedDummy::class, ['id ' ], $ prophecies );
111114
112- $ identifiersExtractor = new IdentifiersExtractor ($ propertyNameCollectionFactoryProphecy ->reveal (), $ propertyMetadataFactoryProphecy ->reveal ());
115+ $ identifiersExtractor = new IdentifiersExtractor ($ propertyNameCollectionFactoryProphecy ->reveal (), $ propertyMetadataFactoryProphecy ->reveal (), null , $ this -> getResourceClassResolver () );
113116
114117 $ related = new RelatedDummy ();
115118 $ related ->setId (2 );
@@ -130,7 +133,7 @@ public function testThrowNoIdentifierFromItem()
130133 $ prophecies = $ this ->getMetadataFactoryProphecies (Dummy::class, ['id ' , 'relatedDummy ' ]);
131134 list ($ propertyNameCollectionFactoryProphecy , $ propertyMetadataFactoryProphecy ) = $ this ->getMetadataFactoryProphecies (RelatedDummy::class, [], $ prophecies );
132135
133- $ identifiersExtractor = new IdentifiersExtractor ($ propertyNameCollectionFactoryProphecy ->reveal (), $ propertyMetadataFactoryProphecy ->reveal ());
136+ $ identifiersExtractor = new IdentifiersExtractor ($ propertyNameCollectionFactoryProphecy ->reveal (), $ propertyMetadataFactoryProphecy ->reveal (), null , $ this -> getResourceClassResolver () );
134137
135138 $ related = new RelatedDummy ();
136139 $ related ->setId (2 );
@@ -141,4 +144,34 @@ public function testThrowNoIdentifierFromItem()
141144
142145 $ identifiersExtractor ->getIdentifiersFromItem ($ dummy );
143146 }
147+
148+ private function getResourceClassResolver ()
149+ {
150+ $ resourceClassResolver = $ this ->prophesize (ResourceClassResolverInterface::class);
151+ $ resourceClassResolver ->isResourceClass (Argument::type ('string ' ))->will (function ($ args ) {
152+ if (Uuid::class === $ args [0 ]) {
153+ return false ;
154+ }
155+
156+ return true ;
157+ });
158+
159+ return $ resourceClassResolver ->reveal ();
160+ }
161+
162+ /**
163+ * @group legacy
164+ * @expectedDeprecation Not injecting ApiPlatform\Core\Api\ResourceClassResolverInterface in the CachedIdentifiersExtractor might introduce cache issues with object identifiers.
165+ */
166+ public function testLegacyGetIdentifiersFromItem ()
167+ {
168+ list ($ propertyNameCollectionFactoryProphecy , $ propertyMetadataFactoryProphecy ) = $ this ->getMetadataFactoryProphecies (Dummy::class, ['id ' ]);
169+
170+ $ identifiersExtractor = new IdentifiersExtractor ($ propertyNameCollectionFactoryProphecy ->reveal (), $ propertyMetadataFactoryProphecy ->reveal ());
171+
172+ $ dummy = new Dummy ();
173+ $ dummy ->setId (1 );
174+
175+ $ this ->assertEquals (['id ' => 1 ], $ identifiersExtractor ->getIdentifiersFromItem ($ dummy ));
176+ }
144177}
0 commit comments