ZF2-482 Attempt to fix the buffer. Also added extra unit tests. #2380

Closed
wants to merge 1 commit into
from
@@ -50,9 +50,18 @@
*/
protected $fieldCount = null;
- protected $position = null;
+ /**
+ * @var int
+ */
+ protected $position = 0;
/**
+ * original datasource is an iterator
+ *
+ * @var bool
+ */
+ protected $iterator = false;
+ /**
* Set the data source for the result set
*
* @param Iterator|IteratorAggregate|ResultInterface $dataSource
@@ -81,7 +90,9 @@ public function initialize($dataSource)
$this->buffer = -1; // array's are a natural buffer
} elseif ($dataSource instanceof IteratorAggregate) {
$this->dataSource = $dataSource->getIterator();
+ $this->iterator = true;
} elseif ($dataSource instanceof Iterator) {
+ $this->iterator = true;
$this->dataSource = $dataSource;
} else {
throw new Exception\InvalidArgumentException('DataSource provided is not an array, nor does it implement Iterator or IteratorAggregate');
@@ -164,7 +175,18 @@ public function next()
if ($this->buffer === null) {
$this->buffer = -2; // implicitly disable buffering from here on
}
- $this->dataSource->next();
+
+ // next can also be called without showing the current item, go to the next value
+ if (is_array($this->buffer) && !isset($this->buffer[$this->position])) {
+ $data = $this->dataSource->current();
+ $this->buffer[$this->position] = $data;
+ }
+
+ // if the buffer is not used or the original datasource is an iterator , go to the next datasource
+ if (!isset($this->buffer[$this->position]) || $this->iterator == true) {
+ $this->dataSource->next();
+ }
+
$this->position++;
}
@@ -214,4 +214,81 @@ public function testCallingBufferAfterIterationThrowsException()
$this->resultSet->buffer();
}
+ /**
+ * @group simpleArray
+ */
+ public function testDataSourceSimpleArray()
+ {
+ $dataSource = array(0 => 'zero', 1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five', 6 => 'six');
+ $this->resultSet->initialize($dataSource);
+
+ $this->resultSet->next();
+ $this->resultSet->next();
+
+ // 2 times next
+ $this->assertSame(2, $this->resultSet->key());
+ $this->assertSame('two', $this->resultSet->current());
+
+ $this->resultSet->next();
+ $this->resultSet->next();
+ // 2 times next again
+ $this->assertSame(4, $this->resultSet->key());
+ $this->assertSame('four', $this->resultSet->current());
+
+ $this->resultSet->next();
+ $this->resultSet->rewind();
+ // resultset rewinded, so key should be 0 again
+ $this->assertSame(0, $this->resultSet->key());
+ $this->assertSame('zero', $this->resultSet->current());
+
+ $this->resultSet->next();
+ /// 1 times next so the key should be 1
+ $this->assertSame(1, $this->resultSet->key());
+ $this->assertSame('one', $this->resultSet->current());
+ }
+
+ /**
+ * @group arrayObject
+ */
+ public function testDataSourceArrayObject()
+ {
+ $dataSource = $this->getArrayDataSource(10);
+ $this->resultSet->initialize($dataSource);
+
+ $this->resultSet->next();
+ $this->resultSet->next();
+
+ // 2 times next
+ $this->assertSame(2, $this->resultSet->key());
+ $this->assertInstanceOf('ArrayObject', $this->resultSet->current());
+ $this->assertSame('title 2', $this->resultSet->current()->title);
+
+ $this->resultSet->next();
+ $this->resultSet->next();
+ // 2 times next again
+ $this->assertSame(4, $this->resultSet->key());
+ $this->assertSame('title 4', $this->resultSet->current()->title);
+
+ $this->resultSet->next();
+ $this->resultSet->rewind();
+ // resultset rewinded, so key should be 0 again
+ $this->assertSame(0, $this->resultSet->key());
+ $this->assertSame('title 0', $this->resultSet->current()->title);
+
+ $this->resultSet->next();
+ /// 1 times next so the key should be 1
+ $this->assertSame(1, $this->resultSet->key());
+ $this->assertSame('title 1', $this->resultSet->current()->title);
+ }
+
+ /**
+ * @group ResultInterfacetest
+ */
+ public function testDataSourceResultInterface()
+ {
+ $this->markTestIncomplete(
+ 'Still have to create a good test for ResultInterface'
+ );
+
+ }
}