Skip to content

Commit

Permalink
Merge pull request #4173 from BenMorel/2.10.x-fix-third-parameter
Browse files Browse the repository at this point in the history
Fix Third parameter not allowed for PDO::FETCH_COLUMN
  • Loading branch information
morozov authored Sep 25, 2020
2 parents 2d09772 + 35c778b commit 787c141
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/Doctrine/DBAL/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,15 @@ public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEX
*/
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
{
return $this->stmt->fetchAll($fetchMode, $fetchArgument, $ctorArgs);
if ($ctorArgs !== null) {
return $this->stmt->fetchAll($fetchMode, $fetchArgument, $ctorArgs);
}

if ($fetchArgument !== null) {
return $this->stmt->fetchAll($fetchMode, $fetchArgument);
}

return $this->stmt->fetchAll($fetchMode);
}

/**
Expand Down
70 changes: 70 additions & 0 deletions tests/Doctrine/Tests/DBAL/Functional/ExternalPDOInstanceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Doctrine\Tests\DBAL\Functional;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\PDO\SQLite\Driver as PDOSqliteDriver;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\Schema\Table;
use Doctrine\Tests\DbalTestCase;
use Doctrine\Tests\TestUtil;
use PDO;

class ExternalPDOInstanceTest extends DbalTestCase
{
/** @var Connection */
protected $connection;

protected function setUp(): void
{
if (! TestUtil::getConnection()->getDriver() instanceof PDOSqliteDriver) {
$this->markTestSkipped('External PDO instance tests are only run on PDO SQLite for now');
}

$pdo = new PDO('sqlite::memory:');

$this->connection = new Connection(['pdo' => $pdo], new PDOSqliteDriver());

$table = new Table('stmt_fetch_all');
$table->addColumn('a', 'integer');
$table->addColumn('b', 'integer');

$this->connection->getSchemaManager()->createTable($table);

$this->connection->insert('stmt_fetch_all', [
'a' => 1,
'b' => 2,
]);
}

public function testFetchAllWithOneArgument(): void
{
$stmt = $this->connection->prepare('SELECT a, b FROM stmt_fetch_all');
$stmt->execute();

self::assertEquals([[1, 2]], $stmt->fetchAll(FetchMode::NUMERIC));
}

public function testFetchAllWithTwoArguments(): void
{
$stmt = $this->connection->prepare('SELECT a, b FROM stmt_fetch_all');
$stmt->execute();

self::assertEquals([2], $stmt->fetchAll(FetchMode::COLUMN, 1));
}

public function testFetchAllWithThreeArguments(): void
{
$stmt = $this->connection->prepare('SELECT a, b FROM stmt_fetch_all');
$stmt->execute();

[$obj] = $stmt->fetchAll(FetchMode::CUSTOM_OBJECT, StatementTestModel::class, ['foo', 'bar']);

$this->assertInstanceOf(StatementTestModel::class, $obj);

self::assertEquals(1, $obj->a);
self::assertEquals(2, $obj->b);
self::assertEquals('foo', $obj->x);
self::assertEquals('bar', $obj->y);
}
}
24 changes: 24 additions & 0 deletions tests/Doctrine/Tests/DBAL/Functional/StatementTestModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Doctrine\Tests\DBAL\Functional;

class StatementTestModel
{
public function __construct(string $x, string $y)
{
$this->x = $x;
$this->y = $y;
}

/** @var int */
public $a;

/** @var int */
public $b;

/** @var string */
public $x;

/** @var string */
public $y;
}

0 comments on commit 787c141

Please sign in to comment.