Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix data loss on multiple Result#rewind() calls #273

Merged
merged 1 commit into from
Dec 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/Adapter/Driver/Pdo/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,11 @@ public function rewind()
'This result is a forward only result set, calling rewind() after moving forward is not supported'
);
}
$this->currentData = $this->resource->fetch($this->fetchMode);
$this->currentComplete = true;
$this->position = 0;
if (! $this->currentComplete) {
$this->currentData = $this->resource->fetch($this->fetchMode);
$this->currentComplete = true;
}
$this->position = 0;
}

/**
Expand Down
33 changes: 33 additions & 0 deletions test/unit/Adapter/Driver/Pdo/ResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
use Laminas\Db\Adapter\Driver\Pdo\Result;
use Laminas\Db\Adapter\Exception\InvalidArgumentException;
use PDO;
use PDOStatement;
use PHPUnit\Framework\TestCase;
use stdClass;

use function assert;
use function uniqid;

/**
Expand Down Expand Up @@ -80,4 +82,35 @@ public function testFetchModeRange()
self::assertEquals(11, $result->getFetchMode());
self::assertInstanceOf('stdClass', $result->current());
}

public function testMultipleRewind()
{
$data = [
['test' => 1],
['test' => 2],
];
$position = 0;

$stub = $this->getMockBuilder('PDOStatement')->getMock();
assert($stub instanceof PDOStatement); // to suppress IDE type warnings
$stub->expects($this->any())
->method('fetch')
->will($this->returnCallback(function () use ($data, &$position) {
return $data[$position++];
}));
$result = new Result();
$result->initialize($stub, null);

$result->rewind();
$result->rewind();

$this->assertEquals(0, $result->key());
$this->assertEquals(1, $position);
$this->assertEquals($data[0], $result->current());

$result->next();
$this->assertEquals(1, $result->key());
$this->assertEquals(2, $position);
$this->assertEquals($data[1], $result->current());
}
}
53 changes: 53 additions & 0 deletions test/unit/ResultSet/AbstractResultSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
namespace LaminasTest\Db\ResultSet;

use ArrayIterator;
use Laminas\Db\Adapter\Driver\Pdo\Result;
use Laminas\Db\Adapter\Driver\ResultInterface;
use Laminas\Db\ResultSet\AbstractResultSet;
use Laminas\Db\ResultSet\Exception\InvalidArgumentException;
use Laminas\Db\ResultSet\Exception\RuntimeException;
use PDOStatement;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

use function assert;

class AbstractResultSetTest extends TestCase
{
/** @var MockObject */
Expand Down Expand Up @@ -266,4 +270,53 @@ public function testBufferIterations()
$data = $resultSet->current();
self::assertEquals(3, $data['id']);
}

/**
* Test multiple iterations with buffer with multiple rewind() calls
*
* @group issue-6845
*/
public function testMultipleRewindBufferIterations()
{
$resultSet = $this->getMockForAbstractClass(AbstractResultSet::class);
$result = new Result();
$stub = $this->getMockBuilder(PDOStatement::class)->getMock();
$data = new ArrayIterator([
['id' => 1, 'name' => 'one'],
['id' => 2, 'name' => 'two'],
['id' => 3, 'name' => 'three'],
]);
assert($stub instanceof PDOStatement); // to suppress IDE type warnings
$stub->expects($this->any())
->method('fetch')
->will($this->returnCallback(function () use ($data) {
$r = $data->current();
$data->next();
return $r;
}));
$result->initialize($stub, null);
$result->rewind();
$result->rewind();
$resultSet->initialize($result);
$resultSet->buffer();
$resultSet->rewind();
$resultSet->rewind();

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

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