function MigrateExecutableTest::getTestRollbackIdMap

Same name and namespace in other branches
  1. 9 core/modules/migrate/tests/src/Unit/MigrateExecutableTest.php \Drupal\Tests\migrate\Unit\MigrateExecutableTest::getTestRollbackIdMap()
  2. 11.x core/modules/migrate/tests/src/Unit/MigrateExecutableTest.php \Drupal\Tests\migrate\Unit\MigrateExecutableTest::getTestRollbackIdMap()

Returns an ID map object prophecy used in ::testRollback.

Return value

\Prophecy\Prophecy\ObjectProphecy An ID map object prophecy.

File

core/modules/migrate/tests/src/Unit/MigrateExecutableTest.php, line 595

Class

MigrateExecutableTest
@coversDefaultClass \Drupal\migrate\MigrateExecutable[[api-linebreak]] @group migrate

Namespace

Drupal\Tests\migrate\Unit

Code

public function getTestRollbackIdMap(array $items, array $source_id_keys, array $destination_id_keys) {
  static::$idMapRecords = array_map(function (array $item) {
    return $item + [
      'source_row_status' => '0',
      'rollback_action' => '0',
      'last_imported' => '0',
      'hash' => '',
    ];
  }, $items);
  $array_iterator = new \ArrayIterator(static::$idMapRecords);
  $id_map = $this->prophesize(MigrateIdMapInterface::class);
  $id_map->setMessage(Argument::cetera())
    ->willReturn(NULL);
  $id_map->rewind()
    ->will(function () use ($array_iterator) {
    $array_iterator->rewind();
  });
  $id_map->valid()
    ->will(function () use ($array_iterator) {
    return $array_iterator->valid();
  });
  $id_map->next()
    ->will(function () use ($array_iterator) {
    $array_iterator->next();
  });
  $id_map->currentDestination()
    ->will(function () use ($array_iterator, $destination_id_keys) {
    $current = $array_iterator->current();
    $destination_values = array_filter($current, function ($key) use ($destination_id_keys) {
      return in_array($key, $destination_id_keys, TRUE);
    }, ARRAY_FILTER_USE_KEY);
    return empty(array_filter($destination_values, 'is_null')) ? array_combine($destination_id_keys, array_values($destination_values)) : NULL;
  });
  $id_map->currentSource()
    ->will(function () use ($array_iterator, $source_id_keys) {
    $current = $array_iterator->current();
    $source_values = array_filter($current, function ($key) use ($source_id_keys) {
      return in_array($key, $source_id_keys, TRUE);
    }, ARRAY_FILTER_USE_KEY);
    return empty(array_filter($source_values, 'is_null')) ? array_combine($source_id_keys, array_values($source_values)) : NULL;
  });
  $id_map->getRowByDestination(Argument::type('array'))
    ->will(function () {
    $destination_ids = func_get_args()[0][0];
    $return = array_reduce(self::$idMapRecords, function (array $carry, array $record) use ($destination_ids) {
      if (array_merge($record, $destination_ids) === $record) {
        $carry = $record;
      }
      return $carry;
    }, []);
    return $return;
  });
  $id_map->deleteDestination(Argument::type('array'))
    ->will(function () {
    $destination_ids = func_get_args()[0][0];
    $matching_records = array_filter(self::$idMapRecords, function (array $record) use ($destination_ids) {
      return array_merge($record, $destination_ids) === $record;
    });
    foreach (array_keys($matching_records) as $record_key) {
      unset(self::$idMapRecords[$record_key]);
    }
  });
  $id_map->delete(Argument::type('array'))
    ->will(function () {
    $source_ids = func_get_args()[0][0];
    $matching_records = array_filter(self::$idMapRecords, function (array $record) use ($source_ids) {
      return array_merge($record, $source_ids) === $record;
    });
    foreach (array_keys($matching_records) as $record_key) {
      unset(self::$idMapRecords[$record_key]);
    }
  });
  return $id_map;
}

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