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. 10 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 359

Class

TypedDataTest
Tests the functionality of all core data types.

Namespace

Drupal\KernelTests\Core\TypedData

Code

public function testTypedDataLists() {
    // Test working with an existing list of strings.
    $value = [
        'one',
        'two',
        'three',
    ];
    $typed_data = $this->createTypedData(ListDataDefinition::create('string'), $value);
    $this->assertEqual($typed_data->getValue(), $value, 'List value has been set.');
    // Test iterating.
    $count = 0;
    foreach ($typed_data as $item) {
        $this->assertInstanceOf(TypedDataInterface::class, $item);
        $count++;
    }
    $this->assertEqual($count, 3);
    // Test getting the string representation.
    $this->assertEqual($typed_data->getString(), 'one, two, three');
    $typed_data[1] = '';
    $this->assertEqual($typed_data->getString(), 'one, three');
    // Test using array access.
    $this->assertEqual($typed_data[0]->getValue(), 'one');
    $typed_data[] = 'four';
    $this->assertEqual($typed_data[3]->getValue(), 'four');
    $this->assertEqual($typed_data->count(), 4);
    $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->assertTrue($typed_data->getValue() === $clone->getValue());
    $this->assertTrue($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->assertIdentical($typed_data->getValue(), []);
    $this->assertIdentical($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->assertEqual($typed_data->getValue(), [
        NULL,
        '',
        'three',
    ]);
    // Test unsetting.
    unset($typed_data[1]);
    $this->assertCount(2, $typed_data);
    // Check that items were shifted.
    $this->assertEqual($typed_data[1]->getValue(), 'three');
    // 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->assertEqual($typed_data->count(), 1);
    // 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.