Skip to content

Commit

Permalink
- Ensure return in QueryBuilder::execute()
Browse files Browse the repository at this point in the history
  • Loading branch information
mdumoulin committed Apr 14, 2021
1 parent 05ef0a7 commit 1a5487b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 8 deletions.
6 changes: 1 addition & 5 deletions lib/Doctrine/DBAL/Connection.php
Expand Up @@ -1370,11 +1370,7 @@ public function executeCacheQuery($sql, $params, $types, QueryCacheProfile $qcp)
*/
private function ensureForwardCompatibilityStatement(ResultStatement $stmt)
{
if ($stmt instanceof ForwardCompatibility\Result) {
return $stmt;
}

return new ForwardCompatibility\Result($stmt);
return ForwardCompatibility\Result::ensure($stmt);
}

/**
Expand Down
9 changes: 9 additions & 0 deletions lib/Doctrine/DBAL/ForwardCompatibility/Result.php
Expand Up @@ -23,6 +23,15 @@ class Result implements IteratorAggregate, DriverStatement, DriverResultStatemen
/** @var Driver\ResultStatement */
private $stmt;

public static function ensure(Driver\ResultStatement $stmt): Result
{
if ($stmt instanceof Result) {
return $stmt;
}

return new Result($stmt);
}

public function __construct(Driver\ResultStatement $stmt)
{
$this->stmt = $stmt;
Expand Down
6 changes: 4 additions & 2 deletions lib/Doctrine/DBAL/Query/QueryBuilder.php
Expand Up @@ -201,14 +201,16 @@ public function getState()
/**
* Executes this query using the bound parameters and their types.
*
* @return ForwardCompatibility\DriverStatement|ForwardCompatibility\DriverResultStatement|int
* @return ForwardCompatibility\DriverStatement|int
*
* @throws Exception
*/
public function execute()
{
if ($this->type === self::SELECT) {
return $this->connection->executeQuery($this->getSQL(), $this->params, $this->paramTypes);
return ForwardCompatibility\Result::ensure(
$this->connection->executeQuery($this->getSQL(), $this->params, $this->paramTypes)
);
}

return $this->connection->executeStatement($this->getSQL(), $this->params, $this->paramTypes);
Expand Down
18 changes: 18 additions & 0 deletions tests/Doctrine/Tests/DBAL/ForwardCompatibility/ResultTest.php
Expand Up @@ -41,6 +41,24 @@ public function setUp(): void
);
}

public function testEnsureWithResult(): void
{
$instance = Result::ensure($this->instance);
$this->assertSame($this->instance, $instance, 'Result is not wrapped twice');
}

public function testEnsureWithResultStatement(): void
{
$instance = Result::ensure($this->createMock(Driver\ResultStatement::class));
$this->assertInstanceOf(Result::class, $instance);
}

public function testEnsureWithStatement(): void
{
$instance = Result::ensure($this->createMock(Driver\Statement::class));
$this->assertInstanceOf(Result::class, $instance);
}

public function testIsTraversable(): void
{
$this->instance->setFetchMode(PDO::FETCH_ASSOC);
Expand Down
23 changes: 22 additions & 1 deletion tests/Doctrine/Tests/DBAL/Query/QueryBuilderTest.php
Expand Up @@ -3,15 +3,18 @@
namespace Doctrine\Tests\DBAL\Query;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Query\Expression\ExpressionBuilder;
use Doctrine\DBAL\Query\QueryBuilder;
use Doctrine\DBAL\Query\QueryException;
use Doctrine\DBAL\Result;
use Doctrine\Tests\DbalTestCase;
use PHPUnit\Framework\MockObject\MockObject;

class QueryBuilderTest extends DbalTestCase
{
/** @var Connection */
/** @var Connection&MockObject */
protected $conn;

protected function setUp(): void
Expand Down Expand Up @@ -1075,4 +1078,22 @@ public function testOrHavingEmptyStringStartingWithNonEmptyExpression(): void

self::assertSame('SELECT id FROM foo HAVING (a = b) OR (c = d)', $qb->getSQL());
}

public function testExecuteSelect(): void
{
$qb = new QueryBuilder($this->conn);

$this->conn
->expects($this->any())
->method('executeQuery')
->willReturn($this->createMock(Driver\Statement::class));

$result = $qb
->select('id')
->from('foo')
->execute();

self::assertInstanceOf(Driver\Statement::class, $result);
self::assertInstanceOf(Result::class, $result);
}
}

0 comments on commit 1a5487b

Please sign in to comment.