You are here

protected function ResourceResponseTestTrait::getEmptyCollectionResponse in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/jsonapi/tests/src/Functional/ResourceResponseTestTrait.php \Drupal\Tests\jsonapi\Functional\ResourceResponseTestTrait::getEmptyCollectionResponse()
  2. 9 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.

File

core/modules/jsonapi/tests/src/Functional/ResourceResponseTestTrait.php, line 542

Class

ResourceResponseTestTrait
Utility methods for handling resource responses.

Namespace

Drupal\Tests\jsonapi\Functional

Code

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);
}