View source
<?php
namespace Drupal\Tests\feeds\Unit\Feeds\Parser;
use Drupal\feeds\Exception\EmptyFeedException;
use Drupal\feeds\FeedInterface;
use Drupal\feeds\Feeds\Parser\CsvParser;
use Drupal\feeds\FeedTypeInterface;
use Drupal\feeds\Result\FetcherResult;
use Drupal\feeds\State;
use Drupal\feeds\StateInterface;
use Drupal\Tests\feeds\Unit\FeedsUnitTestCase;
class CsvParserTest extends FeedsUnitTestCase {
protected $parser;
protected $feedType;
protected $feed;
protected $state;
public function setUp() {
parent::setUp();
$this->feedType = $this
->createMock(FeedTypeInterface::class);
$configuration = [
'feed_type' => $this->feedType,
'line_limit' => 3,
];
$this->parser = new CsvParser($configuration, 'csv', []);
$this->parser
->setStringTranslation($this
->getStringTranslationStub());
$this->state = new State();
$this->feed = $this
->createMock(FeedInterface::class);
$this->feed
->expects($this
->any())
->method('getType')
->will($this
->returnValue($this->feedType));
}
public function testParse() {
$this->feedType
->method('getMappingSources')
->will($this
->returnValue([]));
$this->feed
->expects($this
->any())
->method('getConfigurationFor')
->with($this->parser)
->will($this
->returnValue($this->parser
->defaultFeedConfiguration()));
$file = $this
->resourcesPath() . '/csv/example.csv';
$fetcher_result = new FetcherResult($file);
$result = $this->parser
->parse($this->feed, $fetcher_result, $this->state);
$this
->assertSame(count($result), 3);
$this
->assertSame($result[0]
->get('Header A'), '"1"');
$result = $this->parser
->parse($this->feed, $fetcher_result, $this->state);
$this
->assertSame(count($result), 3);
$this
->assertSame($result[0]
->get('Header B'), "new\r\nline 2");
}
public function testParseWithoutHeaders() {
$config = [
'no_headers' => TRUE,
] + $this->parser
->defaultFeedConfiguration();
$this->feed
->expects($this
->any())
->method('getConfigurationFor')
->with($this->parser)
->will($this
->returnValue($config));
$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);
$this
->assertSame(count($result), 3);
$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'));
}
public function testEmptyFeed() {
$this->feedType
->method('getMappingSources')
->will($this
->returnValue([]));
touch('vfs://feeds/empty_file');
$result = new FetcherResult('vfs://feeds/empty_file');
$this
->expectException(EmptyFeedException::class);
$this->parser
->parse($this->feed, $result, $this->state);
}
public function testFeedWithExtraBlankLines() {
$this->feedType
->method('getMappingSources')
->will($this
->returnValue([]));
$configuration = [
'feed_type' => $this->feedType,
'line_limit' => 100,
];
$this->parser = new CsvParser($configuration, 'csv', []);
$this->parser
->setStringTranslation($this
->getStringTranslationStub());
$this->feed
->expects($this
->any())
->method('getConfigurationFor')
->with($this->parser)
->will($this
->returnValue($this->parser
->defaultFeedConfiguration()));
$file = $this
->resourcesPath() . '/csv/with-empty-lines.csv';
$fetcher_result = new FetcherResult($file);
$result = $this->parser
->parse($this->feed, $fetcher_result, $this->state);
$this
->assertCount(9, $result);
$result = $this->parser
->parse($this->feed, $fetcher_result, $this->state);
$this
->assertCount(0, $result);
$this
->assertEquals(StateInterface::BATCH_COMPLETE, $this->state->progress);
}
public function testGetMappingSources() {
$this
->assertSame([], $this->parser
->getMappingSources());
}
}