Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Closed
wants to merge 1 commit into from

3 participants

@kpieters

Still have to find a good way to test the ResultInterface

@weierophinney

Closing and re-opening to get travis to re-run tests; I fixed the errors on the develop branch.

@weierophinney weierophinney reopened this
@ralphschindler
Collaborator

I've pulled in the unit test (only) and ran them, and they pass without any modification to the AbstractResultSet. I am failing to see what the issue is that is being tested by the unit tests. Can you elaborate?

@weierophinney

Closing due to inactivity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 7, 2012
  1. @kpieters

    ZF2-482 Attempt to fix the buffer. Also added extra unit tests. Still…

    kpieters authored
    … have to find a good way to test the ResultInterface
This page is out of date. Refresh to see the latest.
View
26 library/Zend/Db/ResultSet/AbstractResultSet.php
@@ -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++;
}
View
77 tests/ZendTest/Db/ResultSet/ResultSetTest.php
@@ -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'
+ );
+
+ }
}
Something went wrong with that request. Please try again.