Skip to content

Commit

Permalink
Fix data loss on multiple Result->rewind()
Browse files Browse the repository at this point in the history
  • Loading branch information
Grundik committed Dec 17, 2022
1 parent f382589 commit 66f1f00
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
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());
}
}

0 comments on commit 66f1f00

Please sign in to comment.