You are here

public function JsonApiDocumentTopLevelNormalizerTest::testCacheableMetadata in JSON:API 8

Same name and namespace in other branches
  1. 8.2 tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php \Drupal\Tests\jsonapi\Kernel\Normalizer\JsonApiDocumentTopLevelNormalizerTest::testCacheableMetadata()

Ensure that cacheability metadata is properly added.

@dataProvider testCacheableMetadataProvider

Parameters

\Drupal\Core\Cache\CacheableMetadata $expected_metadata: The expected cacheable metadata.

array|null $fields: Fields to include in the response, keyed by resource type.

array|null $includes: Resources paths to include in the response.

File

tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php, line 687

Class

JsonApiDocumentTopLevelNormalizerTest
@coversDefaultClass \Drupal\jsonapi\Normalizer\JsonApiDocumentTopLevelNormalizer @group jsonapi @group legacy

Namespace

Drupal\Tests\jsonapi\Kernel\Normalizer

Code

public function testCacheableMetadata(CacheableMetadata $expected_metadata, $fields = NULL, $includes = NULL) {
  list($request, $resource_type) = $this
    ->generateProphecies('node', 'article');
  $context = [
    'request' => $this
      ->decorateRequest($request, $fields, $includes),
    'resource_type' => $resource_type,
  ];
  $jsonapi_doc_object = $this
    ->getNormalizer()
    ->normalize(new JsonApiDocumentTopLevel($this->node), 'api_json', $context);
  $this
    ->assertArraySubset($expected_metadata
    ->getCacheTags(), $jsonapi_doc_object
    ->getCacheTags());
  $this
    ->assertArraySubset($expected_metadata
    ->getCacheContexts(), $jsonapi_doc_object
    ->getCacheContexts());
  $this
    ->assertSame($expected_metadata
    ->getCacheMaxAge(), $jsonapi_doc_object
    ->getCacheMaxAge());
}