You are here

public function SpreadsheetIteratorTest::testIteration in Migrate Spreadsheet 8

Same name and namespace in other branches
  1. 2.0.x tests/src/Unit/SpreadsheetIteratorTest.php \Drupal\Tests\migrate_spreadsheet\Unit\SpreadsheetIteratorTest::testIteration()

@covers ::current

File

tests/src/Unit/SpreadsheetIteratorTest.php, line 134

Class

SpreadsheetIteratorTest
Tests the spreadsheet iterator.

Namespace

Drupal\Tests\migrate_spreadsheet\Unit

Code

public function testIteration() {
  $config = $this->iterator
    ->getConfiguration();
  $config['row_index_column'] = 'row';
  $this->iterator
    ->setConfiguration($config);
  $this
    ->assertTrue($this->iterator
    ->valid());
  $this
    ->assertSame([
    3,
  ], $this->iterator
    ->key());
  $this
    ->assertSame([
    'row' => 3,
    'column b' => 'cell b0',
    'column d' => 'cell d0',
    'column e' => 'cell e0',
  ], $this->iterator
    ->current());

  // Move the cursor.
  $this->iterator
    ->next();
  $this
    ->assertTrue($this->iterator
    ->valid());
  $this
    ->assertSame([
    4,
  ], $this->iterator
    ->key());
  $this
    ->assertSame([
    'row' => 4,
    'column b' => 'cell b1',
    'column d' => 7.0,
    // We test here a computed cell (E4 == 'D4+3.23').
    'column e' => 10.23,
  ], $this->iterator
    ->current());

  // Move the cursor.
  $this->iterator
    ->next();
  $this
    ->assertTrue($this->iterator
    ->valid());
  $this
    ->assertSame([
    5,
  ], $this->iterator
    ->key());
  $this
    ->assertSame([
    'row' => 5,
    'column b' => 'cell b2',
    'column d' => 'cell d2',
    'column e' => 'cell e2',
  ], $this->iterator
    ->current());

  // Move the cursor. Should run out of set.
  $this->iterator
    ->next();
  $this
    ->assertFalse($this->iterator
    ->valid());

  // Rewind.
  $this->iterator
    ->rewind();
  $this
    ->assertTrue($this->iterator
    ->valid());
  $this
    ->assertSame([
    3,
  ], $this->iterator
    ->key());
  $this
    ->assertSame([
    'row' => 3,
    'column b' => 'cell b0',
    'column d' => 'cell d0',
    'column e' => 'cell e0',
  ], $this->iterator
    ->current());

  // Try to return all columns.
  $config['columns'] = [];
  $this->iterator
    ->setConfiguration($config);
  $this
    ->assertTrue($this->iterator
    ->valid());
  $this
    ->assertSame([
    3,
  ], $this->iterator
    ->key());
  $this
    ->assertSame([
    'row' => 3,
    'column b' => 'cell b0',
    'column c' => 'cell c0',
    'column d' => 'cell d0',
    'column e' => 'cell e0',
    'column g' => 'cell g0',
  ], $this->iterator
    ->current());

  // Use different primary keys.
  $config['columns'] = [
    'column b',
    'column e',
  ];
  $config['keys'] = [
    'column c',
    'column d',
  ];
  unset($config['row_index_column']);
  $this->iterator
    ->setConfiguration($config);
  $this
    ->assertTrue($this->iterator
    ->valid());
  $this
    ->assertSame([
    'cell c0',
    'cell d0',
  ], $this->iterator
    ->key());
  $this
    ->assertSame([
    'column b' => 'cell b0',
    'column c' => 'cell c0',
    'column d' => 'cell d0',
    'column e' => 'cell e0',
  ], $this->iterator
    ->current());

  // Test with no header_row.
  unset($config['header_row']);
  $config['columns'] = [
    'B',
    'E',
  ];
  $config['keys'] = [
    'C',
    'D',
  ];
  $this->iterator
    ->setConfiguration($config);
  $this
    ->assertTrue($this->iterator
    ->valid());
  $this
    ->assertSame([
    'cell c0',
    'cell d0',
  ], $this->iterator
    ->key());
  $this
    ->assertSame([
    'B' => 'cell b0',
    'C' => 'cell c0',
    'D' => 'cell d0',
    'E' => 'cell e0',
  ], $this->iterator
    ->current());
}