WorkspaceNormalizerTest.php in Replication 8.2
File
tests/src/Unit/Normalizer/WorkspaceNormalizerTest.php
View source
<?php
namespace Drupal\Tests\replication\Unit\Normalizer;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\multiversion\Entity\Workspace;
class WorkspaceNormalizerTest extends NormalizerTestBase {
protected $entityClass = 'Drupal\\multiversion\\Entity\\Workspace';
protected function setUp() {
parent::setUp();
$name = $this
->randomMachineName();
$this->entity = $this
->createWorkspace($name);
$this->entity
->save();
}
public function testNormalizer() {
$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.');
$expected = json_encode($normalized);
$actual = $this->serializer
->serialize($this->entity, 'json');
$this
->assertSame($expected, $actual, 'Entity serializes correctly to JSON.');
$denormalized = $this->serializer
->denormalize($normalized, $this->entityClass, 'json');
$this
->assertTrue($denormalized instanceof $this->entityClass, SafeMarkup::format('Denormalized entity is an instance of @class', [
'@class' => $this->entityClass,
]));
$this
->assertSame($denormalized
->getEntityTypeId(), $this->entity
->getEntityTypeId(), 'Expected entity type found.');
}
protected function createWorkspace($name) {
return Workspace::create([
'machine_name' => $name,
'type' => 'basic',
]);
}
}