function TypedDataTest::testTypedDataLists

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/TypedData/TypedDataTest.php \Drupal\KernelTests\Core\TypedData\TypedDataTest::testTypedDataLists()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/TypedData/TypedDataTest.php \Drupal\KernelTests\Core\TypedData\TypedDataTest::testTypedDataLists()
  3. 11.x core/tests/Drupal/KernelTests/Core/TypedData/TypedDataTest.php \Drupal\KernelTests\Core\TypedData\TypedDataTest::testTypedDataLists()

Tests using typed data lists.

File

core/tests/Drupal/KernelTests/Core/TypedData/TypedDataTest.php, line 387

Class

TypedDataTest
Tests the functionality of all core data types.

Namespace

Drupal\KernelTests\Core\TypedData

Code

public function testTypedDataLists() : void {
  // Test working with an existing list of strings.
  $value = [
    'one',
    'two',
    'three',
  ];
  $typed_data = $this->createTypedData(ListDataDefinition::create('string'), $value);
  $this->assertEquals($value, $typed_data->getValue(), 'List value has been set.');
  // Test iterating.
  $count = 0;
  foreach ($typed_data as $item) {
    $this->assertInstanceOf(TypedDataInterface::class, $item);
    $count++;
  }
  $this->assertEquals(3, $count);
  // Test getting the string representation.
  $this->assertEquals('one, two, three', $typed_data->getString());
  $typed_data[1] = '';
  $this->assertEquals('one, three', $typed_data->getString());
  // Test using array access.
  $this->assertEquals('one', $typed_data[0]->getValue());
  $typed_data[] = 'four';
  $this->assertEquals('four', $typed_data[3]->getValue());
  $this->assertEquals(4, $typed_data->count());
  $this->assertTrue(isset($typed_data[0]));
  $this->assertTrue(!isset($typed_data[6]));
  // Test isEmpty and cloning.
  $this->assertFalse($typed_data->isEmpty());
  $clone = clone $typed_data;
  $this->assertSame($typed_data->getValue(), $clone->getValue());
  $this->assertNotSame($typed_data[0], $clone[0]);
  $clone->setValue([]);
  $this->assertTrue($clone->isEmpty());
  // Make sure that resetting the value using NULL results in an empty array.
  $clone->setValue([]);
  $typed_data->setValue(NULL);
  $this->assertSame([], $typed_data->getValue());
  $this->assertSame([], $clone->getValue());
  // Test dealing with NULL items.
  $typed_data[] = NULL;
  $this->assertTrue($typed_data->isEmpty());
  $this->assertCount(1, $typed_data);
  $typed_data[] = '';
  $this->assertFalse($typed_data->isEmpty());
  $this->assertCount(2, $typed_data);
  $typed_data[] = 'three';
  $this->assertFalse($typed_data->isEmpty());
  $this->assertCount(3, $typed_data);
  $this->assertEquals([
    NULL,
    '',
    'three',
  ], $typed_data->getValue());
  // Test unsetting.
  unset($typed_data[1]);
  $this->assertCount(2, $typed_data);
  // Check that items were shifted.
  $this->assertEquals('three', $typed_data[1]->getValue());
  // Getting a not set list item returns NULL, and does not create a new item.
  $this->assertNull($typed_data[2]);
  $this->assertCount(2, $typed_data);
  // Test setting the list with less values.
  $typed_data->setValue([
    'one',
  ]);
  $this->assertEquals(1, $typed_data->count());
  // Test setting invalid values.
  try {
    $typed_data->setValue('string');
    $this->fail('No exception has been thrown when setting an invalid value.');
  } catch (\Exception $e) {
    // Expected exception; just continue testing.
  }
}

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