Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Broader PDO fetch-style range #296

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ All notable changes to this project will be documented in this file, in reverse

### Changed

- Nothing.
- [#296](https://github.com/zendframework/zend-db/pull/296) Broader PDO fetch style check.

### Deprecated

Expand Down
24 changes: 23 additions & 1 deletion src/Adapter/Driver/Pdo/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,28 @@ class Result implements Iterator, ResultInterface
*/
protected $fetchMode = \PDO::FETCH_ASSOC;

/**
* @var array
*/
const VALID_FETCH_MODES = [
\PDO::FETCH_LAZY, // 1
\PDO::FETCH_ASSOC, // 2
\PDO::FETCH_NUM, // 3
\PDO::FETCH_BOTH, // 4
\PDO::FETCH_OBJ, // 5
\PDO::FETCH_BOUND, // 6
// \PDO::FETCH_COLUMN, // 7
\PDO::FETCH_CLASS, // 8
\PDO::FETCH_INTO, // 9
\PDO::FETCH_FUNC, // 10
\PDO::FETCH_NAMED, // 11
\PDO::FETCH_KEY_PAIR, // 12
\PDO::FETCH_PROPS_LATE, // Extra option for \PDO::FETCH_CLASS
// \PDO::FETCH_SERIALIZE,// Seems to have been removed
// \PDO::FETCH_UNIQUE, // Option for fetchAll
\PDO::FETCH_CLASSTYPE, // Extra option for \PDO::FETCH_CLASS
];

/**
* @var PDOStatement
*/
Expand Down Expand Up @@ -107,7 +129,7 @@ public function isBuffered()
*/
public function setFetchMode($fetchMode)
{
if ($fetchMode < 1 || $fetchMode > 10) {
if (! in_array($fetchMode, self::VALID_FETCH_MODES, true)) {
throw new Exception\InvalidArgumentException(
'The fetch mode must be one of the PDO::FETCH_* constants.'
);
Expand Down
22 changes: 21 additions & 1 deletion test/unit/Adapter/Driver/Pdo/ResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function testFetchModeException()
$result = new Result();

$this->expectException('\Zend\Db\Adapter\Exception\InvalidArgumentException');
$result->setFetchMode(11);
$result->setFetchMode(13);
}

/**
Expand All @@ -69,4 +69,24 @@ public function testFetchModeAnonymousObject()
self::assertEquals(5, $result->getFetchMode());
self::assertInstanceOf('stdClass', $result->current());
}

/**
* Tests whether the fetch mode has a broader range
*/
public function testFetchModeRange()
{
$stub = $this->getMockBuilder('PDOStatement')->getMock();
$stub->expects($this->any())
->method('fetch')
->will($this->returnCallback(function () {
return new stdClass;
}));

$result = new Result();
$result->initialize($stub, null);
$result->setFetchMode(\PDO::FETCH_NAMED);

self::assertEquals(11, $result->getFetchMode());
self::assertInstanceOf('stdClass', $result->current());
}
}