You are here

public function SerializerTest::testFallbackNormalizer in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/jsonapi/tests/src/Kernel/Serializer/SerializerTest.php \Drupal\Tests\jsonapi\Kernel\Serializer\SerializerTest::testFallbackNormalizer()

@covers \Drupal\jsonapi\Serializer\Serializer::normalize

File

core/modules/jsonapi/tests/src/Kernel/Serializer/SerializerTest.php, line 98

Class

SerializerTest
Tests the JSON:API serializer.

Namespace

Drupal\Tests\jsonapi\Kernel\Serializer

Code

public function testFallbackNormalizer() {
  $context = [
    'account' => $this->user,
    'resource_object' => ResourceObject::createFromEntity($this->resourceType, $this->node),
  ];
  $value = $this->sut
    ->normalize($this->node->field_text, 'api_json', $context);
  $this
    ->assertInstanceOf(CacheableNormalization::class, $value);
  $nested_field = [
    $this->node->field_text,
  ];

  // When an object implements \IteratorAggregate and has corresponding
  // fallback normalizer, it should be normalized by fallback normalizer.
  $traversableObject = new TraversableObject();
  $value = $this->sut
    ->normalize($traversableObject, 'api_json', $context);
  $this
    ->assertEquals($traversableObject->property, $value);

  // When wrapped in an array, we should still be using the JSON:API
  // serializer.
  $value = $this->sut
    ->normalize($nested_field, 'api_json', $context);
  $this
    ->assertInstanceOf(CacheableNormalization::class, $value[0]);

  // Continue to use the fallback normalizer when we need it.
  $data = Markup::create('<h2>Test Markup</h2>');
  $value = $this->sut
    ->normalize($data, 'api_json', $context);
  $this
    ->assertEquals('<h2>Test Markup</h2>', $value);
}