Csv tests

This commit is contained in:
Simon Vieille 2015-03-09 20:05:36 +01:00
parent 8b3f576665
commit b180f00940
4 changed files with 197 additions and 0 deletions

18
phpunit.xml Normal file
View File

@ -0,0 +1,18 @@
<phpunit
backupGlobals = "false"
backupStaticAttributes = "false"
colors = "true"
convertErrorsToExceptions = "true"
convertNoticesToExceptions = "true"
convertWarningsToExceptions = "true"
processIsolation = "false"
stopOnFailure = "false"
syntaxCheck = "false"
bootstrap = "./tests/bootstrap.php" >
<testsuites>
<testsuite name="Deblan CSV Test Suite">
<directory>tests/*</directory>
</testsuite>
</testsuites>
</phpunit>

79
tests/CsvParserTest.php Normal file
View File

@ -0,0 +1,79 @@
<?php
use Deblan\Csv\Csv;
class CsvTest extends \PHPUnit_Framework_TestCase
{
public function testAddLine()
{
$csv = new Csv();
$csv->addLine(array('foo', 'bar'));
$this->assertEquals('"foo";"bar"'."\n", $csv->compile());
}
public function testSetLegend()
{
$csv = new Csv();
$this->assertEquals(false, $csv->getHasLegend());
$csv->setLegend(array('bim', 'bam'));
$csv->addLine(array('foo', 'bar'));
$this->assertEquals(true, $csv->getHasLegend());
$this->assertEquals(
'"bim";"bam"'."\n".
'"foo";"bar"'."\n",
$csv->compile()
);
}
public function testHasDatas()
{
$csv = new Csv();
$this->assertEquals(false, $csv->hasDatas());
$csv->setLegend(array('bim', 'bam'));
$this->assertEquals(false, $csv->hasDatas());
$csv->addLine(array('foo', 'bar'));
$this->assertEquals(true, $csv->hasDatas());
$csv = new Csv();
$csv->addLine(array('foo', 'bar'));
$this->assertEquals(true, $csv->hasDatas());
}
public function testDatasToCsvLine()
{
$csv = new Csv();
$csv->addLine(array('fo\\o', 'bar'));
$this->assertEquals('"fo\\\\o";"bar"'."\n", $csv->compile());
$csv = new Csv();
$csv->setDelimiter(':');
$csv->addLine(array('foo', 'bar'));
$this->assertEquals('"foo":"bar"'."\n", $csv->compile());
$csv = new Csv();
$csv->setDelimiter(':');
$csv->addLine(array('fo:o', 'bar'));
$this->assertEquals('"fo:o":"bar"'."\n", $csv->compile());
$csv = new Csv();
$csv->setDelimiter(':');
$csv->setEnclosure('');
$csv->addLine(array('fo:o', 'bar'));
$this->assertEquals('fo\\:o:bar'."\n", $csv->compile());
$csv = new Csv();
$csv->setEnclosure('#');
$csv->addLine(array('foo', 'bar'));
$this->assertEquals('#foo#;#bar#'."\n", $csv->compile());
$csv = new Csv();
$csv->setEnclosure('#');
$csv->addLine(array('f#oo', 'bar'));
$this->assertEquals('#f\\#oo#;#bar#'."\n", $csv->compile());
}
}

79
tests/CsvTest.php Normal file
View File

@ -0,0 +1,79 @@
<?php
use Deblan\Csv\Csv;
class CsvTest extends \PHPUnit_Framework_TestCase
{
public function testAddLine()
{
$csv = new Csv();
$csv->addLine(array('foo', 'bar'));
$this->assertEquals('"foo";"bar"'."\n", $csv->compile());
}
public function testSetLegend()
{
$csv = new Csv();
$this->assertEquals(false, $csv->getHasLegend());
$csv->setLegend(array('bim', 'bam'));
$csv->addLine(array('foo', 'bar'));
$this->assertEquals(true, $csv->getHasLegend());
$this->assertEquals(
'"bim";"bam"'."\n".
'"foo";"bar"'."\n",
$csv->compile()
);
}
public function testHasDatas()
{
$csv = new Csv();
$this->assertEquals(false, $csv->hasDatas());
$csv->setLegend(array('bim', 'bam'));
$this->assertEquals(false, $csv->hasDatas());
$csv->addLine(array('foo', 'bar'));
$this->assertEquals(true, $csv->hasDatas());
$csv = new Csv();
$csv->addLine(array('foo', 'bar'));
$this->assertEquals(true, $csv->hasDatas());
}
public function testDatasToCsvLine()
{
$csv = new Csv();
$csv->addLine(array('fo\\o', 'bar'));
$this->assertEquals('"fo\\\\o";"bar"'."\n", $csv->compile());
$csv = new Csv();
$csv->setDelimiter(':');
$csv->addLine(array('foo', 'bar'));
$this->assertEquals('"foo":"bar"'."\n", $csv->compile());
$csv = new Csv();
$csv->setDelimiter(':');
$csv->addLine(array('fo:o', 'bar'));
$this->assertEquals('"fo:o":"bar"'."\n", $csv->compile());
$csv = new Csv();
$csv->setDelimiter(':');
$csv->setEnclosure('');
$csv->addLine(array('fo:o', 'bar'));
$this->assertEquals('fo\\:o:bar'."\n", $csv->compile());
$csv = new Csv();
$csv->setEnclosure('#');
$csv->addLine(array('foo', 'bar'));
$this->assertEquals('#foo#;#bar#'."\n", $csv->compile());
$csv = new Csv();
$csv->setEnclosure('#');
$csv->addLine(array('f#oo', 'bar'));
$this->assertEquals('#f\\#oo#;#bar#'."\n", $csv->compile());
}
}

21
tests/bootstrap.php Normal file
View File

@ -0,0 +1,21 @@
<?php
function autoload($className)
{
$className = ltrim($className, '\\');
$fileName = '';
$namespace = '';
if ($lastNsPos = strrpos($className, '\\')) {
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
$fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
}
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
if (file_exists('src/'.$fileName)) {
require 'src/'.$fileName;
}
}
spl_autoload_register('autoload');