public function TypedDataTest::testTypedDataMaps in Drupal 9
Same name and namespace in other branches
- 8 core/tests/Drupal/KernelTests/Core/TypedData/TypedDataTest.php \Drupal\KernelTests\Core\TypedData\TypedDataTest::testTypedDataMaps()
Tests using a typed data map.
File
- core/
tests/ Drupal/ KernelTests/ Core/ TypedData/ TypedDataTest.php, line 475
Class
- TypedDataTest
- Tests the functionality of all core data types.
Namespace
Drupal\KernelTests\Core\TypedDataCode
public function testTypedDataMaps() {
// Test working with a simple map.
$value = [
'one' => 'eins',
'two' => 'zwei',
'three' => 'drei',
];
$definition = MapDataDefinition::create()
->setPropertyDefinition('one', DataDefinition::create('string'))
->setPropertyDefinition('two', DataDefinition::create('string'))
->setPropertyDefinition('three', DataDefinition::create('string'));
$typed_data = $this
->createTypedData($definition, $value);
// Test iterating.
$count = 0;
foreach ($typed_data as $item) {
$this
->assertInstanceOf(TypedDataInterface::class, $item);
$count++;
}
$this
->assertEquals(3, $count);
// Test retrieving metadata.
$this
->assertEquals(array_keys($value), array_keys($typed_data
->getDataDefinition()
->getPropertyDefinitions()));
$definition = $typed_data
->getDataDefinition()
->getPropertyDefinition('one');
$this
->assertEquals('string', $definition
->getDataType());
$this
->assertNull($typed_data
->getDataDefinition()
->getPropertyDefinition('invalid'));
// Test getting and setting properties.
$this
->assertEquals('eins', $typed_data
->get('one')
->getValue());
$this
->assertEquals($value, $typed_data
->toArray());
$typed_data
->set('one', 'uno');
$this
->assertEquals('uno', $typed_data
->get('one')
->getValue());
// Make sure the update is reflected in the value of the map also.
$value = $typed_data
->getValue();
$this
->assertEquals([
'one' => 'uno',
'two' => 'zwei',
'three' => 'drei',
], $value);
$properties = $typed_data
->getProperties();
$this
->assertEquals(array_keys($value), array_keys($properties));
$this
->assertSame($typed_data
->get('one'), $properties['one'], 'Properties are identical.');
// Test setting a not defined property. It shouldn't show up in the
// properties, but be kept in the values.
$typed_data
->setValue([
'foo' => 'bar',
]);
$this
->assertEquals([
'one',
'two',
'three',
], array_keys($typed_data
->getProperties()));
$this
->assertEquals([
'foo',
'one',
'two',
'three',
], array_keys($typed_data
->getValue()));
// Test getting the string representation.
$typed_data
->setValue([
'one' => 'eins',
'two' => '',
'three' => 'drei',
]);
$this
->assertEquals('eins, drei', $typed_data
->getString());
// Test isEmpty and cloning.
$this
->assertFalse($typed_data
->isEmpty());
$clone = clone $typed_data;
$this
->assertSame($typed_data
->getValue(), $clone
->getValue());
$this
->assertNotSame($typed_data
->get('one'), $clone
->get('one'));
$clone
->setValue([]);
$this
->assertTrue($clone
->isEmpty());
// Make sure the difference between NULL (not set) and an empty array is
// kept.
$typed_data
->setValue(NULL);
$this
->assertNull($typed_data
->getValue());
$typed_data
->setValue([]);
$value = $typed_data
->getValue();
$this
->assertIsArray($value);
// Test accessing invalid properties.
$typed_data
->setValue($value);
try {
$typed_data
->get('invalid');
$this
->fail('No exception has been thrown when getting an invalid value.');
} catch (\Exception $e) {
// Expected exception; just continue testing.
}
// Test setting invalid values.
try {
$typed_data
->setValue('invalid');
$this
->fail('No exception has been thrown when setting an invalid value.');
} catch (\Exception $e) {
// Expected exception; just continue testing.
}
// Test adding a new property to the map.
$typed_data
->getDataDefinition()
->setPropertyDefinition('zero', DataDefinition::create('any'));
$typed_data
->set('zero', 'null');
$this
->assertEquals('null', $typed_data
->get('zero')
->getValue());
$definition = $typed_data
->get('zero')
->getDataDefinition();
$this
->assertEquals('any', $definition
->getDataType(), 'Definition for a new map entry returned.');
}