public function TypedDataTest::testTypedDataLists in Zircon Profile 8.0
Same name and namespace in other branches
- 8 core/modules/system/src/Tests/TypedData/TypedDataTest.php \Drupal\system\Tests\TypedData\TypedDataTest::testTypedDataLists()
Tests using typed data lists.
File
- core/
modules/ system/ src/ Tests/ TypedData/ TypedDataTest.php, line 315 - Contains \Drupal\system\Tests\TypedData\TypedDataTest.
Class
- TypedDataTest
- Tests the functionality of all core data types.
Namespace
Drupal\system\Tests\TypedDataCode
public function testTypedDataLists() {
// Test working with an existing list of strings.
$value = array(
'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
->assertTrue($item instanceof TypedDataInterface);
$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(array());
$this
->assertTrue($clone
->isEmpty());
// Make sure that resetting the value using NULL results in an empty array.
$clone
->setValue(array());
$typed_data
->setValue(NULL);
$this
->assertIdentical($typed_data
->getValue(), array());
$this
->assertIdentical($clone
->getValue(), array());
// Test dealing with NULL items.
$typed_data[] = NULL;
$this
->assertTrue($typed_data
->isEmpty());
$this
->assertEqual(count($typed_data), 1);
$typed_data[] = '';
$this
->assertFalse($typed_data
->isEmpty());
$this
->assertEqual(count($typed_data), 2);
$typed_data[] = 'three';
$this
->assertFalse($typed_data
->isEmpty());
$this
->assertEqual(count($typed_data), 3);
$this
->assertEqual($typed_data
->getValue(), array(
NULL,
'',
'three',
));
// Test unsetting.
unset($typed_data[1]);
$this
->assertEqual(count($typed_data), 2);
// 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
->assertEqual(count($typed_data), 2);
// Test setting the list with less values.
$typed_data
->setValue(array(
'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) {
$this
->pass('Exception thrown:' . $e
->getMessage());
}
}