Skip to content
Closed
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
5 changes: 3 additions & 2 deletions src/CsvFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ protected function detectLineBreak()
protected function readLine()
{
$this->validateLineBreak();
ini_set('auto_detect_line_endings', '1');

// allow empty enclosure hack
$enclosure = !$this->getEnclosure() ? chr(0) : $this->getEnclosure();
Expand Down Expand Up @@ -405,12 +406,12 @@ public function validateLineBreak()
Exception::INVALID_PARAM_STR
);
}
if (in_array($lineBreak, ["\r\n", "\n"])) {
if (in_array($lineBreak, ["\r\n", "\n", "\r"])) {
return $lineBreak;
}

throw new InvalidArgumentException(
"Invalid line break. Please use unix \\n or win \\r\\n line breaks.",
"Invalid line break. Please use unix \\n or win \\r\\n or Mac \\r line breaks.",
Exception::INVALID_PARAM,
null,
Exception::INVALID_PARAM_STR
Expand Down
42 changes: 22 additions & 20 deletions tests/CsvFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,13 @@ public function validCsvFiles()
];
}

public function testParse()
/**
* @dataProvider testParseProvider
* @throws \Keboola\Csv\InvalidArgumentException
*/
public function testParse($inputFilePath)
{
$csvFile = new CsvFile(__DIR__ . '/data/escaping.csv', ",", '"');
$csvFile = new CsvFile($inputFilePath, ",", '"');

$rows = [];
foreach ($csvFile as $row) {
Expand Down Expand Up @@ -102,6 +106,21 @@ public function testParse()
self::assertEquals($expected, $rows);
}

public function testParseProvider()
{
return [
'linux' => [
__DIR__ . '/data/escaping.csv',
],
'mac' => [
__DIR__ . '/data/escaping.mac.csv',
],
'win' => [
__DIR__ . '/data/escaping.win.csv'
],
];
}

public function testParseEscapedBy()
{
$csvFile = new CsvFile(__DIR__ . '/data/escapingEscapedBy.csv', ",", '"', '\\');
Expand Down Expand Up @@ -172,24 +191,7 @@ public function validLineBreaksData()
['test-input.win.csv', "\r\n", '\r\n'],
['escaping.csv', "\n", '\n'],
['just-header.csv', "\n", '\n'], // default
];
}

/**
* @expectedException \Keboola\Csv\InvalidArgumentException
* @dataProvider invalidLineBreaksData
* @param string $file
*/
public function testInvalidLineBreak($file)
{
$csvFile = new CsvFile(__DIR__ . '/data/' . $file);
$csvFile->validateLineBreak();
}

public function invalidLineBreaksData()
{
return [
['test-input.mac.csv'],
['test-input.mac.csv', "\r", '\r']
];
}

Expand Down
3 changes: 3 additions & 0 deletions tests/data/escaping.mac.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
col1,col2line without enclosure,second column"enclosure "" in column","hello \""line with enclosure","second column""column with enclosure "", and comma inside text","second column enclosure in text """"columns with
new line","columns with tab""Columns with WINDOWS
new line", "second""column with \n \t \\","second col"
Expand Down
10 changes: 10 additions & 0 deletions tests/data/escaping.win.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
col1,col2
line without enclosure,second column
"enclosure "" in column","hello \"
"line with enclosure","second column"
"column with enclosure "", and comma inside text","second column enclosure in text """
"columns with
new line","columns with tab"
"Columns with WINDOWS
new line", "second"
"column with \n \t \\","second col"