function EntityFieldTest::doTestIterator

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityFieldTest.php \Drupal\KernelTests\Core\Entity\EntityFieldTest::doTestIterator()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/EntityFieldTest.php \Drupal\KernelTests\Core\Entity\EntityFieldTest::doTestIterator()
  3. 11.x core/tests/Drupal/KernelTests/Core/Entity/EntityFieldTest.php \Drupal\KernelTests\Core\Entity\EntityFieldTest::doTestIterator()

Executes the iterator tests for the given entity type.

Parameters

string $entity_type: The entity type to run the tests with.

1 call to EntityFieldTest::doTestIterator()
EntityFieldTest::testIterator in core/tests/Drupal/KernelTests/Core/Entity/EntityFieldTest.php
Tests iterating over properties.

File

core/tests/Drupal/KernelTests/Core/Entity/EntityFieldTest.php, line 557

Class

EntityFieldTest
Tests the Entity Field API.

Namespace

Drupal\KernelTests\Core\Entity

Code

protected function doTestIterator($entity_type) {
    $entity = $this->createTestEntity($entity_type);
    foreach ($entity as $name => $field) {
        $this->assertInstanceOf(FieldItemListInterface::class, $field);
        foreach ($field as $delta => $item) {
            $this->assertInstanceOf(FieldItemInterface::class, $field[0]);
            foreach ($item as $value_name => $value_property) {
                $this->assertInstanceOf(TypedDataInterface::class, $value_property);
                $value = $value_property->getValue();
                $this->assertTrue(!isset($value) || is_scalar($value) || $value instanceof EntityInterface, $entity_type . ": Value {$value_name} of item {$delta} of field {$name} is a primitive or an entity.");
            }
        }
    }
    $fields = $entity->getFields();
    $this->assertEquals(array_keys($entity->getTypedData()
        ->getDataDefinition()
        ->getPropertyDefinitions()), array_keys($fields), "{$entity_type}: All fields returned.");
    $this->assertEquals(iterator_to_array($entity->getIterator()), $fields, "{$entity_type}: Entity iterator iterates over all fields.");
}

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