You are here

public function AttachmentNormalizerTest::testNormalizer in Replication 8

File

tests/src/Kernel/Normalizer/AttachmentNormalizerTest.php, line 52

Class

AttachmentNormalizerTest
Tests the attachment serialization format.

Namespace

Drupal\Tests\replication\Kernel\Normalizer

Code

public function testNormalizer() {

  // Test normalize.
  $normalized = $this->serializer
    ->normalize($this->fileEntity);
  $this
    ->assertTrue(!is_resource($normalized), 'File entity was normalized to a file entity.');

  // Test normalize.
  $normalized = $this->serializer
    ->normalize($this->fileEntity, 'json');
  $this
    ->assertTrue(!is_resource($normalized), 'File entity was normalized to a file entity.');

  // Test normalize.
  $normalized = $this->serializer
    ->normalize($this->fileEntity, 'stream');
  $this
    ->assertTrue(is_resource($normalized), 'File entity was normalized to a file resource.');

  // Test normalize.
  $normalized = $this->serializer
    ->normalize($this->fileEntity, 'base64_stream');
  $this
    ->assertTrue(is_resource($normalized), 'File entity was normalized to a file resource.');

  // Test serialize.
  $serialized = $this->serializer
    ->serialize($this->fileEntity, 'stream');
  $this
    ->assertEquals($serialized, $this->fileContents, 'File entity was serialized to file contents.');

  // Test denormalize.
  $denormalized = $this->serializer
    ->denormalize($this->fileHandle, $this->entityClass, 'stream');
  $this
    ->assertTrue($denormalized instanceof $this->entityClass, new FormattableMarkup('Denormalized entity is an instance of @class', [
    '@class' => $this->entityClass,
  ]));
  $this
    ->assertSame($this->fileEntity
    ->getEntityTypeId(), $denormalized
    ->getEntityTypeId(), 'Expected entity type found.');
}