Skip to content

Commit

Permalink
Fixed regression: PDO::FETCH_COLUMN was declared an unsupported fetch…
Browse files Browse the repository at this point in the history
… type (closes laminas#189)
  • Loading branch information
fabiang committed Sep 23, 2021
1 parent cdabb4b commit ebd0d3e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Adapter/Driver/Pdo/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Result implements Iterator, ResultInterface
PDO::FETCH_BOTH, // 4
PDO::FETCH_OBJ, // 5
PDO::FETCH_BOUND, // 6
// \PDO::FETCH_COLUMN, // 7 (not a valid fetch mode)
PDO::FETCH_COLUMN, // 7 (not a valid fetch mode)
PDO::FETCH_CLASS, // 8
PDO::FETCH_INTO, // 9
PDO::FETCH_FUNC, // 10
Expand Down
23 changes: 23 additions & 0 deletions test/integration/Adapter/Driver/Pdo/Mysql/TableGatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Laminas\Db\TableGateway\Feature\MetadataFeature;
use Laminas\Db\TableGateway\TableGateway;
use PHPUnit\Framework\TestCase;
use Laminas\Db\Sql\Select;

use function count;

Expand Down Expand Up @@ -38,6 +39,28 @@ public function testSelect()
}
}

/**
* @covers \Laminas\Db\TableGateway\TableGateway::select
*/
public function testSelectFetchColumn()
{
$tableGateway = new TableGateway('test', $this->adapter);

$select = new Select();
$select->from('test');
$select->columns([
'myid' => 'id'
]);
$select->limit(1);

$statement = $tableGateway->getSql()
->prepareStatementForSqlObject($select);

$result = $statement->execute();
$result->setFetchMode(\PDO::FETCH_COLUMN);
$this->assertSame('1', $result->next());
}

/**
* @covers \Laminas\Db\TableGateway\TableGateway::insert
* @covers \Laminas\Db\TableGateway\TableGateway::select
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Laminas\Db\TableGateway\Feature\SequenceFeature;
use Laminas\Db\TableGateway\TableGateway;
use PHPUnit\Framework\TestCase;
use Laminas\Db\Sql\Select;

class TableGatewayTest extends TestCase
{
Expand All @@ -26,4 +27,26 @@ public function testLastInsertValue()
$tableGateway->insert(['foo' => 'baz']);
self::assertSame(2, $tableGateway->getLastInsertValue());
}

/**
* @covers \Laminas\Db\TableGateway\TableGateway::select
*/
public function testSelectFetchColumn()
{
$tableGateway = new TableGateway('test', $this->adapter);

$select = new Select();
$select->from('test');
$select->columns([
'myid' => 'id'
]);
$select->limit(1);

$statement = $tableGateway->getSql()
->prepareStatementForSqlObject($select);

$result = $statement->execute();
$result->setFetchMode(\PDO::FETCH_COLUMN);
$this->assertSame(1, $result->next());
}
}
15 changes: 15 additions & 0 deletions test/unit/Adapter/Driver/Pdo/ResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,19 @@ public function testFetchModeRange()
self::assertEquals(11, $result->getFetchMode());
self::assertInstanceOf('stdClass', $result->current());
}

public function testFetchModeColumn()
{
$stub = $this->getMockBuilder('PDOStatement')->getMock();
$stub->expects($this->any())
->method('fetch')
->will($this->returnCallback(function () {
return 'column_value';
}));
$result = new Result();
$result->initialize($stub, null);
$result->setFetchMode(PDO::FETCH_COLUMN);
self::assertEquals(7, $result->getFetchMode());
self::assertSame('column_value', $result->current());
}
}

0 comments on commit ebd0d3e

Please sign in to comment.