Skip to content

Commit 607142c

Browse files
committed
fix(test): use an operation within schema assertions
1 parent 97fcb38 commit 607142c

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

src/Symfony/Bundle/Test/ApiTestAssertionsTrait.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use ApiPlatform\JsonSchema\SchemaFactoryInterface;
2020
use ApiPlatform\Metadata\Get;
2121
use ApiPlatform\Metadata\GetCollection;
22+
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
2223
use ApiPlatform\Symfony\Bundle\Test\Constraint\ArraySubset;
2324
use ApiPlatform\Symfony\Bundle\Test\Constraint\MatchesJsonSchema;
2425
use PHPUnit\Framework\ExpectationFailedException;
@@ -120,7 +121,12 @@ public static function assertMatchesResourceCollectionJsonSchema(string $resourc
120121
if ($schemaFactory instanceof LegacySchemaFactoryInterface) {
121122
$schema = $schemaFactory->buildSchema($resourceClass, $format, Schema::TYPE_OUTPUT, OperationType::COLLECTION, $operationName, null);
122123
} 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+
124130
$schema = $schemaFactory->buildSchema($resourceClass, $format, Schema::TYPE_OUTPUT, $operation, null);
125131
}
126132

@@ -134,7 +140,12 @@ public static function assertMatchesResourceItemJsonSchema(string $resourceClass
134140
if ($schemaFactory instanceof LegacySchemaFactoryInterface) {
135141
$schema = $schemaFactory->buildSchema($resourceClass, $format, Schema::TYPE_OUTPUT, OperationType::ITEM, $operationName, null);
136142
} 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+
138149
$schema = $schemaFactory->buildSchema($resourceClass, $format, Schema::TYPE_OUTPUT, $operation, null);
139150
}
140151

@@ -181,6 +192,22 @@ private static function getSchemaFactory()
181192

182193
return $schemaFactory;
183194
}
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+
}
184211
}
185212

186213
class_alias(ApiTestAssertionsTrait::class, \ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestAssertionsTrait::class);

0 commit comments

Comments
 (0)