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\Component\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\Component\CsvParserTest
- class \Drupal\Tests\feeds\Unit\FeedsUnitTestCase uses FeedsMockingTrait, FeedsReflectionTrait
Expanded class hierarchy of CsvParserTest
File
- tests/
src/ Unit/ Component/ CsvParserTest.php, line 13
Namespace
Drupal\Tests\feeds\Unit\ComponentView source
class CsvParserTest extends FeedsUnitTestCase {
/**
* Tests parsing a CSV source with several line endings.
*
* @dataProvider provider
*/
public function testAlternateLineEnding(array $expected, $ending) {
$text = file_get_contents(dirname(dirname(dirname(dirname(__DIR__)))) . '/tests/resources/csv/example.csv');
$text = str_replace("\r\n", $ending, $text);
$parser = new \LimitIterator(CsvParser::createFromString($text), 0, 4);
$first = array_slice($expected, 0, 4);
$this
->assertSame(count(iterator_to_array($parser)), 4);
$this
->assertSame(count(iterator_to_array($parser)), 4);
foreach ($parser as $delta => $row) {
$this
->assertSame($first[$delta], $row);
}
// Test second batch.
$last_pos = $parser
->lastLinePos();
$parser = (new \LimitIterator(CsvParser::createFromString($text), 0, 4))
->setStartByte($last_pos);
$second = array_slice($expected, 4);
// // Test that rewinding works as expected.
$this
->assertSame(3, count(iterator_to_array($parser)));
$this
->assertSame(3, count(iterator_to_array($parser)));
foreach ($parser as $delta => $row) {
$this
->assertSame($second[$delta], $row);
}
}
/**
* Data provider for testAlternateLineEnding().
*/
public function provider() {
$expected = [
[
'Header A',
'Header B',
'Header C',
],
[
'"1"',
'"2"',
'"3"',
],
[
'qu"ote',
'qu"ote',
'qu"ote',
],
[
"\r\n\r\nline1",
"\r\n\r\nline2",
"\r\n\r\nline3",
],
[
"new\r\nline 1",
"new\r\nline 2",
"new\r\nline 3",
],
[
"\r\n\r\nline1\r\n\r\n",
"\r\n\r\nline2\r\n\r\n",
"\r\n\r\nline3\r\n\r\n",
],
[
'Col A',
'Col B',
'Col, C',
],
];
$unix = $expected;
array_walk_recursive($unix, function (&$item, $key) {
$item = str_replace("\r\n", "\n", $item);
});
$mac = $expected;
array_walk_recursive($mac, function (&$item, $key) {
$item = str_replace("\r\n", "\r", $item);
});
return [
[
$expected,
"\r\n",
],
[
$unix,
"\n",
],
[
$mac,
"\r",
],
];
}
/**
* @covers ::setHasHeader
* @covers ::getHeader
*/
public function testHasHeader() {
$file = dirname(dirname(dirname(dirname(__DIR__)))) . '/tests/resources/csv/example.csv';
$parser = CsvParser::createFromFilePath($file)
->setHasHeader();
$this
->assertSame(count(iterator_to_array($parser)), 6);
$this
->assertSame([
'Header A',
'Header B',
'Header C',
], $parser
->getHeader());
}
/**
* Tests using an asterisk as delimiter.
*/
public function testAlternateSeparator() {
// This implicitly tests lines without a newline.
$parser = CsvParser::createFromString("a*b*c")
->setDelimiter('*');
$this
->assertSame([
'a',
'b',
'c',
], iterator_to_array($parser)[0]);
}
/**
* Tries to create a CsvParser instance with an invalid file path.
*/
public function testInvalidFilePath() {
$this
->expectException(InvalidArgumentException::class);
CsvParser::createFromFilePath('beep boop');
}
/**
* Creates a new CsvParser instance with an invalid CSV source.
*/
public function testInvalidResourcePath() {
$this
->expectException(InvalidArgumentException::class);
new CsvParser('beep boop');
}
/**
* Basic test for parsing CSV.
*
* @dataProvider csvFileProvider
*/
public function testCsvParsing($file, $expected) {
$parser = CsvParser::createFromFilePath($file);
$parser
->setHasHeader();
$header = $parser
->getHeader();
$output = [];
$test = [];
foreach (iterator_to_array($parser) as $row) {
$new_row = [];
foreach ($row as $key => $value) {
if (isset($header[$key])) {
$new_row[$header[$key]] = $value;
}
}
$output[] = $new_row;
}
$this
->assertSame($expected, $output);
}
/**
* Data provider for testCsvParsing().
*/
public function csvFileProvider() {
$path = dirname(dirname(dirname(dirname(__DIR__)))) . '/tests/resources/csv-parser-component-files';
$return = [];
foreach (glob($path . '/csv/*.csv') as $file) {
$json_file = $path . '/json/' . str_replace('.csv', '.json', basename($file));
$return[] = [
$file,
json_decode(file_get_contents($json_file), TRUE),
];
}
return $return;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CsvParserTest:: |
public | function | Data provider for testCsvParsing(). | |
CsvParserTest:: |
public | function | Data provider for testAlternateLineEnding(). | |
CsvParserTest:: |
public | function | Tests parsing a CSV source with several line endings. | |
CsvParserTest:: |
public | function | Tests using an asterisk as delimiter. | |
CsvParserTest:: |
public | function | Basic test for parsing CSV. | |
CsvParserTest:: |
public | function | @covers ::setHasHeader @covers ::getHeader | |
CsvParserTest:: |
public | function | Tries to create a CsvParser instance with an invalid file path. | |
CsvParserTest:: |
public | function | Creates a new CsvParser instance with an invalid CSV source. | |
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. | |
FeedsUnitTestCase:: |
public | function |
Overrides UnitTestCase:: |
27 |
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. |