You are here

public function CSVUnitTest::testInitializeIterator in Migrate Source CSV 8.2

Same name and namespace in other branches
  1. 8.3 tests/src/Unit/Plugin/migrate/source/CSVUnitTest.php \Drupal\Tests\migrate_source_csv\Unit\Plugin\migrate\source\CSVUnitTest::testInitializeIterator()

Tests initialization of the iterator.

@covers ::initializeIterator

File

tests/src/Unit/Plugin/migrate/source/CSVUnitTest.php, line 113

Class

CSVUnitTest
@coversDefaultClass \Drupal\migrate_source_csv\Plugin\migrate\source\CSV

Namespace

Drupal\Tests\migrate_source_csv\Unit\Plugin\migrate\source

Code

public function testInitializeIterator() {
  $configuration = [
    'path' => $this->happyPath,
    'keys' => [
      'id',
    ],
    'header_row_count' => 1,
  ];
  $config_common = [
    'path' => $this->sad,
    'keys' => [
      'id',
    ],
  ];
  $config_delimiter = [
    'delimiter' => '|',
  ];
  $config_enclosure = [
    'enclosure' => '%',
  ];
  $config_escape = [
    'escape' => '`',
  ];
  $csv = new CSV($config_common + $config_delimiter, $this->pluginId, $this->pluginDefinition, $this->migration);
  $this
    ->assertEquals(current($config_delimiter), $csv
    ->initializeIterator()
    ->getCsvControl()[0]);
  $this
    ->assertEquals('"', $csv
    ->initializeIterator()
    ->getCsvControl()[1]);
  $csv = new CSV($config_common + $config_enclosure, $this->pluginId, $this->pluginDefinition, $this->migration);
  $this
    ->assertEquals(',', $csv
    ->initializeIterator()
    ->getCsvControl()[0]);
  $this
    ->assertEquals(current($config_enclosure), $csv
    ->initializeIterator()
    ->getCsvControl()[1]);
  $csv = new CSV($config_common + $config_delimiter + $config_enclosure + $config_escape, $this->pluginId, $this->pluginDefinition, $this->migration);
  $csv_file_object = $csv
    ->initializeIterator();
  $row = [
    '1',
    'Justin',
    'Dean',
    'jdean0@example.com',
    'Indonesia',
    '60.242.130.40',
  ];
  $csv_file_object
    ->rewind();
  $current = $csv_file_object
    ->current();
  $this
    ->assertArrayEquals($row, $current);
  $csv = new CSV($configuration, $this->pluginId, $this->pluginDefinition, $this->migration);
  $csv_file_object = $csv
    ->initializeIterator();
  $row = [
    'id' => '1',
    'first_name' => 'Justin',
    'last_name' => 'Dean',
    'email' => 'jdean0@example.com',
    'country' => 'Indonesia',
    'ip_address' => '60.242.130.40',
  ];
  $second_row = [
    'id' => '2',
    'first_name' => 'Joan',
    'last_name' => 'Jordan',
    'email' => 'jjordan1@example.com',
    'country' => 'Thailand',
    'ip_address' => '137.230.209.171',
  ];
  $csv_file_object
    ->rewind();
  $current = $csv_file_object
    ->current();
  $this
    ->assertArrayEquals($row, $current);
  $csv_file_object
    ->next();
  $next = $csv_file_object
    ->current();
  $this
    ->assertArrayEquals($second_row, $next);
  $column_names = [
    'column_names' => [
      0 => [
        'id' => 'identifier',
      ],
      2 => [
        'last_name' => 'User last name',
      ],
    ],
  ];
  $csv = new CSV($configuration + $column_names, $this->pluginId, $this->pluginDefinition, $this->migration);
  $csv_file_object = $csv
    ->initializeIterator();
  $row = [
    'id' => '1',
    'last_name' => 'Dean',
  ];
  $second_row = [
    'id' => '2',
    'last_name' => 'Jordan',
  ];
  $csv_file_object
    ->rewind();
  $current = $csv_file_object
    ->current();
  $this
    ->assertArrayEquals($row, $current);
  $csv_file_object
    ->next();
  $next = $csv_file_object
    ->current();
  $this
    ->assertArrayEquals($second_row, $next);
}