You are here

private function CommerceMigrateBaseTestCase::readCsv in Commerce Migrate 7

Returns data from one of CSV files.

Parameters

string|\CommerceMigrateExampleMigration $class_name: The name of object which represents CSV file.

string $primary_key: One of values from the first column in CSV.

Return value

\stdClass Row from CSV.

2 calls to CommerceMigrateBaseTestCase::readCsv()
CommerceMigrateBaseTestCase::getOrdersCsvItem in tests/commerce_migrate_base.test
Returns raw data of an order from CSV file.
CommerceMigrateBaseTestCase::getProductsCsvItem in tests/commerce_migrate_base.test
Returns raw data of a product from CSV file.

File

tests/commerce_migrate_base.test, line 218
Base facade for "Commerce Migrate" tests.

Class

CommerceMigrateBaseTestCase
Class CommerceMigrateBaseTestCase.

Code

private function readCsv($class_name, $primary_key) {
  if (!isset($this->csvFiles[$class_name][$primary_key])) {
    $file_object = new \SplFileObject(drupal_get_path('module', 'commerce_migrate_example') . '/files/' . $class_name::FILE);
    $column_names = array_keys($class_name::csvColumns());
    $column_count = count($column_names);
    while ($file_object
      ->valid()) {
      $line = $file_object
        ->fgetcsv();

      // Empty lines.
      if ($column_count !== count($line)) {
        continue;
      }
      if (current($line) === $primary_key) {
        return $this->csvFiles[$class_name][$primary_key] = (object) array_combine($column_names, $line);
      }
    }
    throw new \RuntimeException(format_string('One of @type cannot be loaded by "@value"', array(
      '@type' => basename($class_name::FILE),
      '@value' => $primary_key,
    )));
  }
  return $this->csvFiles[$class_name][$primary_key];
}