summaryrefslogtreecommitdiffstats
path: root/test/CsvTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'test/CsvTest.php')
-rw-r--r--test/CsvTest.php189
1 files changed, 189 insertions, 0 deletions
diff --git a/test/CsvTest.php b/test/CsvTest.php
new file mode 100644
index 0000000..e912505
--- /dev/null
+++ b/test/CsvTest.php
@@ -0,0 +1,189 @@
+<?php
+
+namespace Bakame\Csv;
+
+use SplFileInfo;
+use SplFileObject;
+use SplTempFileObject;
+use PHPUnit_Framework_TestCase;
+
+/**
+ * @group csv
+ */
+class CsvTest extends PHPUnit_Framework_TestCase
+{
+ private $csv;
+
+ private $expected = [
+ ['john', 'doe', 'john.doe@example.com'],
+ ['jane','doe','jane.doe@example.com'],
+ ];
+
+ public function setUp()
+ {
+ $csv = new SplTempFileObject;
+ foreach ($this->expected as $row) {
+ $csv->fputcsv($row);
+ }
+
+ $this->csv = new Reader($csv);
+ }
+
+ public function testConstructorWithFileObject()
+ {
+ $path = __DIR__.'/foo.csv';
+
+ $csv = new Reader(new SplFileInfo($path));
+ $this->assertSame($path, $csv->getIterator()->getRealPath());
+ }
+
+ public function testConstructorWithFilePath()
+ {
+ $path = __DIR__.'/foo.csv';
+
+ $csv = new Reader($path);
+ $this->assertSame($path, $csv->getIterator()->getRealPath());
+ }
+
+ /**
+ * @expectedException RuntimeException
+ */
+ public function testConstructorWithNotWritablePath()
+ {
+ new Reader('/usr/bin/foo.csv');
+ }
+
+ /**
+ * @expectedException InvalidArgumentException
+ */
+ public function testConstructorWithWrongType()
+ {
+ new Reader(['/usr/bin/foo.csv']);
+ }
+
+ public function testCreateFromString()
+ {
+ $expected = "john,doe,john.doe@example.com".PHP_EOL
+ ."jane,doe,jane.doe@example.com".PHP_EOL;
+ $reader = Reader::createFromString($expected);
+ $this->assertSame($reader->fetchOne(0), ['john', 'doe', 'john.doe@example.com']);
+ $this->assertSame($reader->fetchOne(1), ['jane', 'doe', 'jane.doe@example.com']);
+ }
+
+ /**
+ * @expectedException InvalidArgumentException
+ */
+ public function testDelimeter()
+ {
+ $this->csv->setDelimiter('o');
+ $this->assertSame('o', $this->csv->getDelimiter());
+
+ $this->csv->setDelimiter('foo');
+ }
+
+ /**
+ * @expectedException InvalidArgumentException
+ */
+ public function testEscape()
+ {
+ $this->csv->setEscape('o');
+ $this->assertSame('o', $this->csv->getEscape());
+
+ $this->csv->setEscape('foo');
+ }
+
+ /**
+ * @expectedException InvalidArgumentException
+ */
+ public function testEnclosure()
+ {
+ $this->csv->setEnclosure('o');
+ $this->assertSame('o', $this->csv->getEnclosure());
+
+ $this->csv->setEnclosure('foo');
+ }
+
+ /**
+ * @expectedException InvalidArgumentException
+ */
+ public function testFailCreateFromString()
+ {
+ Reader::createFromString(new \DateTime);
+ }
+
+ /**
+ * @expectedException InvalidArgumentException
+ */
+ public function testEncoding()
+ {
+ $expected = 'iso-8859-15';
+ $this->csv->setEncoding($expected);
+ $this->assertSame(strtoupper($expected), $this->csv->getEncoding());
+
+ $this->csv->setEncoding('');
+ }
+
+ public function testToString()
+ {
+ $expected = "john,doe,john.doe@example.com".PHP_EOL
+ ."jane,doe,jane.doe@example.com".PHP_EOL;
+ $this->assertSame($expected, $this->csv->__toString());
+ }
+
+ public function testIterator()
+ {
+ foreach ($this->csv as $key => $row) {
+ $this->assertSame($this->expected[$key], $row);
+ }
+ }
+
+ /**
+ * @expectedException InvalidArgumentException
+ */
+ public function testSetFlags()
+ {
+ $this->csv->setFlags(SplFileObject::SKIP_EMPTY);
+ $this->assertSame(SplFileObject::SKIP_EMPTY, $this->csv->getFlags() & SplFileObject::SKIP_EMPTY);
+ $this->assertSame(SplFileObject::READ_CSV, $this->csv->getFlags() & SplFileObject::READ_CSV);
+
+ $this->csv->setFlags(-3);
+ }
+
+ public function testToHTML()
+ {
+ $expected = <<<EOF
+<table class="table-csv-data">
+<tr>
+<td>john</td>
+<td>doe</td>
+<td>john.doe@example.com</td>
+</tr>
+<tr>
+<td>jane</td>
+<td>doe</td>
+<td>jane.doe@example.com</td>
+</tr>
+</table>
+EOF;
+ $this->assertSame($expected, $this->csv->toHTML());
+ }
+
+ /**
+ * @param $rawCsv
+ *
+ * @dataProvider getIso8859Csv
+ */
+ public function testJsonInterface($rawCsv)
+ {
+ $this->assertSame(json_encode($this->expected), json_encode($this->csv));
+ $csv = Reader::createFromString($rawCsv);
+ $csv->setEncoding('iso-8859-15');
+ json_encode($csv);
+ $this->assertEquals(JSON_ERROR_NONE, json_last_error());
+ }
+
+ public static function getIso8859Csv()
+ {
+ return [[file_get_contents(__DIR__.'/data/prenoms.csv')]];
+ }
+}