Skip to content

Commit

Permalink
- Add tests on return types of Connection::executeQuery()
Browse files Browse the repository at this point in the history
- More accurate documentation for return types of executed queries
  • Loading branch information
mdumoulin committed Apr 14, 2021
1 parent 5663448 commit 05ef0a7
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 18 deletions.
6 changes: 4 additions & 2 deletions lib/Doctrine/DBAL/Connection.php
Expand Up @@ -1263,7 +1263,9 @@ public function prepare($sql)
* @param array<int, mixed>|array<string, mixed> $params Query parameters
* @param array<int, int|string|Type|null>|array<string, int|string|Type|null> $types Parameter types
*
* @return ForwardCompatibility\Result The executed statement.
* @return ForwardCompatibility\DriverStatement|ForwardCompatibility\DriverResultStatement
*
* The executed statement or the cached result statement if a query cache profile is used
*
* @throws Exception
*/
Expand Down Expand Up @@ -1319,7 +1321,7 @@ public function executeQuery($sql, array $params = [], $types = [], ?QueryCacheP
* @param array<int, mixed>|array<string, mixed> $params Query parameters
* @param array<int, int|string|Type|null>|array<string, int|string|Type|null> $types Parameter types
*
* @return ForwardCompatibility\Result
* @return ForwardCompatibility\DriverResultStatement
*
* @throws CacheException
*/
Expand Down
@@ -0,0 +1,9 @@
<?php

namespace Doctrine\DBAL\ForwardCompatibility;

use Doctrine\DBAL;

interface DriverResultStatement extends DBAL\Driver\ResultStatement, DBAL\Result
{
}
9 changes: 9 additions & 0 deletions lib/Doctrine/DBAL/ForwardCompatibility/DriverStatement.php
@@ -0,0 +1,9 @@
<?php

namespace Doctrine\DBAL\ForwardCompatibility;

use Doctrine\DBAL;

interface DriverStatement extends DBAL\Driver\Statement, DBAL\Result
{
}
22 changes: 10 additions & 12 deletions lib/Doctrine/DBAL/ForwardCompatibility/Result.php
Expand Up @@ -2,12 +2,10 @@

namespace Doctrine\DBAL\ForwardCompatibility;

use Doctrine\DBAL\Driver\ResultStatement as DriverResultStatement;
use Doctrine\DBAL\Driver\Statement as DriverStatement;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Exception\NoKeyValue;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Result as BaseResult;
use Doctrine\Deprecations\Deprecation;
use IteratorAggregate;
use PDO;
Expand All @@ -20,18 +18,18 @@
* A wrapper around a Doctrine\DBAL\Driver\ResultStatement that adds 3.0 features
* defined in Result interface
*/
final class Result implements IteratorAggregate, DriverStatement, BaseResult
class Result implements IteratorAggregate, DriverStatement, DriverResultStatement
{
/** @var DriverResultStatement */
/** @var Driver\ResultStatement */
private $stmt;

public function __construct(DriverResultStatement $stmt)
public function __construct(Driver\ResultStatement $stmt)
{
$this->stmt = $stmt;
}

/**
* @return DriverResultStatement
* @return Driver\ResultStatement
*/
public function getIterator()
{
Expand Down Expand Up @@ -317,7 +315,7 @@ public function bindValue($param, $value, $type = ParameterType::STRING)
'Result::bindValue() is deprecated, no replacement.'
);

if ($this->stmt instanceof DriverStatement) {
if ($this->stmt instanceof Driver\Statement) {
return $this->stmt->bindValue($param, $value, $type);
}

Expand All @@ -337,7 +335,7 @@ public function bindParam($param, &$variable, $type = ParameterType::STRING, $le
'Result::bindParam() is deprecated, no replacement.'
);

if ($this->stmt instanceof DriverStatement) {
if ($this->stmt instanceof Driver\Statement) {
return $this->stmt->bindParam($param, $variable, $type, $length);
}

Expand All @@ -357,7 +355,7 @@ public function errorCode()
'Result::errorCode() is deprecated, the error information is available via exceptions.'
);

if ($this->stmt instanceof DriverStatement) {
if ($this->stmt instanceof Driver\Statement) {
return $this->stmt->errorCode();
}

Expand All @@ -377,7 +375,7 @@ public function errorInfo()
'Result::errorInfo() is deprecated, the error information is available via exceptions.'
);

if ($this->stmt instanceof DriverStatement) {
if ($this->stmt instanceof Driver\Statement) {
return $this->stmt->errorInfo();
}

Expand All @@ -397,7 +395,7 @@ public function execute($params = null)
'Result::execute() is deprecated, no replacement.'
);

if ($this->stmt instanceof DriverStatement) {
if ($this->stmt instanceof Driver\Statement) {
return $this->stmt->execute($params);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/DBAL/Query/QueryBuilder.php
Expand Up @@ -4,7 +4,7 @@

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\ForwardCompatibility\Result as ForwardCompatibleResult;
use Doctrine\DBAL\ForwardCompatibility;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Query\Expression\CompositeExpression;
use Doctrine\DBAL\Query\Expression\ExpressionBuilder;
Expand Down Expand Up @@ -201,7 +201,7 @@ public function getState()
/**
* Executes this query using the bound parameters and their types.
*
* @return ForwardCompatibleResult|int
* @return ForwardCompatibility\DriverStatement|ForwardCompatibility\DriverResultStatement|int
*
* @throws Exception
*/
Expand Down
32 changes: 30 additions & 2 deletions tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php
Expand Up @@ -6,11 +6,13 @@
use Doctrine\DBAL\Cache\QueryCacheProfile;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\ConnectionException;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\Connection as DriverConnection;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\ForwardCompatibility;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Result;
use Doctrine\DBAL\Types\Types;
use Doctrine\Tests\DbalFunctionalTestCase;
use Error;
Expand Down Expand Up @@ -366,7 +368,32 @@ public function testResultCompatibilityWhenExecutingQueryWithoutParam(): void
$this->connection->getDatabasePlatform()->getDummySelectSQL()
);

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

public function testResultCompatibilityWhenExecutingQueryWithParams(): void
{
$result = $this->connection->executeQuery(
$this->connection->getDatabasePlatform()->getDummySelectSQL(),
['param1' => 'value']
);

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

public function testResultCompatibilityWhenExecutingQueryWithQueryCacheParam(): void
{
$result = $this->connection->executeQuery(
$this->connection->getDatabasePlatform()->getDummySelectSQL(),
[],
[],
new QueryCacheProfile(1, 'cacheKey', new ArrayCache())
);

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

public function testResultCompatibilityWhenExecutingCacheQuery(): void
Expand All @@ -378,6 +405,7 @@ public function testResultCompatibilityWhenExecutingCacheQuery(): void
new QueryCacheProfile(1, 'cacheKey', new ArrayCache())
);

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

0 comments on commit 05ef0a7

Please sign in to comment.