summaryrefslogtreecommitdiffstats
path: root/test/Iterator/IteratorQueryTest.php
diff options
context:
space:
mode:
authorignace nyamagana butera <nyamsprod@gmail.com>2014-02-14 16:00:38 +0100
committerignace nyamagana butera <nyamsprod@gmail.com>2014-02-14 16:00:38 +0100
commita24d0ce6d0f9e3463d09a6cf25ba41a3487be6de (patch)
treeeff1e952d951f888f3d0cd88fe70f0ce8cb32fe9 /test/Iterator/IteratorQueryTest.php
parent837f7293ff6e50bce0e9ebbf4075d143adbb5cfc (diff)
parent941299fce9de20dce87b687854ac35351f128183 (diff)
downloadcsv-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/Iterator/IteratorQueryTest.php')
-rw-r--r--test/Iterator/IteratorQueryTest.php111
1 files changed, 111 insertions, 0 deletions
diff --git a/test/Iterator/IteratorQueryTest.php b/test/Iterator/IteratorQueryTest.php
new file mode 100644
index 0000000..45e7b6f
--- /dev/null
+++ b/test/Iterator/IteratorQueryTest.php
@@ -0,0 +1,111 @@
+<?php
+
+namespace Bakame\Csv\Iterator;
+
+use ArrayIterator;
+use ReflectionClass;
+use PHPUnit_Framework_TestCase;
+
+/**
+ * @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('\Bakame\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 $row == 'john';
+ };
+ $this->traitQuery->setFilter($func);
+
+ $iterator = $this->invokeMethod($this->traitQuery, 'execute', [$this->iterator]);
+ $res = iterator_to_array($iterator);
+ $this->assertCount(1, $res);
+ }
+
+ public function testSortBy()
+ {
+ $this->traitQuery->setSortBy('strcmp');
+ $iterator = $this->invokeMethod($this->traitQuery, 'execute', [$this->iterator]);
+ $res = iterator_to_array($iterator);
+
+ $this->assertSame(['bar', 'foo', 'jane', 'john'], array_values($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));
+ }
+}