public function BulkDocsNormalizerTest::testNormalizer in Replication 8
File
- tests/
src/ Kernel/ Normalizer/ BulkDocsNormalizerTest.php, line 57
Class
- BulkDocsNormalizerTest
- Tests the content serialization format.
Namespace
Drupal\Tests\replication\Kernel\NormalizerCode
public function testNormalizer() {
// Test normalize.
$expected = [];
for ($key = 0; $key < $this->testValuesNumber; $key++) {
$entity = EntityTestMulRev::load($key + 1);
$expected[$key] = [
'ok' => TRUE,
'id' => $entity
->uuid(),
'rev' => $entity->_rev->value,
];
}
$normalized = $this->serializer
->normalize($this->bulkDocs);
$entity_number = 1;
foreach ($expected as $key => $value) {
foreach (array_keys($value) as $value_key) {
$this
->assertEquals($value[$value_key], $normalized[$key][$value_key], "Field {$value_key} is normalized correctly for entity number {$entity_number}.");
}
$this
->assertEquals(array_diff_key($normalized[$key], $expected[$key]), [], 'No unexpected data is added to the normalized array.');
$entity_number++;
}
// Test serialize.
$expected = json_encode($normalized);
// Paranoid test because JSON serialization is tested elsewhere.
$actual = $this->serializer
->serialize($this->bulkDocs, 'json');
$this
->assertSame($actual, $expected, 'Entity serializes correctly to JSON.');
// Test denormalize.
$data = [
'docs' => [],
];
foreach ($this->testEntities as $entity) {
$data['docs'][] = $this->serializer
->normalize($entity);
}
$context = [
'workspace' => $this->workspaceManager
->getActiveWorkspace(),
];
$bulk_docs = $this->serializer
->denormalize($data, 'Drupal\\replication\\BulkDocs\\BulkDocs', 'json', $context);
$this
->assertTrue($bulk_docs instanceof BulkDocsInterface, 'Denormalized data is an instance of the correct interface.');
foreach ($bulk_docs
->getEntities() as $key => $entity) {
$entity_number = $key + 1;
$this
->assertTrue($entity instanceof $this->entityClass, new FormattableMarkup("Denormalized entity number {$entity_number} is an instance of @class", [
'@class' => $this->entityClass,
]));
$this
->assertSame($entity
->getEntityTypeId(), $this->testEntities[$key]
->getEntityTypeId(), "Expected entity type foundfor entity number {$entity_number}.");
$this
->assertSame($entity
->bundle(), $this->testEntities[$key]
->bundle(), "Expected entity bundle found for entity number {$entity_number}.");
$this
->assertSame($entity
->uuid(), $this->testEntities[$key]
->uuid(), "Expected entity UUID found for entity number {$entity_number}.");
}
// @todo {@link https://www.drupal.org/node/2600460 Test context switches.}
}