diff --git a/src/CsvFile.php b/src/CsvFile.php index ee9892e..c7fae1a 100644 --- a/src/CsvFile.php +++ b/src/CsvFile.php @@ -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(); @@ -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 diff --git a/tests/CsvFileTest.php b/tests/CsvFileTest.php index 050dab3..30bf721 100644 --- a/tests/CsvFileTest.php +++ b/tests/CsvFileTest.php @@ -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) { @@ -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', ",", '"', '\\'); @@ -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'] ]; } diff --git a/tests/data/escaping.mac.csv b/tests/data/escaping.mac.csv new file mode 100644 index 0000000..13a926e --- /dev/null +++ b/tests/data/escaping.mac.csv @@ -0,0 +1,3 @@ +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" diff --git a/tests/data/escaping.win.csv b/tests/data/escaping.win.csv new file mode 100644 index 0000000..0db477b --- /dev/null +++ b/tests/data/escaping.win.csv @@ -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"