You are here

public function WorkspaceNormalizerTest::testNormalizer in Replication 8

File

tests/src/Kernel/Normalizer/WorkspaceNormalizerTest.php, line 24

Class

WorkspaceNormalizerTest
Tests the workspace serialization format.

Namespace

Drupal\Tests\replication\Kernel\Normalizer

Code

public function testNormalizer() {

  // Test normalize.
  $expected = [
    'db_name' => (string) $this->entity
      ->getMachineName(),
    'instance_start_time' => (string) $this->entity
      ->getStartTime(),
    'update_seq' => 0,
  ];
  $normalized = $this->serializer
    ->normalize($this->entity);
  foreach (array_keys($expected) as $fieldName) {
    $this
      ->assertEquals($expected[$fieldName], $normalized[$fieldName], "Field {$fieldName} is normalized correctly.");
  }
  $this
    ->assertTrue(is_string($normalized['instance_start_time']), 'Instance start time is a string.');
  $this
    ->assertEquals(array_diff_key($normalized, $expected), [], 'No unexpected data is added to the normalized array.');

  // Test serialize.
  $expected = json_encode($normalized);

  // Paranoid test because JSON serialization is tested elsewhere.
  $actual = $this->serializer
    ->serialize($this->entity, 'json');
  $this
    ->assertSame($expected, $actual, 'Entity serializes correctly to JSON.');

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