From d679deba02f0198da16ece17a54b0445d2badca6 Mon Sep 17 00:00:00 2001 From: rquetiez Date: Thu, 13 Aug 2020 21:12:50 +0200 Subject: [PATCH] refactor CSV Parser unit tests from the old home-made framework into PHP Unit, and add new test to check that is parsed as a real null value --- test/core/CSVParserTest.php | 68 +++++++++++++++++++++++++++++++ test/testlist.inc.php | 81 ------------------------------------- 2 files changed, 68 insertions(+), 81 deletions(-) create mode 100644 test/core/CSVParserTest.php diff --git a/test/core/CSVParserTest.php b/test/core/CSVParserTest.php new file mode 100644 index 000000000..5c232b0d0 --- /dev/null +++ b/test/core/CSVParserTest.php @@ -0,0 +1,68 @@ + + ? a ? ; ? b ? ; ? c ? + a ; b ; c +??;??;?? +;; +?a"?;?b?;?c? +?a1 +a2?;?b?;?c? +?a1,a2?;?b?;?c? +?a?;?b?;?c1,",c2 +,c3? +?a?;?b?;?ouf !? + spaces trimmed out ; 1234; mac@enroe.com '; + + $aExpectedResult = array( + array('line 0, col 0', 'line 0, col 1', 'line 0, col 2'), + array('a', 'b', 'c'), + array('a', 'b', null), + array(' a ', ' b ', ' c '), + array('a', 'b', 'c'), + array('', '', ''), + array('', '', ''), + array('a"', 'b', 'c'), + array("a1\na2", 'b', 'c'), + array('a1,a2', 'b', 'c'), + array('a', 'b', "c1,\",c2\n,c3"), + array('a', 'b', 'ouf !'), + array('spaces trimmed out', '1234', 'mac@enroe.com'), + ); + + $oCSVParser = new CSVParser($sDataFile, $sSeparator, $sDelimiter); + $aData = $oCSVParser->ToArray(1, null, 0); + + foreach ($aData as $iRow => $aRow) + { + foreach ($aRow as $iCol => $cellValue) + { + $this->assertSame($aExpectedResult[$iRow][$iCol], $cellValue, "Line $iRow, Column $iCol"); + } + } + } +} + diff --git a/test/testlist.inc.php b/test/testlist.inc.php index 0a8bfed61..fd53907fe 100644 --- a/test/testlist.inc.php +++ b/test/testlist.inc.php @@ -123,87 +123,6 @@ class TestSQLQuery extends TestScenarioOnDB } } -class TestCSVParser extends TestFunction -{ - static public function GetName() {return 'Check CSV parsing';} - static public function GetDescription() {return 'Loads a set of CSV data';} - - public function DoExecute() - { - $sDataFile = '?field1?;?field2?;?field3? -?a?;?b?;?c? -a;b;c - ? a ? ; ? b ? ; ? c ? - a ; b ; c -??;??;?? -;; -?a"?;?b?;?c? -?a1 -a2?;?b?;?c? -?a1,a2?;?b?;?c? -?a?;?b?;?c1,",c2 -,c3? -?a?;?b?;?ouf !? - Espace sur la fin ; 1234; e@taloc.com '; - - self::DumpVariable($sDataFile); - - $aExpectedResult = array( - //array('field1', 'field2', 'field3'), - array('a', 'b', 'c'), - array('a', 'b', 'c'), - array(' a ', ' b ', ' c '), - array('a', 'b', 'c'), - array('', '', ''), - array('', '', ''), - array('a"', 'b', 'c'), - array("a1\na2", 'b', 'c'), - array('a1,a2', 'b', 'c'), - array('a', 'b', "c1,\",c2\n,c3"), - array('a', 'b', 'ouf !'), - array('Espace sur la fin', '1234', 'e@taloc.com'), - ); - - $oCSVParser = new CSVParser($sDataFile, ';', '?'); - $aData = $oCSVParser->ToArray(1, null, 0); - - $iIssues = 0; - - echo "\n"; - foreach ($aData as $iRow => $aRow) - { - echo "\n"; - foreach ($aRow as $iCol => $sCell) - { - if (empty($sCell)) - { - $sCellValue = ' '; - } - else - { - $sCellValue = htmlentities($sCell, ENT_QUOTES, 'UTF-8'); - } - - if (!isset($aExpectedResult[$iRow][$iCol])) - { - $iIssues++; - $sCellValue = "$sCellValue"; - } - elseif ($aExpectedResult[$iRow][$iCol] != $sCell) - { - $iIssues++; - $sCellValue = "$sCellValue, expecting '".$aExpectedResult[$iRow][$iCol]."'"; - } - - echo ""; - } - echo "\n"; - } - echo "
$sCellValue
\n"; - return ($iIssues > 0); - } -} - class TestGenericItoMyModel extends TestBizModelGeneric { static public function GetName()