From 01f0891cfb22265b984b028f5674f05176f1fb0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Bl=C3=A4ul?= Date: Sun, 21 Jan 2018 22:08:39 +0100 Subject: [PATCH 1/6] Test some conditions --- tests/methods/ConditionsTest.php | 74 ++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 tests/methods/ConditionsTest.php diff --git a/tests/methods/ConditionsTest.php b/tests/methods/ConditionsTest.php new file mode 100644 index 0000000..245721f --- /dev/null +++ b/tests/methods/ConditionsTest.php @@ -0,0 +1,74 @@ +csv = new parseCSV(); + } + + public function testNotDanBrown() { + $this->csv->conditions = 'author does not contain dan brown'; + + $this->_compareWithExpected([ + 'The Killing Kind', + 'The Third Secret', + 'The Last Templar', + 'The Traveller', + 'Crisis Four', + 'Prey', + 'The Broker (Paperback)', + 'Without Blood (Paperback)', + 'State of Fear (Paperback)', + 'The Rule of Four (Paperback)', + ]); + } + + public function testRating() { + $this->csv->conditions = 'rating < 3'; + $this->_compareWithExpected([ + 'The Killing Kind', + 'The Third Secret', + ]); + + $this->csv->conditions = 'rating >= 5'; + $this->_compareWithExpected([ + 'The Traveller', + 'Prey', + 'State of Fear (Paperback)', + 'Digital Fortress : A Thriller (Mass Market Paperback)', + 'Angels & Demons (Mass Market Paperback)', + ]); + } + + public function testTitleContainsSecretOrCode() { + $this->csv->conditions = 'title contains code OR title contains SECRET'; + + $this->_compareWithExpected([ + 'The Third Secret', + 'The Da Vinci Code (Hardcover)', + ]); + } + + protected function _compareWithExpected($expected) { + $this->csv->auto(__DIR__ . '/../../examples/_books.csv'); + $actual = array_map(function ($row) { + return $row['title']; + }, $this->csv->data); + $this->assertEquals($expected, $actual); + } +} From 7470d2b804fd4b019155a67d7ed32a3c215ba7ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Bl=C3=A4ul?= Date: Fri, 2 Feb 2018 12:10:25 +0100 Subject: [PATCH 2/6] PHPUnit: Repaired file paths in autoQuotesDataProvider() --- tests/methods/ParseTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/methods/ParseTest.php b/tests/methods/ParseTest.php index c7f8fcb..b6c4036 100644 --- a/tests/methods/ParseTest.php +++ b/tests/methods/ParseTest.php @@ -152,8 +152,8 @@ 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', "'"), + array('auto-double-enclosure.csv', '"'), + array('auto-single-enclosure.csv', "'"), ); } @@ -165,7 +165,7 @@ class ParseTest extends PHPUnit\Framework\TestCase { */ public function testAutoQuotes($file, $enclosure) { $csv = new parseCSV(); - $csv->auto($file, true, null, null, $enclosure); + $csv->auto(__DIR__ . '/../example_files/' . $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); } From 75902f4a22a620263671873143846b727f80d408 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Bl=C3=A4ul?= Date: Fri, 2 Feb 2018 12:22:23 +0100 Subject: [PATCH 3/6] Added PHPUnit test for sorting; I could not get the rows in the right... order without calling array_values - maybe this helps just PHPUnit. I consider the risk of breaking library users' code unlikely. --- parsecsv.lib.php | 3 ++ tests/properties/BaseClass.php | 31 ++++++++++++++++++ tests/properties/SortByTest.php | 56 +++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 tests/properties/BaseClass.php create mode 100644 tests/properties/SortByTest.php diff --git a/parsecsv.lib.php b/parsecsv.lib.php index beffd1e..4ea9e08 100644 --- a/parsecsv.lib.php +++ b/parsecsv.lib.php @@ -730,6 +730,9 @@ class parseCSV { $this->sort_reverse ? krsort($rows, $sort_type) : ksort($rows, $sort_type); + // Avoid issues with mixing string and integer keys: + $rows = array_values($rows); + if ($this->offset !== null || $this->limit !== null) { $rows = array_slice($rows, ($this->offset === null ? 0 : $this->offset), $this->limit, true); } diff --git a/tests/properties/BaseClass.php b/tests/properties/BaseClass.php new file mode 100644 index 0000000..4d9c420 --- /dev/null +++ b/tests/properties/BaseClass.php @@ -0,0 +1,31 @@ +csv = new parseCSV(); + } + + protected function _compareWithExpected($expected) { + $this->csv->auto(__DIR__ . '/../../examples/_books.csv'); + $actual = array_map(function ($row) { + return $row['title']; + }, $this->csv->data); + $this->assertEquals($expected, $actual); + } +} diff --git a/tests/properties/SortByTest.php b/tests/properties/SortByTest.php new file mode 100644 index 0000000..c26d4bb --- /dev/null +++ b/tests/properties/SortByTest.php @@ -0,0 +1,56 @@ +csv->sort_by = 'rating'; + $this->_compareWithExpected([ + // Rating 0 + 'The Killing Kind', + 'The Third Secret', + + // Rating 3 + 'The Last Templar', + 'The Broker (Paperback)', + 'Without Blood (Paperback)', + + // Rating 4 + 'Deception Point (Paperback)', + 'The Rule of Four (Paperback)', + 'The Da Vinci Code (Hardcover)', + + // Rating 5 + 'State of Fear (Paperback)', + 'Prey', + 'Digital Fortress : A Thriller (Mass Market Paperback)', + 'Angels & Demons (Mass Market Paperback)', + ]); + } + + public function testReverseSortByRating() { + $this->csv->sort_by = 'rating'; + $this->csv->sort_reverse = true; + $this->_compareWithExpected([ + + // Rating 5 + 'Digital Fortress : A Thriller (Mass Market Paperback)', + 'Prey', + 'State of Fear (Paperback)', + 'Angels & Demons (Mass Market Paperback)', + + // Rating 4 + 'The Da Vinci Code (Hardcover)', + 'The Rule of Four (Paperback)', + 'Deception Point (Paperback)', + + // Rating 3 + 'The Broker (Paperback)', + 'The Last Templar', + 'Without Blood (Paperback)', + + // Rating 0 + 'The Third Secret', + 'The Killing Kind', + ]); + } +} From ca8446995070a66c9c78fb4bb347cdaa44af03b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Bl=C3=A4ul?= Date: Fri, 2 Feb 2018 12:24:42 +0100 Subject: [PATCH 4/6] Moved ConditionsTest to properties subfolder --- tests/Bootstrap.php | 2 ++ .../ConditionsTest.php | 29 +------------------ 2 files changed, 3 insertions(+), 28 deletions(-) rename tests/{methods => properties}/ConditionsTest.php (66%) diff --git a/tests/Bootstrap.php b/tests/Bootstrap.php index 0999d1c..f87a6a3 100644 --- a/tests/Bootstrap.php +++ b/tests/Bootstrap.php @@ -9,3 +9,5 @@ if (!class_exists('PHPUnit\Framework\TestCase')) { // we run on an older PHPUnit version without namespaces. require_once __DIR__ . '/PHPUnit_Framework_TestCase.inc.php'; } + +require_once BASE . 'tests/properties/BaseClass.php'; diff --git a/tests/methods/ConditionsTest.php b/tests/properties/ConditionsTest.php similarity index 66% rename from tests/methods/ConditionsTest.php rename to tests/properties/ConditionsTest.php index 245721f..7326209 100644 --- a/tests/methods/ConditionsTest.php +++ b/tests/properties/ConditionsTest.php @@ -1,25 +1,6 @@ csv = new parseCSV(); - } +class ConditionsTest extends BaseClass { public function testNotDanBrown() { $this->csv->conditions = 'author does not contain dan brown'; @@ -63,12 +44,4 @@ class ConditionsTest extends PHPUnit\Framework\TestCase { 'The Da Vinci Code (Hardcover)', ]); } - - protected function _compareWithExpected($expected) { - $this->csv->auto(__DIR__ . '/../../examples/_books.csv'); - $actual = array_map(function ($row) { - return $row['title']; - }, $this->csv->data); - $this->assertEquals($expected, $actual); - } } From 5ced1f42123a725e5ac257761dbc43be1076e586 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Bl=C3=A4ul?= Date: Fri, 2 Feb 2018 14:28:51 +0100 Subject: [PATCH 5/6] Added conditions to SortByTest because otherwise different PHP versions... yielded different sort orders (that's sort of ok, as rating is ambiguous). See https://travis-ci.org/parsecsv/parsecsv-for-php/jobs/336540118 --- tests/properties/SortByTest.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/tests/properties/SortByTest.php b/tests/properties/SortByTest.php index c26d4bb..5c10d1e 100644 --- a/tests/properties/SortByTest.php +++ b/tests/properties/SortByTest.php @@ -4,6 +4,7 @@ class SortByTest extends BaseClass { public function testSortByRating() { $this->csv->sort_by = 'rating'; + $this->csv->conditions = 'title does not contain Blood'; $this->_compareWithExpected([ // Rating 0 'The Killing Kind', @@ -12,7 +13,6 @@ class SortByTest extends BaseClass { // Rating 3 'The Last Templar', 'The Broker (Paperback)', - 'Without Blood (Paperback)', // Rating 4 'Deception Point (Paperback)', @@ -29,14 +29,17 @@ class SortByTest extends BaseClass { public function testReverseSortByRating() { $this->csv->sort_by = 'rating'; + $this->csv->conditions = + 'title does not contain Prey AND ' . + 'title does not contain Fortress AND ' . + 'title does not contain Blood AND ' . + 'title does not contain Fear'; $this->csv->sort_reverse = true; $this->_compareWithExpected([ // Rating 5 - 'Digital Fortress : A Thriller (Mass Market Paperback)', - 'Prey', - 'State of Fear (Paperback)', 'Angels & Demons (Mass Market Paperback)', + 'The Traveller', // Rating 4 'The Da Vinci Code (Hardcover)', @@ -46,7 +49,6 @@ class SortByTest extends BaseClass { // Rating 3 'The Broker (Paperback)', 'The Last Templar', - 'Without Blood (Paperback)', // Rating 0 'The Third Secret', From 913dc94e2c6ea2fa3909efd11bb4908ef2fa9d90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Bl=C3=A4ul?= Date: Sun, 11 Feb 2018 09:44:43 +0100 Subject: [PATCH 6/6] Keep sorting keys compatible: Moved array_values call to PHPUnit. This commit partially reverts adcd258ea2e4d30c64f468bd1376118bcfdd92c1. --- parsecsv.lib.php | 3 --- tests/properties/BaseClass.php | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/parsecsv.lib.php b/parsecsv.lib.php index 4ea9e08..beffd1e 100644 --- a/parsecsv.lib.php +++ b/parsecsv.lib.php @@ -730,9 +730,6 @@ class parseCSV { $this->sort_reverse ? krsort($rows, $sort_type) : ksort($rows, $sort_type); - // Avoid issues with mixing string and integer keys: - $rows = array_values($rows); - if ($this->offset !== null || $this->limit !== null) { $rows = array_slice($rows, ($this->offset === null ? 0 : $this->offset), $this->limit, true); } diff --git a/tests/properties/BaseClass.php b/tests/properties/BaseClass.php index 4d9c420..1131ac8 100644 --- a/tests/properties/BaseClass.php +++ b/tests/properties/BaseClass.php @@ -26,6 +26,6 @@ class BaseClass extends PHPUnit\Framework\TestCase { $actual = array_map(function ($row) { return $row['title']; }, $this->csv->data); - $this->assertEquals($expected, $actual); + $this->assertEquals($expected, array_values($actual)); } }