You are here

private function EntityHelper::getEntity in Feeds Paragraphs 8

Creates entity object

Parameters

string $type: The entity type.

string $bundle: The entity bundle.

int $id: The entity id.

string $host_field: The host field.

mixed $host: The host entity.

Return value

ObjectProphecy A mocked entity object.

1 call to EntityHelper::getEntity()
EntityHelper::__construct in tests/src/Unit/Helpers/EntityHelper.php

File

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

Class

EntityHelper

Namespace

Drupal\Tests\feeds_para_mapper\Unit\Helpers

Code

private function getEntity($type, $bundle, $id, $host_field = null, $host = null) {
  $class = Node::class;
  if ($type === 'paragraph') {
    $class = Paragraph::class;
  }
  $entity = $this->prophet
    ->prophesize($class);
  $entity
    ->isNew()
    ->willReturn(true);
  $that = $this;
  $entity
    ->hasField(Argument::type('string'))
    ->will(function ($args) use ($that, $type, $bundle) {
    if ($type === 'node') {
      $field = $that->fieldHelper->fields[0]
        ->reveal()
        ->getName();
      if ($field === $args[0]) {
        return true;
      }
      return false;
    }
    else {
      $fields = $that->fieldHelper
        ->getBundleFields($bundle);
      foreach ($fields as $field) {
        if ($field
          ->reveal()
          ->getName() === $args[0]) {
          return true;
        }
      }
      return false;
    }
  });
  $entity
    ->getEntityTypeId()
    ->willReturn($type);
  $entity
    ->bundle()
    ->willReturn($bundle);
  $entity
    ->id()
    ->willReturn($id);
  $that = $this;
  if (isset($host_field)) {
    $entity
      ->getParentEntity()
      ->willReturn($host);
  }
  $entity
    ->get(Argument::type('string'))
    ->will(function ($args) use ($entity, $host_field, $bundle, $that) {
    if ($args[0] === 'parent_field_name') {
      return $that
        ->getFieldItemListMock($host_field, 'text');
    }
    $fields = $that->fieldHelper
      ->getBundleFields($bundle);
    $found = array_filter($fields, function ($field) use ($args) {
      $name = $field
        ->reveal()
        ->getName();
      return $name === $args[0];
    })[0];
    return $that
      ->getFieldItemListMock($args[0], 'reference', $found
      ->reveal());
  });
  $entity
    ->getType()
    ->willReturn($bundle);
  $entity
    ->getFieldDefinitions()
    ->will(function ($args) use ($that, $type, $bundle) {
    return $that->fieldHelper
      ->getFieldDefinitions($type, $bundle);
  });
  $entity
    ->save()
    ->willReturn(TRUE);
  return $entity;
}