diff --git a/CHANGELOG.md b/CHANGELOG.md index a8cbecf9fe..5c44f5668c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/Adapter/Driver/Pdo/Result.php b/src/Adapter/Driver/Pdo/Result.php index ec4cdc6528..d047a37871 100644 --- a/src/Adapter/Driver/Pdo/Result.php +++ b/src/Adapter/Driver/Pdo/Result.php @@ -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 */ @@ -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.' ); diff --git a/test/unit/Adapter/Driver/Pdo/ResultTest.php b/test/unit/Adapter/Driver/Pdo/ResultTest.php index d806000dc1..405211ec58 100644 --- a/test/unit/Adapter/Driver/Pdo/ResultTest.php +++ b/test/unit/Adapter/Driver/Pdo/ResultTest.php @@ -47,7 +47,7 @@ public function testFetchModeException() $result = new Result(); $this->expectException('\Zend\Db\Adapter\Exception\InvalidArgumentException'); - $result->setFetchMode(11); + $result->setFetchMode(13); } /** @@ -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()); + } }