summaryrefslogtreecommitdiffstats
path: root/test/ReaderTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'test/ReaderTest.php')
-rw-r--r--test/ReaderTest.php175
1 files changed, 132 insertions, 43 deletions
diff --git a/test/ReaderTest.php b/test/ReaderTest.php
index d650c51..4535dce 100644
--- a/test/ReaderTest.php
+++ b/test/ReaderTest.php
@@ -1,14 +1,16 @@
<?php
-namespace League\Csv\Test;
+namespace League\Csv\test;
use League\Csv\Reader;
+use League\Csv\Writer;
+use PHPUnit_Framework_TestCase;
use SplTempFileObject;
/**
* @group reader
*/
-class ReaderTest extends AbstractTestCase
+class ReaderTest extends PHPUnit_Framework_TestCase
{
private $csv;
@@ -151,10 +153,21 @@ class ReaderTest extends AbstractTestCase
$this->assertContains(['JANE', 'DOE', 'JANE.DOE@EXAMPLE.COM'], $res);
}
- public function testFetchAssoc()
+ public function testFetchAssocReturnsArray()
{
$keys = ['firstname', 'lastname', 'email'];
$res = $this->csv->fetchAssoc($keys);
+ $this->assertInternalType('array', $res);
+ foreach ($res as $offset => $row) {
+ $this->assertSame($keys, array_keys($row));
+ }
+ }
+
+ public function testFetchAssocReturnsIterator()
+ {
+ $keys = ['firstname', 'lastname', 'email'];
+ $res = $this->csv->setFetchMode(Reader::FETCH_ITERATOR)->fetchAssoc($keys);
+ $this->assertInstanceof('\Iterator', $res);
foreach ($res as $offset => $row) {
$this->assertSame($keys, array_keys($row));
}
@@ -216,15 +229,6 @@ class ReaderTest extends AbstractTestCase
}
/**
- * @expectedException \InvalidArgumentException
- */
- public function testFetchAssocThrowsExceptionWithNonUniqueAssocKeys()
- {
- $keys = ['firstname', 'lastname', 'firstname'];
- $this->csv->fetchAssoc($keys);
- }
-
- /**
* @param $expected
* @dataProvider validBOMSequences
*/
@@ -364,21 +368,19 @@ class ReaderTest extends AbstractTestCase
];
}
- public function testFetchCol()
+ public function testFetchColumn()
{
- $this->csv->addFilter(function ($row) {
- return $row != [null];
-
- });
- $this->assertSame(['john', 'jane'], $this->csv->fetchColumn(0));
- $this->csv->addFilter(function ($row) {
- return $row != [null];
+ $this->assertContains('john', $this->csv->fetchColumn(0));
+ $this->assertContains('jane', $this->csv->fetchColumn());
+ }
- });
- $this->assertSame(['john', 'jane'], $this->csv->fetchColumn());
+ public function testFetchColumnReturnsIterator()
+ {
+ $this->assertContains('john', $this->csv->setFetchMode(Reader::FETCH_ITERATOR)->fetchColumn(0));
+ $this->assertContains('jane', $this->csv->setFetchMode(Reader::FETCH_ITERATOR)->fetchColumn());
}
- public function testFetchColInconsistentColumnCSV()
+ public function testFetchColumnInconsistentColumnCSV()
{
$raw = [
['john', 'doe'],
@@ -390,16 +392,11 @@ class ReaderTest extends AbstractTestCase
$file->fputcsv($row);
}
$csv = Reader::createFromFileObject($file);
- $this->csv->addFilter(function ($row) {
- return $row != [null];
-
- });
$res = $csv->fetchColumn(2);
- $this->assertInternalType('array', $res);
$this->assertCount(1, $res);
}
- public function testFetchColEmptyCol()
+ public function testFetchColumnEmptyCol()
{
$raw = [
['john', 'doe'],
@@ -412,31 +409,23 @@ class ReaderTest extends AbstractTestCase
}
$csv = Reader::createFromFileObject($file);
$res = $csv->fetchColumn(2);
- $this->csv->addFilter(function ($row) {
- return $row != [null];
-
- });
- $this->assertInternalType('array', $res);
$this->assertCount(0, $res);
}
- public function testFetchColCallback()
+ public function testFetchColumnCallback()
{
$func = function ($value) {
- return array_map('strtoupper', $value);
+ return strtoupper($value);
};
- $this->csv->addFilter(function ($row) {
- return $row != [null];
-
- });
- $this->assertSame(['JOHN', 'JANE'], $this->csv->fetchColumn(0, $func));
+ $iterator = $this->csv->fetchColumn(0, $func);
+ $this->assertSame(['JOHN', 'JANE'], $iterator);
}
/**
* @expectedException \InvalidArgumentException
*/
- public function testFetchColFailure()
+ public function testFetchColumnFailure()
{
$this->csv->fetchColumn('toto');
}
@@ -482,6 +471,106 @@ class ReaderTest extends AbstractTestCase
public function testGetWriter()
{
- $this->assertInstanceOf('\League\Csv\Writer', $this->csv->newWriter());
+ $this->assertInstanceOf(Writer::class, $this->csv->newWriter());
+ }
+
+ /**
+ * @dataProvider fetchPairsDataProvider
+ */
+ public function testFetchPairsIteratorMode($key, $value, $callable, $expected)
+ {
+ $iterator = $this->csv->setFetchMode(Reader::FETCH_ITERATOR)->fetchPairs($key, $value, $callable);
+ foreach ($iterator as $key => $value) {
+ $res = current($expected);
+ $this->assertSame($value, $res[$key]);
+ next($expected);
+ }
+ }
+
+ public function fetchPairsDataProvider()
+ {
+ return [
+ 'default values' => [
+ 'key' => 0,
+ 'value' => 1,
+ 'callable' => null,
+ 'expected' => [
+ ['john' => 'doe'],
+ ['jane' => 'doe'],
+ ],
+ ],
+ 'changed key order' => [
+ 'key' => 1,
+ 'value' => 0,
+ 'callable' => null,
+ 'expected' => [
+ ['doe' => 'john'],
+ ['doe' => 'jane'],
+ ],
+ ],
+ 'with callback' => [
+ 'key' => 0,
+ 'value' => 1,
+ 'callable' => function ($row) {
+ return [
+ strtoupper($row[0]),
+ strtoupper($row[1]),
+ ];
+ },
+ 'expected' => [
+ ['JOHN' => 'DOE'],
+ ['JANE' => 'DOE'],
+ ],
+ ],
+ ];
+ }
+
+ /**
+ * @dataProvider fetchPairsArrayDataProvider
+ */
+ public function testFetchPairsArrayMode($key, $value, $callable, $expected)
+ {
+ $array = $this->csv->fetchPairs($key, $value, $callable);
+ $this->assertSame($expected, $array);
+ }
+
+ public function fetchPairsArrayDataProvider()
+ {
+ return [
+ 'default values' => [
+ 'key' => 0,
+ 'value' => 1,
+ 'callable' => null,
+ 'expected' => ['john' => 'doe', 'jane' => 'doe'],
+ ],
+ 'changed key order' => [
+ 'key' => 1,
+ 'value' => 0,
+ 'callable' => null,
+ 'expected' => ['doe' => 'jane'],
+ ],
+ 'with callback' => [
+ 'key' => 0,
+ 'value' => 1,
+ 'callable' => function ($row) {
+ return [
+ strtoupper($row[0]),
+ strtoupper($row[1]),
+ ];
+ },
+ 'expected' => ['JOHN' => 'DOE', 'JANE' => 'DOE'],
+ ],
+ ];
+ }
+
+ /**
+ * @expectedException \InvalidArgumentException
+ */
+ public function testFetchMode()
+ {
+ $this->assertSame(Reader::FETCH_ARRAY, $this->csv->getFetchMode());
+ $this->csv->setFetchMode(Reader::FETCH_ITERATOR);
+ $this->assertSame(Reader::FETCH_ITERATOR, $this->csv->getFetchMode());
+ $this->csv->setFetchMode('toto');
}
}