From b82ad03bd571ec69dc8cf403f92e6f367693370b Mon Sep 17 00:00:00 2001 From: Fonata Date: Sun, 29 Apr 2018 11:08:40 +0200 Subject: [PATCH 1/2] Ignore entirely empty lines at the end of files Closes #141 --- src/Csv.php | 2 +- tests/example_files/multiple_empty_lines.csv | 22 ++++++++++++++++++++ tests/methods/ParseTest.php | 22 ++++++++++++++++++-- 3 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 tests/example_files/multiple_empty_lines.csv diff --git a/src/Csv.php b/src/Csv.php index f3c6bd8..ced67a2 100644 --- a/src/Csv.php +++ b/src/Csv.php @@ -1170,7 +1170,7 @@ class Csv { $data = fread($fh, filesize($file)); fclose($fh); - return $data; + return rtrim($data, "\r\n"); } return false; diff --git a/tests/example_files/multiple_empty_lines.csv b/tests/example_files/multiple_empty_lines.csv new file mode 100644 index 0000000..e305066 --- /dev/null +++ b/tests/example_files/multiple_empty_lines.csv @@ -0,0 +1,22 @@ +ID,dID,createdAt,else1,else2,else3,else4,else5,user,else6,else7,else8,else9,else10,else11,else12,else13,else14,else15,else16,else17,else18,else19,else20,else21,else22,else23,else24,else25,else26,else27,else28,else29,else30,else31,else32,else33,else34,else35,else36,else37,else38 +418,0,2017-05-16,,,2018-01-22,22.01.2018 10:00:09,,admin,Ja,,10001,Abweichung,,10001,v1,1,ddd,100,1000,,HH,,v1,0,401,1,2,H1,,-1,10,1,111,Ja,2017-01-01,,12,0,11109,HH-100,default +419,0,2017-05-16,,,2017-05-16,14.07.2017 09:58:09,,admin,Ja,,,Abweichung,,10002,v2,1,ddd,200,500,DD,DD,,v2,1,402,2,4,H2,,-2,100,1,1111,Ja,2017-01-01,1,13,1,11109,DD-200,default +438,0,2017-05-16,,,2017-05-16,14.07.2017 09:58:29,,admin,Ja,,10021,Abweichung,,10021,v3,4,ddd,300,400,DD,DD,,v3,0,421,1,,H3,,-1,106,1,111,Ja,2017-05-08,,2,1,11109,DD-300,default +440,0,2017-05-16,,,,14.07.2017 09:58:53,,admin,Ja,,,Alt,,10023,v4,3,,400,500,BE,DD,,v4,1,423,3,,H4,,-3,143,1,1111,Ja,2017-01-01,1,33,1,11108,BE-400,default +441,0,2017-05-16,,,,14.07.2017 09:59:19,,admin,Ja,,,Fehlt,,10024,v5,3,,500,,BE,,,v5,2,424,4,,H5,,0,1435,0,111,Ja,2017-01-01,,12,1,0,BE-500,default +442,0,2017-05-16,,,,14.07.2017 10:00:46,,admin,Ja,,,Neu,,10025,v6,3,,100,,DD,,,v6,435,425,1,,H6,,0,10,0,1111,Ja,2017-01-01,,102,1,0,DD-100,default +443,0,2017-05-16,,,2017-07-04,14.07.2017 10:01:12,,admin,Ja,,,OK,,10026,v7,3,,200,200,DD,DD,,v7,32,426,2,2,H7,,0,100,0,111,Ja,2017-01-01,,77,1,0,DD-200,default +444,0,2017-05-16,,,,14.07.2017 10:02:13,,admin,Ja,,,Fehlt,,10027,v8,3,,300,,BE,,,v8,45,427,3,,H8,,0,200,0,1111,Ja,2017-01-01,,44,3,0,BE-300,default +445,0,2017-05-16,,,,14.07.2017 10:02:38,,admin,Ja,,,Fehlt,,10028,v9,3,,100,,BE,,,v9,45,428,4,,H9,,0,400,0,111,Ja,2017-01-01,,44,1,0,BE-100,default +446,0,2017-05-16,,,,14.07.2017 10:03:01,,admin,Ja,,,Fehlt,,10029,v10,3,,,400,,DD,,v10,45,429,,,H10,,0,1124,0,1111,Ja,2017-01-01,,89,1,0,,default + + + + + + + + + + + diff --git a/tests/methods/ParseTest.php b/tests/methods/ParseTest.php index b793a17..45c637d 100644 --- a/tests/methods/ParseTest.php +++ b/tests/methods/ParseTest.php @@ -126,6 +126,25 @@ class ParseTest extends TestCase { ], $aCity); } + public function testWithMultipleNewlines() { + $this->csv->auto(__DIR__ . '/../example_files/multiple_empty_lines.csv'); + $aElse9 = array_column($this->csv->data, 'else9'); + + /** @noinspection SpellCheckingInspection */ + $this->assertEquals([ + 'Abweichung', + 'Abweichung', + 'Abweichung', + 'Alt', + 'Fehlt', + 'Neu', + 'OK', + 'Fehlt', + 'Fehlt', + 'Fehlt', + ], $aElse9); + } + /** * @depends testSepRowAutoDetection */ @@ -217,8 +236,7 @@ class ParseTest extends TestCase { * * @return mixed Method return. */ - private function invokeMethod(&$object, $methodName, array $parameters = array()) - { + private function invokeMethod(&$object, $methodName, array $parameters = array()) { $reflection = new \ReflectionClass(get_class($object)); $method = $reflection->getMethod($methodName); $method->setAccessible(true); From 4504f4b158eec59b1144c88fc251386caffd6d37 Mon Sep 17 00:00:00 2001 From: Fonata Date: Sun, 29 Apr 2018 11:21:00 +0200 Subject: [PATCH 2/2] Converted function names to camelCase for PSR karma points --- tests/methods/ParseTest.php | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/tests/methods/ParseTest.php b/tests/methods/ParseTest.php index 45c637d..79a911a 100644 --- a/tests/methods/ParseTest.php +++ b/tests/methods/ParseTest.php @@ -22,13 +22,13 @@ class ParseTest extends TestCase { $this->csv = new Csv(); } - public function test_parse() { + public function testParse() { // can we trick 'is_readable' into whining? See #67. - $this->parse_repetitive_string('c:/looks/like/a/path'); - $this->parse_repetitive_string('http://looks/like/an/url'); + $this->parseRepetitiveString('c:/looks/like/a/path'); + $this->parseRepetitiveString('http://looks/like/an/url'); } - private function parse_repetitive_string($content) { + private function parseRepetitiveString($content) { $this->csv->delimiter = ';'; $this->csv->heading = false; $success = $this->csv->parse(str_repeat($content . ';', 500)); @@ -41,9 +41,11 @@ class ParseTest extends TestCase { } /** - * @depends test_parse + * @depends testParse * * @dataProvider autoDetectionProvider + * + * @param string $file */ public function testSepRowAutoDetection($file) { // This file (parse_test.php) is encoded in UTF-8, hence comparison will @@ -96,7 +98,7 @@ class ParseTest extends TestCase { ], array_map('next', $actual_data)); } - public function test_single_column() { + public function testSingleColumn() { $this->csv->auto(__DIR__ . '/../example_files/single_column.csv'); $expected = [ ['SMS' => '0444'], @@ -107,7 +109,8 @@ class ParseTest extends TestCase { $this->assertEquals($expected, $this->csv->data); } - public function test_Piwik_data() { + public function testMatomoData() { + // Matomo (Piwik) export cannot be read with $this->csv->use_mb_convert_encoding = true; $this->csv->output_encoding = 'UTF-8'; $this->csv->auto(__DIR__ . '/../example_files/Piwik_API_download.csv');