Skip to content

Commit

Permalink
Merge branch 'hotfix/zendframework#6847-zendframework#6845-fix-multip…
Browse files Browse the repository at this point in the history
…le-iteration-over-buffered-resultset'

Close zendframework#6847
Close zendframework#6845
  • Loading branch information
Ocramius committed Jan 3, 2015
2 parents 6bf0b23 + 814edde commit 38782ac
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
4 changes: 3 additions & 1 deletion library/Zend/Db/ResultSet/AbstractResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ public function next()
if ($this->buffer === null) {
$this->buffer = -2; // implicitly disable buffering from here on
}
$this->dataSource->next();
if (!is_array($this->buffer) || $this->position == $this->dataSource->key()) {
$this->dataSource->next();
}
$this->position++;
}

Expand Down
31 changes: 31 additions & 0 deletions tests/ZendTest/Db/ResultSet/AbstractResultSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,35 @@ public function testToArray()
$resultSet->toArray()
);
}

/**
* Test multiple iterations with buffer
* @group issue-6845
*/
public function testBufferIterations()
{
$resultSet = $this->getMockForAbstractClass('Zend\Db\ResultSet\AbstractResultSet');
$resultSet->initialize(new \ArrayIterator(array(
array('id' => 1, 'name' => 'one'),
array('id' => 2, 'name' => 'two'),
array('id' => 3, 'name' => 'three'),
)));
$resultSet->buffer();

$data = $resultSet->current();
$this->assertEquals(1, $data['id']);
$resultSet->next();
$data = $resultSet->current();
$this->assertEquals(2, $data['id']);

$resultSet->rewind();
$data = $resultSet->current();
$this->assertEquals(1, $data['id']);
$resultSet->next();
$data = $resultSet->current();
$this->assertEquals(2, $data['id']);
$resultSet->next();
$data = $resultSet->current();
$this->assertEquals(3, $data['id']);
}
}

0 comments on commit 38782ac

Please sign in to comment.