14
14
namespace ApiPlatform \Core \Tests \Api ;
15
15
16
16
use ApiPlatform \Core \Api \IdentifiersExtractor ;
17
+ use ApiPlatform \Core \Api \ResourceClassResolverInterface ;
17
18
use ApiPlatform \Core \Metadata \Property \Factory \PropertyMetadataFactoryInterface ;
18
19
use ApiPlatform \Core \Metadata \Property \Factory \PropertyNameCollectionFactoryInterface ;
19
20
use ApiPlatform \Core \Metadata \Property \PropertyMetadata ;
22
23
use ApiPlatform \Core \Tests \Fixtures \TestBundle \Entity \Dummy ;
23
24
use ApiPlatform \Core \Tests \Fixtures \TestBundle \Entity \RelatedDummy ;
24
25
use PHPUnit \Framework \TestCase ;
26
+ use Prophecy \Argument ;
25
27
26
28
/**
27
29
* @author Antoine Bluchet <[email protected] >
@@ -53,7 +55,7 @@ public function testGetIdentifiersFromResourceClass()
53
55
{
54
56
list ($ propertyNameCollectionFactoryProphecy , $ propertyMetadataFactoryProphecy ) = $ this ->getMetadataFactoryProphecies (Dummy::class, ['id ' ]);
55
57
56
- $ identifiersExtractor = new IdentifiersExtractor ($ propertyNameCollectionFactoryProphecy ->reveal (), $ propertyMetadataFactoryProphecy ->reveal ());
58
+ $ identifiersExtractor = new IdentifiersExtractor ($ propertyNameCollectionFactoryProphecy ->reveal (), $ propertyMetadataFactoryProphecy ->reveal (), null , $ this -> getResourceClassResolver () );
57
59
58
60
$ this ->assertEquals (['id ' ], $ identifiersExtractor ->getIdentifiersFromResourceClass (Dummy::class));
59
61
}
@@ -62,7 +64,7 @@ public function testGetCompositeIdentifiersFromResourceClass()
62
64
{
63
65
list ($ propertyNameCollectionFactoryProphecy , $ propertyMetadataFactoryProphecy ) = $ this ->getMetadataFactoryProphecies (Dummy::class, ['id ' , 'name ' ]);
64
66
65
- $ identifiersExtractor = new IdentifiersExtractor ($ propertyNameCollectionFactoryProphecy ->reveal (), $ propertyMetadataFactoryProphecy ->reveal ());
67
+ $ identifiersExtractor = new IdentifiersExtractor ($ propertyNameCollectionFactoryProphecy ->reveal (), $ propertyMetadataFactoryProphecy ->reveal (), null , $ this -> getResourceClassResolver () );
66
68
67
69
$ this ->assertEquals (['id ' , 'name ' ], $ identifiersExtractor ->getIdentifiersFromResourceClass (Dummy::class));
68
70
}
@@ -71,31 +73,32 @@ public function testGetIdentifiersFromItem()
71
73
{
72
74
list ($ propertyNameCollectionFactoryProphecy , $ propertyMetadataFactoryProphecy ) = $ this ->getMetadataFactoryProphecies (Dummy::class, ['id ' ]);
73
75
74
- $ identifiersExtractor = new IdentifiersExtractor ($ propertyNameCollectionFactoryProphecy ->reveal (), $ propertyMetadataFactoryProphecy ->reveal ());
76
+ $ identifiersExtractor = new IdentifiersExtractor ($ propertyNameCollectionFactoryProphecy ->reveal (), $ propertyMetadataFactoryProphecy ->reveal (), null , $ this -> getResourceClassResolver () );
75
77
76
78
$ dummy = new Dummy ();
77
79
$ dummy ->setId (1 );
78
80
79
81
$ this ->assertEquals (['id ' => 1 ], $ identifiersExtractor ->getIdentifiersFromItem ($ dummy ));
80
82
}
81
83
82
- public function testGetStringableIdentifiersFromItem ()
84
+ public function testUseObjectIdentifier ()
83
85
{
84
86
list ($ propertyNameCollectionFactoryProphecy , $ propertyMetadataFactoryProphecy ) = $ this ->getMetadataFactoryProphecies (Dummy::class, ['id ' ]);
85
87
86
- $ identifiersExtractor = new IdentifiersExtractor ($ propertyNameCollectionFactoryProphecy ->reveal (), $ propertyMetadataFactoryProphecy ->reveal ());
88
+ $ identifiersExtractor = new IdentifiersExtractor ($ propertyNameCollectionFactoryProphecy ->reveal (), $ propertyMetadataFactoryProphecy ->reveal (), null , $ this -> getResourceClassResolver () );
87
89
90
+ $ uuid = new Uuid ();
88
91
$ dummy = new Dummy ();
89
- $ dummy ->setId (new Uuid () );
92
+ $ dummy ->setId ($ uuid );
90
93
91
- $ this ->assertEquals (['id ' => ' foo ' ], $ identifiersExtractor ->getIdentifiersFromItem ($ dummy ));
94
+ $ this ->assertEquals (['id ' => $ uuid ], $ identifiersExtractor ->getIdentifiersFromItem ($ dummy ));
92
95
}
93
96
94
97
public function testGetCompositeIdentifiersFromItem ()
95
98
{
96
99
list ($ propertyNameCollectionFactoryProphecy , $ propertyMetadataFactoryProphecy ) = $ this ->getMetadataFactoryProphecies (Dummy::class, ['id ' , 'name ' ]);
97
100
98
- $ identifiersExtractor = new IdentifiersExtractor ($ propertyNameCollectionFactoryProphecy ->reveal (), $ propertyMetadataFactoryProphecy ->reveal ());
101
+ $ identifiersExtractor = new IdentifiersExtractor ($ propertyNameCollectionFactoryProphecy ->reveal (), $ propertyMetadataFactoryProphecy ->reveal (), null , $ this -> getResourceClassResolver () );
99
102
100
103
$ dummy = new Dummy ();
101
104
$ dummy ->setId (1 );
@@ -109,7 +112,7 @@ public function testGetRelatedIdentifiersFromItem()
109
112
$ prophecies = $ this ->getMetadataFactoryProphecies (Dummy::class, ['id ' , 'relatedDummy ' ]);
110
113
list ($ propertyNameCollectionFactoryProphecy , $ propertyMetadataFactoryProphecy ) = $ this ->getMetadataFactoryProphecies (RelatedDummy::class, ['id ' ], $ prophecies );
111
114
112
- $ identifiersExtractor = new IdentifiersExtractor ($ propertyNameCollectionFactoryProphecy ->reveal (), $ propertyMetadataFactoryProphecy ->reveal ());
115
+ $ identifiersExtractor = new IdentifiersExtractor ($ propertyNameCollectionFactoryProphecy ->reveal (), $ propertyMetadataFactoryProphecy ->reveal (), null , $ this -> getResourceClassResolver () );
113
116
114
117
$ related = new RelatedDummy ();
115
118
$ related ->setId (2 );
@@ -130,7 +133,7 @@ public function testThrowNoIdentifierFromItem()
130
133
$ prophecies = $ this ->getMetadataFactoryProphecies (Dummy::class, ['id ' , 'relatedDummy ' ]);
131
134
list ($ propertyNameCollectionFactoryProphecy , $ propertyMetadataFactoryProphecy ) = $ this ->getMetadataFactoryProphecies (RelatedDummy::class, [], $ prophecies );
132
135
133
- $ identifiersExtractor = new IdentifiersExtractor ($ propertyNameCollectionFactoryProphecy ->reveal (), $ propertyMetadataFactoryProphecy ->reveal ());
136
+ $ identifiersExtractor = new IdentifiersExtractor ($ propertyNameCollectionFactoryProphecy ->reveal (), $ propertyMetadataFactoryProphecy ->reveal (), null , $ this -> getResourceClassResolver () );
134
137
135
138
$ related = new RelatedDummy ();
136
139
$ related ->setId (2 );
@@ -141,4 +144,34 @@ public function testThrowNoIdentifierFromItem()
141
144
142
145
$ identifiersExtractor ->getIdentifiersFromItem ($ dummy );
143
146
}
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
+ }
144
177
}
0 commit comments