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 01/11] 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 02/11] 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 03/11] 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 04/11] 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 05/11] 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 06/11] 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)); } } From b753d9c694f5b31b78ed1a157310ac4a73fe7795 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Bl=C3=A4ul?= Date: Wed, 21 Feb 2018 18:45:50 +0100 Subject: [PATCH 07/11] Repaired examples: there was a require missing. --- examples/basic.php | 2 +- examples/conditions.php | 1 + examples/download.php | 1 + examples/limit.php | 1 + 4 files changed, 4 insertions(+), 1 deletion(-) diff --git a/examples/basic.php b/examples/basic.php index beec5cb..66f1abc 100644 --- a/examples/basic.php +++ b/examples/basic.php @@ -1,8 +1,8 @@
 
Date: Wed, 21 Feb 2018 18:57:38 +0100
Subject: [PATCH 08/11] Re-added the parsecsv.lib.php file for compatibility

---
 parsecsv.lib.php | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
 create mode 100644 parsecsv.lib.php

diff --git a/parsecsv.lib.php b/parsecsv.lib.php
new file mode 100644
index 0000000..bfcf0c7
--- /dev/null
+++ b/parsecsv.lib.php
@@ -0,0 +1,23 @@
+
Date: Wed, 21 Feb 2018 19:04:00 +0100
Subject: [PATCH 09/11] Added namespace support for further tests

---
 tests/properties/BaseClass.php      | 11 ++++++++---
 tests/properties/ConditionsTest.php |  2 ++
 tests/properties/SortByTest.php     |  2 ++
 3 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/tests/properties/BaseClass.php b/tests/properties/BaseClass.php
index 1131ac8..a81f5c6 100644
--- a/tests/properties/BaseClass.php
+++ b/tests/properties/BaseClass.php
@@ -1,13 +1,18 @@
 csv = new parseCSV();
+        $this->csv = new Csv();
     }
 
     protected function _compareWithExpected($expected) {
diff --git a/tests/properties/ConditionsTest.php b/tests/properties/ConditionsTest.php
index 7326209..c68b65d 100644
--- a/tests/properties/ConditionsTest.php
+++ b/tests/properties/ConditionsTest.php
@@ -1,5 +1,7 @@
 
Date: Wed, 21 Feb 2018 19:15:38 +0100
Subject: [PATCH 10/11] Make sure the code examples run without syntax or
 runtime errors.

---
 tests/methods/ConstructTest.php | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/tests/methods/ConstructTest.php b/tests/methods/ConstructTest.php
index a170310..48330ae 100644
--- a/tests/methods/ConstructTest.php
+++ b/tests/methods/ConstructTest.php
@@ -1,18 +1,18 @@
 assertTrue(is_string($this->csv->file_data));
         $this->assertEquals($csv, $this->csv->file_data);
     }
+
+    /**
+     * @runInSeparateProcess because download.php uses header()
+     *
+     * @see                  https://github.com/sebastianbergmann/phpunit/issues/720#issuecomment-10421092
+     */
+    public function testCodeExamples() {
+        foreach (glob('examples/*.php') as $script_file) {
+            /** @noinspection PhpIncludeInspection */
+            require $script_file;
+        }
+    }
 }

From 2ac9f450f187850a3a1d2151f836891804ca69ce Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Christian=20Bl=C3=A4ul?= 
Date: Wed, 21 Feb 2018 19:25:39 +0100
Subject: [PATCH 11/11] Check if output contains HTML table cells

They should if the _books.csv file is found
---
 tests/methods/ConstructTest.php | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/tests/methods/ConstructTest.php b/tests/methods/ConstructTest.php
index 48330ae..3da03a4 100644
--- a/tests/methods/ConstructTest.php
+++ b/tests/methods/ConstructTest.php
@@ -78,9 +78,14 @@ class ConstructTest extends TestCase {
      * @see                  https://github.com/sebastianbergmann/phpunit/issues/720#issuecomment-10421092
      */
     public function testCodeExamples() {
-        foreach (glob('examples/*.php') as $script_file) {
+        chdir('examples');
+        foreach (glob('*.php') as $script_file) {
+            ob_start();
             /** @noinspection PhpIncludeInspection */
             require $script_file;
+            if ($script_file != 'download.php') {
+                $this->assertContains('', ob_get_clean());
+            }
         }
     }
 }