function TypedDataTest::testTypedDataMaps

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/TypedData/TypedDataTest.php \Drupal\KernelTests\Core\TypedData\TypedDataTest::testTypedDataMaps()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/TypedData/TypedDataTest.php \Drupal\KernelTests\Core\TypedData\TypedDataTest::testTypedDataMaps()
  3. 10 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 510

Class

TypedDataTest
Tests the functionality of all core data types.

Namespace

Drupal\KernelTests\Core\TypedData

Code

public function testTypedDataMaps() : void {
  // Test working with a simple map.
  $value = [
    'one' => 'eins',
    'two' => 'beta',
    'three' => 'gamma',
  ];
  $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', 'alpha');
  $this->assertEquals('alpha', $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' => 'alpha',
    'two' => 'beta',
    'three' => 'gamma',
  ], $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' => 'gamma',
  ]);
  $this->assertEquals('eins, gamma', $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) {
    // 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) {
    // 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.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.