TypedDataDefinitionTest.php in Drupal 8
File
core/tests/Drupal/KernelTests/Core/TypedData/TypedDataDefinitionTest.php
View source
<?php
namespace Drupal\KernelTests\Core\TypedData;
use Drupal\Core\TypedData\ComplexDataDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\TypedData\DataDefinitionInterface;
use Drupal\Core\TypedData\DataReferenceDefinition;
use Drupal\Core\TypedData\DataReferenceDefinitionInterface;
use Drupal\Core\TypedData\ListDataDefinition;
use Drupal\Core\TypedData\ListDataDefinitionInterface;
use Drupal\Core\TypedData\MapDataDefinition;
use Drupal\KernelTests\KernelTestBase;
class TypedDataDefinitionTest extends KernelTestBase {
protected $typedDataManager;
protected function setUp() {
parent::setup();
$this->typedDataManager = $this->container
->get('typed_data_manager');
}
public function testLists() {
$list_definition = ListDataDefinition::create('string');
$this
->assertInstanceOf(ListDataDefinitionInterface::class, $list_definition);
$item_definition = $list_definition
->getItemDefinition();
$this
->assertInstanceOf(DataDefinitionInterface::class, $item_definition);
$this
->assertEqual($item_definition
->getDataType(), 'string');
$list_definition2 = $this->typedDataManager
->createListDataDefinition('string');
$this
->assertInstanceOf(ListDataDefinitionInterface::class, $list_definition2);
$this
->assertEqual($list_definition, $list_definition2);
$list_definition = $this->typedDataManager
->createDataDefinition('list');
$this
->assertInstanceOf(ListDataDefinitionInterface::class, $list_definition);
$this
->assertEqual($list_definition
->getDataType(), 'list');
$this
->assertEqual($list_definition
->getItemDefinition()
->getDataType(), 'any');
}
public function testMaps() {
$map_definition = MapDataDefinition::create()
->setPropertyDefinition('one', DataDefinition::create('string'))
->setPropertyDefinition('two', DataDefinition::create('string'))
->setPropertyDefinition('three', DataDefinition::create('string'));
$this
->assertInstanceOf(ComplexDataDefinitionInterface::class, $map_definition);
$this
->assertEqual(array_keys($map_definition
->getPropertyDefinitions()), [
'one',
'two',
'three',
]);
$this
->assertEqual($map_definition
->getPropertyDefinition('one')
->getDataType(), 'string');
$this
->assertNull($map_definition
->getMainPropertyName());
$this
->assertNull($map_definition
->getPropertyDefinition('invalid'));
$map_definition2 = $this->typedDataManager
->createDataDefinition('map');
$this
->assertInstanceOf(ComplexDataDefinitionInterface::class, $map_definition2);
$map_definition2
->setPropertyDefinition('one', DataDefinition::create('string'))
->setPropertyDefinition('two', DataDefinition::create('string'))
->setPropertyDefinition('three', DataDefinition::create('string'));
$this
->assertEqual(serialize($map_definition), serialize($map_definition2));
}
public function testDataReferences() {
$language_reference_definition = DataReferenceDefinition::create('language');
$this
->assertInstanceOf(DataReferenceDefinitionInterface::class, $language_reference_definition);
$this
->assertEqual($language_reference_definition
->getTargetDefinition()
->getDataType(), 'language');
$language_reference_definition2 = $this->typedDataManager
->createDataDefinition('language_reference');
$this
->assertInstanceOf(DataReferenceDefinitionInterface::class, $language_reference_definition2);
$this
->assertEqual(serialize($language_reference_definition), serialize($language_reference_definition2));
}
}