forked from deblan/csv
Csv tests
This commit is contained in:
parent
8b3f576665
commit
b180f00940
18
phpunit.xml
Normal file
18
phpunit.xml
Normal 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
79
tests/CsvParserTest.php
Normal 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
79
tests/CsvTest.php
Normal 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
21
tests/bootstrap.php
Normal 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');
|
Loading…
Reference in a new issue