protected function EntityFieldTest::assertIterator

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::assertIterator()
EntityFieldTest::testIterator in drupal/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFieldTest.php
Tests iterating over properties.

File

drupal/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFieldTest.php, line 432
Definition of Drupal\system\Tests\Entity\EntityFieldTest.

Class

EntityFieldTest
Tests Entity API base functionality.

Namespace

Drupal\system\Tests\Entity

Code

protected function assertIterator($entity_type) {
  $entity = $this
    ->createTestEntity($entity_type);
  foreach ($entity as $name => $field) {
    $this
      ->assertTrue($field instanceof FieldInterface, $entity_type . ": Field {$name} implements interface.");
    foreach ($field as $delta => $item) {
      $this
        ->assertTrue($field[0] instanceof FieldItemInterface, $entity_type . ": Item {$delta} of field {$name} implements interface.");
      foreach ($item as $value_name => $value_property) {
        $this
          ->assertTrue($value_property instanceof TypedDataInterface, $entity_type . ": Value {$value_name} of item {$delta} of field {$name} implements interface.");
        $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.");
      }
    }
  }
  $properties = $entity
    ->getProperties();
  $this
    ->assertEqual(array_keys($properties), array_keys($entity
    ->getPropertyDefinitions()), format_string('%entity_type: All properties returned.', array(
    '%entity_type' => $entity_type,
  )));
  $this
    ->assertEqual($properties, iterator_to_array($entity
    ->getIterator()), format_string('%entity_type: Entity iterator iterates over all properties.', array(
    '%entity_type' => $entity_type,
  )));
}