From 5a9a10aeeddf95c70b8242962de4f63696e0103c Mon Sep 17 00:00:00 2001 From: Alexander Vlasov Date: Tue, 29 Dec 2015 15:21:00 +0300 Subject: [PATCH 1/2] Incorrect parsing with single quote during auto-detection delimiter. --- parsecsv.lib.php | 2 ++ tests/methods/auto_test.php | 26 +++++++++++++++++++ .../fixtures/auto-double-enclosure.csv | 3 +++ .../fixtures/auto-single-enclosure.csv | 3 +++ 4 files changed, 34 insertions(+) create mode 100644 tests/methods/auto_test.php create mode 100644 tests/methods/fixtures/auto-double-enclosure.csv create mode 100644 tests/methods/fixtures/auto-single-enclosure.csv diff --git a/parsecsv.lib.php b/parsecsv.lib.php index 568ae82..a2c7def 100644 --- a/parsecsv.lib.php +++ b/parsecsv.lib.php @@ -495,6 +495,8 @@ class parseCSV { if (is_null($enclosure)) { $enclosure = $this->enclosure; + } else { + $this->enclosure = $enclosure; } if (is_null($preferred)) { diff --git a/tests/methods/auto_test.php b/tests/methods/auto_test.php new file mode 100644 index 0000000..124a9ef --- /dev/null +++ b/tests/methods/auto_test.php @@ -0,0 +1,26 @@ +auto($file, true, null, null, $enclosure); + $this->assertArrayHasKey('column1', $csv->data[0], 'Data parsed incorrectly with enclosure ' . $enclosure); + $this->assertEquals('value1', $csv->data[0]['column1'], 'Data parsed incorrectly with enclosure ' . $enclosure); + } +} diff --git a/tests/methods/fixtures/auto-double-enclosure.csv b/tests/methods/fixtures/auto-double-enclosure.csv new file mode 100644 index 0000000..47f7bec --- /dev/null +++ b/tests/methods/fixtures/auto-double-enclosure.csv @@ -0,0 +1,3 @@ +"column1","column2" +"value1","value2" +"value3","value4" \ No newline at end of file diff --git a/tests/methods/fixtures/auto-single-enclosure.csv b/tests/methods/fixtures/auto-single-enclosure.csv new file mode 100644 index 0000000..716beb2 --- /dev/null +++ b/tests/methods/fixtures/auto-single-enclosure.csv @@ -0,0 +1,3 @@ +'column1','column2' +'value1','value2' +'value3','value4' \ No newline at end of file From 38944ba221e33f80c79970143e3bb24cb68066e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Bl=C3=A4ul?= Date: Sun, 21 Jan 2018 21:47:28 +0100 Subject: [PATCH 2/2] Moved testAutoQuotes into existing test infrastructure --- .../auto-double-enclosure.csv | 0 .../auto-single-enclosure.csv | 0 tests/methods/ParseTest.php | 20 ++++++++++++++ tests/methods/auto_test.php | 26 ------------------- 4 files changed, 20 insertions(+), 26 deletions(-) rename tests/{methods/fixtures => example_files}/auto-double-enclosure.csv (100%) rename tests/{methods/fixtures => example_files}/auto-single-enclosure.csv (100%) delete mode 100644 tests/methods/auto_test.php diff --git a/tests/methods/fixtures/auto-double-enclosure.csv b/tests/example_files/auto-double-enclosure.csv similarity index 100% rename from tests/methods/fixtures/auto-double-enclosure.csv rename to tests/example_files/auto-double-enclosure.csv diff --git a/tests/methods/fixtures/auto-single-enclosure.csv b/tests/example_files/auto-single-enclosure.csv similarity index 100% rename from tests/methods/fixtures/auto-single-enclosure.csv rename to tests/example_files/auto-single-enclosure.csv diff --git a/tests/methods/ParseTest.php b/tests/methods/ParseTest.php index 0752e30..abe30d8 100644 --- a/tests/methods/ParseTest.php +++ b/tests/methods/ParseTest.php @@ -94,4 +94,24 @@ class ParseTest extends PHPUnit\Framework\TestCase { ], ]; } + + public function autoQuotesDataProvider() { + return array( + array('tests/methods/fixtures/auto-double-enclosure.csv', '"'), + array('tests/methods/fixtures/auto-single-enclosure.csv', "'"), + ); + } + + /** + * @dataProvider autoQuotesDataProvider + * + * @param string $file + * @param string $enclosure + */ + public function testAutoQuotes($file, $enclosure) { + $csv = new parseCSV(); + $csv->auto($file, true, null, null, $enclosure); + $this->assertArrayHasKey('column1', $csv->data[0], 'Data parsed incorrectly with enclosure ' . $enclosure); + $this->assertEquals('value1', $csv->data[0]['column1'], 'Data parsed incorrectly with enclosure ' . $enclosure); + } } diff --git a/tests/methods/auto_test.php b/tests/methods/auto_test.php deleted file mode 100644 index 124a9ef..0000000 --- a/tests/methods/auto_test.php +++ /dev/null @@ -1,26 +0,0 @@ -auto($file, true, null, null, $enclosure); - $this->assertArrayHasKey('column1', $csv->data[0], 'Data parsed incorrectly with enclosure ' . $enclosure); - $this->assertEquals('value1', $csv->data[0]['column1'], 'Data parsed incorrectly with enclosure ' . $enclosure); - } -}