Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/Iterators/CsvIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,31 @@ public function __construct($filePath)
$this->filePath = $filePath;
}

/**
* @deprecated use setColumns instead
*/
public function parseColumns($columns): self
{
$this->columns = $columns;

return $this;
}

public function setColumns($columns): self
{
$this->columns = $columns;

return $this;
}

public function rewind(): void
{
fclose($this->file);

$this->file = fopen($this->filePath, 'r');

$this->currentCsvLine = fgetcsv($this->file);
$this->currentRow = 0;

$this->next();
}

public function current(): array
Expand All @@ -54,6 +64,12 @@ public function getGenerator(): Generator
{
$this->rewind();

if (empty($this->columns)) {
$this->columns = $this->currentCsvLine;

$this->next();
}

while ($this->valid()) {
$line = $this->current();

Expand Down
30 changes: 23 additions & 7 deletions tests/CsvIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class CsvIteratorTest extends HelpersTestCase
{
protected CsvIterator $csvIteratorClass;
protected ReflectionProperty $columnsProperty;
protected ReflectionProperty $filePathProperty;

public function setUp(): void
{
Expand All @@ -19,6 +20,9 @@ public function setUp(): void

$this->columnsProperty = new ReflectionProperty(CsvIterator::class, 'columns');
$this->columnsProperty->setAccessible(true);

$this->filePathProperty = new ReflectionProperty(CsvIterator::class, 'filePath');
$this->filePathProperty->setAccessible(true);
}

public function testOpenNotExistsFile()
Expand All @@ -40,6 +44,17 @@ public function testParseColumns()
$this->assertEquals($header, $actualHeader);
}

public function testSetColumns()
{
$header = $this->getJsonFixture('header.json');

$this->csvIteratorClass->setColumns($header);

$actualHeader = $this->columnsProperty->getValue($this->csvIteratorClass);

$this->assertEquals($header, $actualHeader);
}

public function testCurrent()
{
$currentLine = $this->csvIteratorClass->current();
Expand Down Expand Up @@ -73,11 +88,11 @@ public function testRewind()
$rowKey = $this->csvIteratorClass->key();
$currentLine = $this->csvIteratorClass->current();

$this->assertEquals(0, $rowKey);
$this->assertEquals(1, $rowKey);
$this->assertEqualsFixture('current_after_next_line.json', $currentLine);
}

public function testGenerator()
public function testGeneratorWithoutSettingColumnHeaders()
{
$result = [];
$generator = $this->csvIteratorClass->getGenerator();
Expand All @@ -88,16 +103,17 @@ public function testGenerator()

$rowKey = $this->csvIteratorClass->key();

$this->assertEquals(6, $rowKey);
$this->assertEquals(7, $rowKey);
$this->assertEqualsFixture('all_data.json', $result);
}

public function testGeneratorWithHeader()
public function testGeneratorWithSettingColumnHeaders()
{
$result = [];
$header = $this->getJsonFixture('header.json');

$this->csvIteratorClass->parseColumns($header);
$this->filePathProperty->setValue($this->csvIteratorClass, $this->getFixturePath('addresses_without_header.csv'));
$this->csvIteratorClass->setColumns($header);

$generator = $this->csvIteratorClass->getGenerator();

Expand All @@ -108,7 +124,7 @@ public function testGeneratorWithHeader()
$rowKey = $this->csvIteratorClass->key();

$this->assertEquals(6, $rowKey);
$this->assertEqualsFixture('all_data_with_header.json', $result);
$this->assertEqualsFixture('all_data.json', $result);
}

public function testGeneratorWithHeadersInvalidCount()
Expand All @@ -118,7 +134,7 @@ public function testGeneratorWithHeadersInvalidCount()

$header = $this->getJsonFixture('header_invalid_count.json');

$this->csvIteratorClass->parseColumns($header);
$this->csvIteratorClass->setColumns($header);

$generator = $this->csvIteratorClass->getGenerator();

Expand Down
5 changes: 5 additions & 0 deletions tests/fixtures/CsvIteratorTest/addresses_without_header.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
John,Doe,120 jefferson st.,Riverside, NJ, 08075
Jack,McGinnis,220 hobo Av.,Phila, PA,09119
"John ""Da Man""",Repici,120 Jefferson St.,Riverside, NJ,08075
Stephen,Tyler,"7452 Terrace ""At the Plaza"" road",SomeTown,SD, 91234
"Joan ""the bone"", Anne",Jet,"9th, at Terrace plc",Desert City,CO,00123
88 changes: 40 additions & 48 deletions tests/fixtures/CsvIteratorTest/all_data.json
Original file line number Diff line number Diff line change
@@ -1,50 +1,42 @@
[
[
"First Name",
"Last Name",
"Address",
"City",
"State",
"Zip"
],
[
"John",
"Doe",
"120 jefferson st.",
"Riverside",
" NJ",
" 08075"
],
[
"Jack",
"McGinnis",
"220 hobo Av.",
"Phila",
" PA",
"09119"
],
[
"John \"Da Man\"",
"Repici",
"120 Jefferson St.",
"Riverside",
" NJ",
"08075"
],
[
"Stephen",
"Tyler",
"7452 Terrace \"At the Plaza\" road",
"SomeTown",
"SD",
" 91234"
],
[
"Joan \"the bone\", Anne",
"Jet",
"9th, at Terrace plc",
"Desert City",
"CO",
"00123"
]
{
"First Name": "John",
"Last Name": "Doe",
"Address": "120 jefferson st.",
"City": "Riverside",
"State": " NJ",
"Zip": " 08075"
},
{
"First Name": "Jack",
"Last Name": "McGinnis",
"Address": "220 hobo Av.",
"City": "Phila",
"State": " PA",
"Zip": "09119"
},
{
"First Name": "John \"Da Man\"",
"Last Name": "Repici",
"Address": "120 Jefferson St.",
"City": "Riverside",
"State": " NJ",
"Zip": "08075"
},
{
"First Name": "Stephen",
"Last Name": "Tyler",
"Address": "7452 Terrace \"At the Plaza\" road",
"City": "SomeTown",
"State": "SD",
"Zip": " 91234"
},
{
"First Name": "Joan \"the bone\", Anne",
"Last Name": "Jet",
"Address": "9th, at Terrace plc",
"City": "Desert City",
"State": "CO",
"Zip": "00123"
}
]
50 changes: 0 additions & 50 deletions tests/fixtures/CsvIteratorTest/all_data_with_header.json

This file was deleted.