function TypedDataTest::testTypedDataListsLast

Same name and namespace in other branches
  1. main core/tests/Drupal/KernelTests/Core/TypedData/TypedDataTest.php \Drupal\KernelTests\Core\TypedData\TypedDataTest::testTypedDataListsLast()

Tests the last() method on typed data lists.

File

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

Class

TypedDataTest
Tests the functionality of all core data types.

Namespace

Drupal\KernelTests\Core\TypedData

Code

public function testTypedDataListsLast() : void {
  // Create an ItemList with two string items.
  $value = [
    'zero',
    'one',
  ];
  $typed_data = $this->createTypedData(ListDataDefinition::create('string'), $value);
  // Assert that the last item is the second one ('one').
  $this->assertEquals('one', $typed_data->last()
    ->getValue());
  // Add another item to the list and check the last item.
  $value[] = 'two';
  $typed_data = $this->createTypedData(ListDataDefinition::create('string'), $value);
  $this->assertEquals('two', $typed_data->last()
    ->getValue());
  // Check behavior with an empty list.
  $typed_data = $this->createTypedData(ListDataDefinition::create('string'), []);
  $this->assertNull($typed_data->last(), 'Empty list should return NULL.');
}

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