Skip to content

Commit

Permalink
ZF2-482 Attempt to fix the buffer. Also added extra unit tests. Still…
Browse files Browse the repository at this point in the history
… have to find a good way to test the ResultInterface
  • Loading branch information
Koen Pieters committed Sep 7, 2012
1 parent 3a4cee6 commit b1b5183
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 2 deletions.
26 changes: 24 additions & 2 deletions library/Zend/Db/ResultSet/AbstractResultSet.php
Expand Up @@ -50,8 +50,17 @@ abstract class AbstractResultSet implements Iterator, ResultSetInterface
*/
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
*
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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++;
}

Expand Down
77 changes: 77 additions & 0 deletions tests/ZendTest/Db/ResultSet/ResultSetTest.php
Expand Up @@ -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'
);

}
}

0 comments on commit b1b5183

Please sign in to comment.