class CsvParserTest in Feeds 8.3
Same name in this branch
- 8.3 tests/src/Unit/Component/CsvParserTest.php \Drupal\Tests\feeds\Unit\Component\CsvParserTest
- 8.3 tests/src/FunctionalJavascript/Feeds/Parser/CsvParserTest.php \Drupal\Tests\feeds\FunctionalJavascript\Feeds\Parser\CsvParserTest
- 8.3 tests/src/Unit/Feeds/Parser/CsvParserTest.php \Drupal\Tests\feeds\Unit\Feeds\Parser\CsvParserTest
- 8.3 tests/src/Kernel/Feeds/Parser/CsvParserTest.php \Drupal\Tests\feeds\Kernel\Feeds\Parser\CsvParserTest
@coversDefaultClass \Drupal\feeds\Feeds\Parser\CsvParser @group feeds
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\feeds\Unit\FeedsUnitTestCase uses FeedsMockingTrait, FeedsReflectionTrait
- class \Drupal\Tests\feeds\Unit\Feeds\Parser\CsvParserTest
- class \Drupal\Tests\feeds\Unit\FeedsUnitTestCase uses FeedsMockingTrait, FeedsReflectionTrait
Expanded class hierarchy of CsvParserTest
File
- tests/
src/ Unit/ Feeds/ Parser/ CsvParserTest.php, line 18
Namespace
Drupal\Tests\feeds\Unit\Feeds\ParserView source
class CsvParserTest extends FeedsUnitTestCase {
/**
* The Feeds parser plugin under test.
*
* @var \Drupal\feeds\Feeds\Parser\CsvParser
*/
protected $parser;
/**
* The feed type entity.
*
* @var \Drupal\feeds\FeedTypeInterface
*/
protected $feedType;
/**
* The feed entity.
*
* @var \Drupal\feeds\FeedInterface
*/
protected $feed;
/**
* The state object.
*
* @var \Drupal\feeds\StateInterface
*/
protected $state;
/**
* {@inheritdoc}
*/
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));
}
/**
* Tests parsing a CSV file that succeeds.
*
* @covers ::parse
*/
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"');
// Parse again. Tests batching.
$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");
}
/**
* Tests parsing with the "no_headers" option enabled.
*/
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'));
}
/**
* Tests parsing an empty CSV file.
*
* @covers ::parse
*/
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);
}
/**
* Tests parsing a file with a few extra blank lines.
*/
public function testFeedWithExtraBlankLines() {
$this->feedType
->method('getMappingSources')
->will($this
->returnValue([]));
// Set an high line limit.
$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);
// Parse again.
$result = $this->parser
->parse($this->feed, $fetcher_result, $this->state);
$this
->assertCount(0, $result);
// Assert that parsing has finished.
$this
->assertEquals(StateInterface::BATCH_COMPLETE, $this->state->progress);
}
/**
* @covers ::getMappingSources
*/
public function testGetMappingSources() {
// Not really much to test here.
$this
->assertSame([], $this->parser
->getMappingSources());
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CsvParserTest:: |
protected | property | The feed entity. | |
CsvParserTest:: |
protected | property | The feed type entity. | |
CsvParserTest:: |
protected | property | The Feeds parser plugin under test. | |
CsvParserTest:: |
protected | property | The state object. | |
CsvParserTest:: |
public | function |
Overrides FeedsUnitTestCase:: |
|
CsvParserTest:: |
public | function | Tests parsing an empty CSV file. | |
CsvParserTest:: |
public | function | Tests parsing a file with a few extra blank lines. | |
CsvParserTest:: |
public | function | @covers ::getMappingSources | |
CsvParserTest:: |
public | function | Tests parsing a CSV file that succeeds. | |
CsvParserTest:: |
public | function | Tests parsing with the "no_headers" option enabled. | |
FeedsMockingTrait:: |
protected | function | Mocks an account object. | |
FeedsMockingTrait:: |
protected | function | Returns a mocked AccountSwitcher object. | |
FeedsMockingTrait:: |
protected | function | Returns a mocked feed entity. | |
FeedsMockingTrait:: |
protected | function | Returns a mocked feed type entity. | |
FeedsMockingTrait:: |
protected | function | Mocks a field definition. | 1 |
FeedsMockingTrait:: |
protected | function | Mocks the file system. | |
FeedsReflectionTrait:: |
protected | function | Calls a protected method on the given object. | |
FeedsReflectionTrait:: |
protected | function | Gets a ReflectionMethod for a class method. | |
FeedsReflectionTrait:: |
protected | function | Returns a dynamically created closure for the object's method. | |
FeedsReflectionTrait:: |
protected | function | Sets a protected property. | |
FeedsUnitTestCase:: |
protected | function | Returns the absolute directory path of the Feeds module. | |
FeedsUnitTestCase:: |
protected | function | Defines stub constants. | |
FeedsUnitTestCase:: |
protected | function | Returns a mock stream wrapper manager. | |
FeedsUnitTestCase:: |
protected | function | Returns the absolute directory path of the resources folder. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |