diff options
author | ignace nyamagana butera <nyamsprod@gmail.com> | 2014-02-14 16:00:38 +0100 |
---|---|---|
committer | ignace nyamagana butera <nyamsprod@gmail.com> | 2014-02-14 16:00:38 +0100 |
commit | a24d0ce6d0f9e3463d09a6cf25ba41a3487be6de (patch) | |
tree | eff1e952d951f888f3d0cd88fe70f0ce8cb32fe9 /test/ReaderTest.php | |
parent | 837f7293ff6e50bce0e9ebbf4075d143adbb5cfc (diff) | |
parent | 941299fce9de20dce87b687854ac35351f128183 (diff) | |
download | csv-a24d0ce6d0f9e3463d09a6cf25ba41a3487be6de.zip csv-a24d0ce6d0f9e3463d09a6cf25ba41a3487be6de.tar.gz csv-a24d0ce6d0f9e3463d09a6cf25ba41a3487be6de.tar.bz2 |
Merge pull request #9 from nyamsprod/psr44.1.2
Moving the library from PSR-0 to PSR-4
Diffstat (limited to 'test/ReaderTest.php')
-rw-r--r-- | test/ReaderTest.php | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/test/ReaderTest.php b/test/ReaderTest.php new file mode 100644 index 0000000..25d0241 --- /dev/null +++ b/test/ReaderTest.php @@ -0,0 +1,164 @@ +<?php + +namespace Bakame\Csv; + +use PHPUnit_Framework_TestCase; +use SplTempFileObject; + +/** + * @group reader + */ +class ReaderTest 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 testFetchAll() + { + $func = function ($value) { + return array_map('strtoupper', $value); + }; + + $this->assertSame($this->expected, $this->csv->fetchAll()); + $this->assertSame(array_map($func, $this->expected), $this->csv->fetchAll($func)); + } + + public function testFetchAssoc() + { + $keys = ['firstname', 'lastname', 'email']; + $res = $this->csv->fetchAssoc($keys); + foreach ($res as $index => $row) { + $this->assertSame($keys, array_keys($row)); + $this->assertSame($this->expected[$index], array_values($row)); + } + } + + public function testFetchAssocCallback() + { + $keys = ['firstname', 'lastname', 'email']; + $func = function ($value) { + return array_map('strtoupper', $value); + }; + $res = $this->csv->fetchAssoc($keys, $func); + foreach ($res as $row) { + $this->assertSame($keys, array_keys($row)); + } + } + + public function testFetchAssocLessKeys() + { + $keys = ['firstname']; + $res = $this->csv->fetchAssoc($keys); + $this->assertSame([['firstname' => 'john'], ['firstname' => 'jane']], $res); + } + + public function testFetchAssocMoreKeys() + { + $keys = ['firstname', 'lastname', 'email', 'age']; + $res = $this->csv->fetchAssoc($keys); + + foreach ($res as $row) { + $this->assertCount(4, array_values($row)); + $this->assertNull($row['age']); + } + } + + /** + * @expectedException InvalidArgumentException + */ + public function testFetchAssocKeyFailure() + { + $this->csv->fetchAssoc([['firstname', 'lastname', 'email', 'age']]); + } + + public function testFetchCol() + { + $this->assertSame(['john', 'jane'], $this->csv->fetchCol(0)); + } + + public function testFetchColEmptyCol() + { + $raw = [ + ['john', 'doe'], + ['lara', 'croft', 'lara.croft@example.com'] + ]; + + $file = new SplTempFileObject; + foreach ($raw as $row) { + $file->fputcsv($row); + } + $csv = new Reader($file); + $res = $csv->fetchCol(2); + $this->assertInternalType('array', $res); + $this->assertCount(2, $res); + $this->assertNull($res[0][2]); + } + + public function testFetchColCallback() + { + $func = function ($value) { + return array_map('strtoupper', $value); + }; + + $this->assertSame(['JOHN', 'JANE'], $this->csv->fetchCol(0, $func)); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testFetchColFailure() + { + $this->csv->fetchCol('toto'); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testfetchOne() + { + $this->assertSame($this->expected[0], $this->csv->fetchOne(0)); + $this->assertSame($this->expected[1], $this->csv->fetchOne(1)); + $this->assertSame([], $this->csv->fetchOne(35)); + $this->csv->fetchOne(-5); + } + + public function testGetWriter() + { + $writer = $this->csv->getWriter(); + $writer->insertOne(['toto', 'le', 'herisson']); + $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> +<tr> +<td>toto</td> +<td>le</td> +<td>herisson</td> +</tr> +</table> +EOF; + $this->assertSame($expected, $writer->toHTML()); + } +} |