summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorIgnace Nyamagana Butera <nyamsprod@gmail.com>2014-04-25 10:23:22 +0200
committerIgnace Nyamagana Butera <nyamsprod@gmail.com>2014-04-25 10:23:22 +0200
commit96f7e6bb3dee085f0496bbfcf5ebb565bd9f6220 (patch)
tree77f4db70d1c2fbf2a22fe59b423908c9a1ca320c /test
parent38ae3e0023671af23f54b1e62078a955579e57ed (diff)
downloadcsv-96f7e6bb3dee085f0496bbfcf5ebb565bd9f6220.zip
csv-96f7e6bb3dee085f0496bbfcf5ebb565bd9f6220.tar.gz
csv-96f7e6bb3dee085f0496bbfcf5ebb565bd9f6220.tar.bz2
remove IteratorQuery
Diffstat (limited to 'test')
-rw-r--r--test/Iterator/IteratorQueryTest.php136
-rw-r--r--test/ReaderTest.php78
2 files changed, 77 insertions, 137 deletions
diff --git a/test/Iterator/IteratorQueryTest.php b/test/Iterator/IteratorQueryTest.php
deleted file mode 100644
index d47ae1d..0000000
--- a/test/Iterator/IteratorQueryTest.php
+++ /dev/null
@@ -1,136 +0,0 @@
-<?php
-
-namespace League\Csv\test\Iterator;
-
-use ArrayIterator;
-use ReflectionClass;
-use PHPUnit_Framework_TestCase;
-use League\Csv\Iterator\IteratorQuery;
-
-/**
- * @group iterator
- */
-class IteratorQueryTest extends PHPUnit_Framework_TestCase
-{
- private $traitQuery;
- private $iterator;
- private $data = ['john', 'jane', 'foo', 'bar'];
-
- public function invokeMethod(&$object, $methodName, array $parameters = [])
- {
- $reflection = new ReflectionClass(get_class($object));
- $method = $reflection->getMethod($methodName);
- $method->setAccessible(true);
-
- return $method->invokeArgs($object, $parameters);
- }
-
- private function createTraitObject()
- {
- return $this->getObjectForTrait('\League\Csv\Iterator\IteratorQuery');
- }
-
- public function setUp()
- {
- $this->traitQuery = $this->createTraitObject();
- $this->iterator = new ArrayIterator($this->data);
- }
-
- /**
- * @expectedException \InvalidArgumentException
- */
- public function testSetLimit()
- {
- $this->traitQuery->setLimit(1);
- $iterator = $this->invokeMethod($this->traitQuery, 'execute', [$this->iterator]);
- $res = iterator_to_array($iterator);
- $this->assertCount(1, $res);
-
- $this->traitQuery->setLimit(-4);
- }
-
- /**
- * @expectedException \InvalidArgumentException
- */
- public function testSetOffset()
- {
- $this->traitQuery->setOffset(1);
- $iterator = $this->invokeMethod($this->traitQuery, 'execute', [$this->iterator]);
- $res = iterator_to_array($iterator);
- $this->assertCount(3, $res);
-
- $this->traitQuery->setOffset('toto');
- }
-
- public function testIntervalLimitTooLong()
- {
- $this->traitQuery->setOffset(3);
- $this->traitQuery->setLimit(10);
- $iterator = $this->invokeMethod($this->traitQuery, 'execute', [$this->iterator]);
- $res = iterator_to_array($iterator);
- $this->assertSame([3 => 'bar'], $res);
- $this->assertCount(1, $res);
- }
-
- public function testInterval()
- {
- $this->traitQuery->setOffset(1);
- $this->traitQuery->setLimit(1);
- $iterator = $this->invokeMethod($this->traitQuery, 'execute', [$this->iterator]);
- $res = iterator_to_array($iterator);
- $this->assertCount(1, $res);
- }
-
- public function testFilter()
- {
- $func = function ($row) {
- return false !== strpos($row, 'o');
- };
- $this->traitQuery->setFilter($func);
-
- $iterator = $this->invokeMethod($this->traitQuery, 'execute', [$this->iterator]);
- $this->assertCount(2, iterator_to_array($iterator, false));
-
- $func2 = function ($row) {
- return false !== strpos($row, 'j');
- };
- $this->traitQuery->addFilter($func2);
- $this->traitQuery->addFilter($func);
-
- $iterator = $this->invokeMethod($this->traitQuery, 'execute', [$this->iterator]);
- $this->assertCount(1, iterator_to_array($iterator, false));
-
- $this->traitQuery->addFilter($func2);
- $this->traitQuery->addFilter($func);
- $this->assertTrue($this->traitQuery->hasFilter($func2));
- $this->traitQuery->removeFilter($func2);
- $this->assertFalse($this->traitQuery->hasFilter($func2));
-
- $iterator = $this->invokeMethod($this->traitQuery, 'execute', [$this->iterator]);
- $this->assertCount(2, iterator_to_array($iterator, false));
- }
-
- public function testSortBy()
- {
- $this->traitQuery->setSortBy('strcmp');
- $iterator = $this->invokeMethod($this->traitQuery, 'execute', [$this->iterator]);
- $res = iterator_to_array($iterator, false);
- $this->assertSame(['bar', 'foo', 'jane', 'john'], $res);
-
- $this->traitQuery->addSortBy('strcmp');
- $this->traitQuery->addSortBy('strcmp');
- $this->traitQuery->removeSortBy('strcmp');
- $this->assertTrue($this->traitQuery->hasSortBy('strcmp'));
- $iterator = $this->invokeMethod($this->traitQuery, 'execute', [$this->iterator]);
- $res = iterator_to_array($iterator, false);
- $this->assertSame(['bar', 'foo', 'jane', 'john'], $res);
- }
-
- public function testExecuteWithCallback()
- {
- $iterator = $this->invokeMethod($this->traitQuery, 'execute', [$this->iterator, function ($value) {
- return strtoupper($value);
- }]);
- $this->assertSame(array_map('strtoupper', $this->data), iterator_to_array($iterator));
- }
-}
diff --git a/test/ReaderTest.php b/test/ReaderTest.php
index 36f8f0b..f9f0d48 100644
--- a/test/ReaderTest.php
+++ b/test/ReaderTest.php
@@ -15,7 +15,7 @@ class ReaderTest extends PHPUnit_Framework_TestCase
private $expected = [
['john', 'doe', 'john.doe@example.com'],
- ['jane','doe','jane.doe@example.com'],
+ ['jane', 'doe', 'jane.doe@example.com'],
];
public function setUp()
@@ -28,6 +28,82 @@ class ReaderTest extends PHPUnit_Framework_TestCase
$this->csv = new Reader($csv);
}
+ /**
+ * @expectedException \InvalidArgumentException
+ */
+ public function testSetLimit()
+ {
+ $this->csv->setLimit(1);
+ $this->assertCount(1, $this->csv->fetchAll());
+ $this->csv->setLimit(-4);
+ }
+
+ /**
+ * @expectedException \InvalidArgumentException
+ */
+ public function testSetOffset()
+ {
+ $this->csv->setOffset(1);
+ $this->assertCount(1, $this->csv->fetchAll());
+
+ $this->csv->setOffset('toto');
+ }
+
+ public function testIntervalLimitTooLong()
+ {
+ $this->csv->setOffset(1);
+ $this->csv->setLimit(10);
+ $this->assertSame([['jane', 'doe', 'jane.doe@example.com']], $this->csv->fetchAll());
+ }
+
+ public function testInterval()
+ {
+ $this->csv->setOffset(1);
+ $this->csv->setLimit(1);
+ $this->assertCount(1, $this->csv->fetchAll());
+ }
+
+ public function testFilter()
+ {
+ $func = function ($row) {
+ return ! in_array('jane', $row);
+ };
+ $this->csv->setFilter($func);
+
+ $this->assertCount(1, $this->csv->fetchAll());
+
+ $func2 = function ($row) {
+ return ! in_array('john', $row);
+ };
+ $this->csv->addFilter($func2);
+ $this->csv->addFilter($func);
+
+ $this->assertCount(0, $this->csv->fetchAll());
+
+ $this->csv->addFilter($func2);
+ $this->csv->addFilter($func);
+ $this->assertTrue($this->csv->hasFilter($func2));
+ $this->csv->removeFilter($func2);
+ $this->assertFalse($this->csv->hasFilter($func2));
+
+ $this->assertCount(1, $this->csv->fetchAll());
+ }
+
+ public function testSortBy()
+ {
+ $func = function ($rowA, $rowB) {
+ return strcmp($rowA[0], $rowB[0]);
+ };
+ $this->csv->setSortBy($func);
+ $this->assertSame(array_reverse($this->expected), $this->csv->fetchAll());
+
+ $this->csv->addSortBy($func);
+ $this->csv->addSortBy($func);
+ $this->csv->removeSortBy($func);
+ $this->assertTrue($this->csv->hasSortBy($func));
+ $this->assertSame(array_reverse($this->expected), $this->csv->fetchAll());
+ }
+
public function testFetchAll()
{
$func = function ($value) {