protected function ResourceResponseTestTrait::getEmptyCollectionResponse in Drupal 9
Same name and namespace in other branches
- 8 core/modules/jsonapi/tests/src/Functional/ResourceResponseTestTrait.php \Drupal\Tests\jsonapi\Functional\ResourceResponseTestTrait::getEmptyCollectionResponse()
Gets a generic empty collection response.
Parameters
int $cardinality: The cardinality of the resource collection. 1 for a to-one related resource collection; -1 for an unlimited cardinality.
string $self_link: The self link for collection ResourceResponse.
Return value
\Drupal\jsonapi\CacheableResourceResponse The empty collection ResourceResponse.
1 call to ResourceResponseTestTrait::getEmptyCollectionResponse()
- ResourceTestBase::getExpectedRelatedResponse in core/
modules/ jsonapi/ tests/ src/ Functional/ ResourceTestBase.php - Builds an expected related ResourceResponse for the given field.
File
- core/
modules/ jsonapi/ tests/ src/ Functional/ ResourceResponseTestTrait.php, line 542
Class
- ResourceResponseTestTrait
- Utility methods for handling resource responses.
Namespace
Drupal\Tests\jsonapi\FunctionalCode
protected function getEmptyCollectionResponse($cardinality, $self_link) {
// If the entity type is revisionable, add a resource version cache context.
$cache_contexts = Cache::mergeContexts([
// Cache contexts for JSON:API URL query parameters.
'url.query_args:fields',
'url.query_args:include',
// Drupal defaults.
'url.site',
], $this->entity
->getEntityType()
->isRevisionable() ? [
'url.query_args:resourceVersion',
] : []);
$cacheability = (new CacheableMetadata())
->addCacheContexts($cache_contexts)
->addCacheTags([
'http_response',
]);
return (new CacheableResourceResponse([
// Empty to-one relationships should be NULL and empty to-many
// relationships should be an empty array.
'data' => $cardinality === 1 ? NULL : [],
'jsonapi' => static::$jsonApiMember,
'links' => [
'self' => [
'href' => $self_link,
],
],
]))
->addCacheableDependency($cacheability);
}