You are here

public function SerializerTest::testFallbackNormalizer in JSON:API 8.2

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

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

File

tests/src/Kernel/Serializer/SerializerTest.php, line 89

Class

SerializerTest
Tests the JSON:API serializer.

Namespace

Drupal\Tests\jsonapi\Kernel\Serializer

Code

public function testFallbackNormalizer() {
  $context = [
    'account' => $this->user,
  ];
  $value = $this->sut
    ->normalize($this->node->field_text, 'api_json', $context);
  $this
    ->assertTrue($value instanceof CacheableNormalization);
  $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
    ->assertTrue($value[0] instanceof CacheableNormalization);

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