You are here

protected function ResourceTestBase::normalize in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::normalize()
  2. 9 core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::normalize()

Generates a JSON:API normalization for the given entity.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity to generate a JSON:API normalization for.

\Drupal\Core\Url $url: The URL to use as the "self" link.

Return value

array The JSON:API normalization for the given entity.

File

core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php, line 379

Class

ResourceTestBase
Subclass this for every JSON:API resource type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected function normalize(EntityInterface $entity, Url $url) {

  // Don't use cached normalizations in tests.
  $this->container
    ->get('cache.jsonapi_normalizations')
    ->deleteAll();
  $self_link = new Link(new CacheableMetadata(), $url, 'self');
  $resource_type = $this->container
    ->get('jsonapi.resource_type.repository')
    ->getByTypeName(static::$resourceTypeName);
  $doc = new JsonApiDocumentTopLevel(new ResourceObjectData([
    ResourceObject::createFromEntity($resource_type, $entity),
  ], 1), new NullIncludedData(), new LinkCollection([
    'self' => $self_link,
  ]));
  return $this->serializer
    ->normalize($doc, 'api_json', [
    'resource_type' => $resource_type,
    'account' => $this->account,
  ])
    ->getNormalization();
}