function ResourceResponseTestTrait::getEmptyCollectionResponse

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

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',
        // 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);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.