You are here

public function CsvParserTest::testParseWithoutHeaders in Feeds 8.3

Tests parsing with the "no_headers" option enabled.

File

tests/src/Unit/Feeds/Parser/CsvParserTest.php, line 99

Class

CsvParserTest
@coversDefaultClass \Drupal\feeds\Feeds\Parser\CsvParser @group feeds

Namespace

Drupal\Tests\feeds\Unit\Feeds\Parser

Code

public function testParseWithoutHeaders() {

  // Enable "no_headers" option.
  $config = [
    'no_headers' => TRUE,
  ] + $this->parser
    ->defaultFeedConfiguration();
  $this->feed
    ->expects($this
    ->any())
    ->method('getConfigurationFor')
    ->with($this->parser)
    ->will($this
    ->returnValue($config));

  // Provide mapping sources.
  $this->feedType
    ->method('getMappingSources')
    ->will($this
    ->returnValue([
    'column1' => [
      'label' => 'Column 1',
      'value' => 0,
      'machine_name' => 'column1',
    ],
    'column2' => [
      'label' => 'Column 2',
      'value' => 1,
      'machine_name' => 'column2',
    ],
  ]));
  $file = $this
    ->resourcesPath() . '/csv/content.csv';
  $fetcher_result = new FetcherResult($file);
  $result = $this->parser
    ->parse($this->feed, $fetcher_result, $this->state);

  // Assert that there are three items.
  $this
    ->assertSame(count($result), 3);

  // Assert that each item has the expected value on the machine name.
  $this
    ->assertSame('guid', $result[0]
    ->get('column1'));
  $this
    ->assertSame('title', $result[0]
    ->get('column2'));
  $this
    ->assertSame('1', $result[1]
    ->get('column1'));
  $this
    ->assertSame('Lorem ipsum', $result[1]
    ->get('column2'));
  $this
    ->assertSame('2', $result[2]
    ->get('column1'));
  $this
    ->assertSame('Ut wisi enim ad minim veniam', $result[2]
    ->get('column2'));
}