19
19
use ApiPlatform \JsonSchema \SchemaFactoryInterface ;
20
20
use ApiPlatform \Metadata \Get ;
21
21
use ApiPlatform \Metadata \GetCollection ;
22
+ use ApiPlatform \Metadata \Resource \Factory \ResourceMetadataCollectionFactoryInterface ;
22
23
use ApiPlatform \Symfony \Bundle \Test \Constraint \ArraySubset ;
23
24
use ApiPlatform \Symfony \Bundle \Test \Constraint \MatchesJsonSchema ;
24
25
use PHPUnit \Framework \ExpectationFailedException ;
@@ -120,7 +121,12 @@ public static function assertMatchesResourceCollectionJsonSchema(string $resourc
120
121
if ($ schemaFactory instanceof LegacySchemaFactoryInterface) {
121
122
$ schema = $ schemaFactory ->buildSchema ($ resourceClass , $ format , Schema::TYPE_OUTPUT , OperationType::COLLECTION , $ operationName , null );
122
123
} else {
123
- $ operation = $ operationName ? (new GetCollection ())->withName ($ operationName ) : new GetCollection ();
124
+ if ($ resourceMetadataFactoryCollection = self ::getResourceMetadataCollectionFactory ()) {
125
+ $ operation = $ resourceMetadataFactoryCollection ->create ($ resourceClass )->getOperation ($ operationName , true );
126
+ } else {
127
+ $ operation = $ operationName ? (new GetCollection ())->withName ($ operationName ) : new GetCollection ();
128
+ }
129
+
124
130
$ schema = $ schemaFactory ->buildSchema ($ resourceClass , $ format , Schema::TYPE_OUTPUT , $ operation , null );
125
131
}
126
132
@@ -134,7 +140,12 @@ public static function assertMatchesResourceItemJsonSchema(string $resourceClass
134
140
if ($ schemaFactory instanceof LegacySchemaFactoryInterface) {
135
141
$ schema = $ schemaFactory ->buildSchema ($ resourceClass , $ format , Schema::TYPE_OUTPUT , OperationType::ITEM , $ operationName , null );
136
142
} else {
137
- $ operation = $ operationName ? (new Get ())->withName ($ operationName ) : new Get ();
143
+ if ($ resourceMetadataFactoryCollection = self ::getResourceMetadataCollectionFactory ()) {
144
+ $ operation = $ resourceMetadataFactoryCollection ->create ($ resourceClass )->getOperation ($ operationName );
145
+ } else {
146
+ $ operation = $ operationName ? (new Get ())->withName ($ operationName ) : new Get ();
147
+ }
148
+
138
149
$ schema = $ schemaFactory ->buildSchema ($ resourceClass , $ format , Schema::TYPE_OUTPUT , $ operation , null );
139
150
}
140
151
@@ -181,6 +192,22 @@ private static function getSchemaFactory()
181
192
182
193
return $ schemaFactory ;
183
194
}
195
+
196
+ /**
197
+ * @return ResourceMetadataCollectionFactoryInterface|null
198
+ */
199
+ private static function getResourceMetadataCollectionFactory ()
200
+ {
201
+ $ container = method_exists (static ::class, 'getContainer ' ) ? static ::getContainer () : static ::$ container ; // @phpstan-ignore-line
202
+
203
+ try {
204
+ $ resourceMetadataFactoryCollection = $ container ->get ('api_platform.metadata.resource.metadata_collection_factory ' );
205
+ } catch (ServiceNotFoundException $ e ) {
206
+ return null ;
207
+ }
208
+
209
+ return $ resourceMetadataFactoryCollection ;
210
+ }
184
211
}
185
212
186
213
class_alias (ApiTestAssertionsTrait::class, \ApiPlatform \Core \Bridge \Symfony \Bundle \Test \ApiTestAssertionsTrait::class);
0 commit comments