refactor CSV Parser unit tests from the old home-made framework into PHP Unit, and add new test to check that <NULL> is parsed as a real null value

This commit is contained in:
rquetiez
2020-08-13 21:12:50 +02:00
parent 91c6916d86
commit d679deba02
2 changed files with 68 additions and 81 deletions

View File

@@ -0,0 +1,68 @@
<?php
namespace Combodo\iTop\Test\UnitTest\Core;
use Combodo\iTop\Test\UnitTest\ItopTestCase;
use CSVParser;
class CSVParserTest extends ItopTestCase
{
protected function setUp()
{
parent::setUp();
require_once(APPROOT.'core/csvparser.class.inc.php');
require_once(APPROOT.'core/coreexception.class.inc.php');
}
public function testFile()
{
$sSeparator = ';';
$sDelimiter = '?';
$sDataFile = '?field1?;?field2?;?field3?
?line 0, col 0?;?line 0, col 1?;?line 0, col 2?
a;b;c
a;b;<NULL>
? 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");
}
}
}
}

View File

@@ -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 "<table border=\"1\">\n";
foreach ($aData as $iRow => $aRow)
{
echo "<tr>\n";
foreach ($aRow as $iCol => $sCell)
{
if (empty($sCell))
{
$sCellValue = '&nbsp;';
}
else
{
$sCellValue = htmlentities($sCell, ENT_QUOTES, 'UTF-8');
}
if (!isset($aExpectedResult[$iRow][$iCol]))
{
$iIssues++;
$sCellValue = "<span style =\"color: red; background-color: grey;\">$sCellValue</span>";
}
elseif ($aExpectedResult[$iRow][$iCol] != $sCell)
{
$iIssues++;
$sCellValue = "<span style =\"color: red; background-color: lightgrey;\">$sCellValue</span>, expecting '<span style =\"color: green; background-color: lightgrey;\">".$aExpectedResult[$iRow][$iCol]."</span>'";
}
echo "<td><pre>$sCellValue</pre></td>";
}
echo "</tr>\n";
}
echo "</table>\n";
return ($iIssues > 0);
}
}
class TestGenericItoMyModel extends TestBizModelGeneric
{
static public function GetName()