You are here

public function EntityHelper::getFieldItemListMock in Feeds Paragraphs 8

Parameters

string $field:

string $type:

mixed $instance:

Return value

EntityReferenceRevisionsFieldItemList

File

tests/src/Unit/Helpers/EntityHelper.php, line 155

Class

EntityHelper

Namespace

Drupal\Tests\feeds_para_mapper\Unit\Helpers

Code

public function getFieldItemListMock($field, $type = "reference", $instance = null) {
  $class = EntityReferenceRevisionsFieldItemList::class;
  $values =& $this->values;
  if ($type !== 'reference') {
    $class = FieldItemList::class;
    $values =& $this->host_fields_values;
  }
  $fieldItem = $this->prophet
    ->prophesize($class);
  $fieldItem
    ->getValue()
    ->will(function ($args) use ($field, $values) {
    $result = array();
    if (isset($values[$field])) {
      $result = $values[$field];
    }
    return $result;
  });
  $fieldItem
    ->appendItem(Argument::any())
    ->will(function ($args) use ($field, &$values) {
    $v = array();
    if (isset($values[$field])) {
      $v = $values[$field];
    }
    $v[] = array(
      'entity' => $args[0],
    );
    $values[$field] = $v;
    return $this
      ->reveal();
  });
  $fieldItem
    ->set(Argument::type('int'), Argument::any())
    ->will(function ($args) use ($field, &$values) {
    $v = array();
    if (isset($values[$field])) {
      $v = $values[$field];
    }
    $v[$args[0]] = $args[1];
    $values[$field] = $v;
    return $this
      ->reveal();
  });
  $fieldItem
    ->removeItem(Argument::type('int'))
    ->will(function ($args) use ($field, &$values) {
    $v = array();
    if (isset($values[$field])) {
      $v = $values[$field];
    }
    unset($v[$args[0]]);
    $values[$field] = $v;
    return $this
      ->reveal();
  });
  $fieldItem
    ->getFieldDefinition()
    ->willReturn($instance);
  return $fieldItem
    ->reveal();
}