View source
<?php
namespace Drupal\Tests\serialization\Kernel;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\TypedData\MapDataDefinition;
use Drupal\KernelTests\KernelTestBase;
class MapDataNormalizerTest extends KernelTestBase {
protected static $modules = [
'system',
'serialization',
];
protected $serializer;
protected $typedDataManager;
protected function setUp() : void {
parent::setUp();
$this->serializer = \Drupal::service('serializer');
$this->typedDataManager = \Drupal::typedDataManager();
}
public function testMapNormalize() {
$typed_data = $this
->buildExampleTypedData();
$data = $this->serializer
->normalize($typed_data, 'json');
$expect_value = [
'key1' => 'value1',
'key2' => 'value2',
'key3' => 3,
'key4' => [
0 => TRUE,
1 => 'value6',
'key7' => 'value7',
],
];
$this
->assertSame($expect_value, $data);
}
public function testMapWithPropertiesNormalize() {
$typed_data = $this
->buildExampleTypedDataWithProperties();
$data = $this->serializer
->normalize($typed_data, 'json');
$expect_value = [
'key1' => 'value1',
'key2' => 'value2',
'key3' => 3,
'key4' => [
0 => TRUE,
1 => 'value6',
'key7' => 'value7',
],
];
$this
->assertSame($expect_value, $data);
}
protected function buildExampleTypedData() {
$tree = [
'key1' => 'value1',
'key2' => 'value2',
'key3' => 3,
'key4' => [
0 => TRUE,
1 => 'value6',
'key7' => 'value7',
],
];
$map_data_definition = MapDataDefinition::create();
$typed_data = $this->typedDataManager
->create($map_data_definition, $tree, 'test name');
return $typed_data;
}
protected function buildExampleTypedDataWithProperties() {
$tree = [
'key1' => 'value1',
'key2' => 'value2',
'key3' => 3,
'key4' => [
0 => TRUE,
1 => 'value6',
'key7' => 'value7',
],
];
$map_data_definition = MapDataDefinition::create()
->setPropertyDefinition('key1', DataDefinition::create('string'))
->setPropertyDefinition('key2', DataDefinition::create('string'))
->setPropertyDefinition('key3', DataDefinition::create('integer'))
->setPropertyDefinition('key4', MapDataDefinition::create()
->setPropertyDefinition(0, DataDefinition::create('boolean'))
->setPropertyDefinition(1, DataDefinition::create('string'))
->setPropertyDefinition('key7', DataDefinition::create('string')));
$typed_data = $this->typedDataManager
->create($map_data_definition, $tree, 'test name');
return $typed_data;
}
}