From b180f00940d5180025fef35a84222ed883fcc092 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Mon, 9 Mar 2015 20:05:36 +0100 Subject: [PATCH] Csv tests --- phpunit.xml | 18 ++++++++++ tests/CsvParserTest.php | 79 +++++++++++++++++++++++++++++++++++++++++ tests/CsvTest.php | 79 +++++++++++++++++++++++++++++++++++++++++ tests/bootstrap.php | 21 +++++++++++ 4 files changed, 197 insertions(+) create mode 100644 phpunit.xml create mode 100644 tests/CsvParserTest.php create mode 100644 tests/CsvTest.php create mode 100644 tests/bootstrap.php diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..e41ae99 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,18 @@ + + + + + tests/* + + + diff --git a/tests/CsvParserTest.php b/tests/CsvParserTest.php new file mode 100644 index 0000000..e3ad382 --- /dev/null +++ b/tests/CsvParserTest.php @@ -0,0 +1,79 @@ +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()); + } +} diff --git a/tests/CsvTest.php b/tests/CsvTest.php new file mode 100644 index 0000000..e3ad382 --- /dev/null +++ b/tests/CsvTest.php @@ -0,0 +1,79 @@ +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()); + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..472d676 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,21 @@ +