Unverified Commit 663fe259 authored by Sergei Morozov's avatar Sergei Morozov

Merge branch '2.11.x' into 3.0.x

parents e33c76a6 3c199249
...@@ -228,6 +228,10 @@ Please use other database client applications for import, e.g.: ...@@ -228,6 +228,10 @@ Please use other database client applications for import, e.g.:
# Upgrade to 2.11 # Upgrade to 2.11
## `Connection::getParams()` has been marked internal
Consumers of the Connection class should not rely on connection parameters stored in the connection object. If needed, they should be obtained from a different source, e.g. application configuration.
## Deprecated `Doctrine\DBAL\Driver::getDatabase()` ## Deprecated `Doctrine\DBAL\Driver::getDatabase()`
- The usage of `Doctrine\DBAL\Driver::getDatabase()` is deprecated. Please use `Doctrine\DBAL\Connection::getDatabase()` instead. - The usage of `Doctrine\DBAL\Driver::getDatabase()` is deprecated. Please use `Doctrine\DBAL\Connection::getDatabase()` instead.
......
This diff is collapsed.
...@@ -101,6 +101,11 @@ ...@@ -101,6 +101,11 @@
<exclude-pattern>src/Driver/SQLSrv/Result.php</exclude-pattern> <exclude-pattern>src/Driver/SQLSrv/Result.php</exclude-pattern>
</rule> </rule>
<!-- See https://github.com/slevomat/coding-standard/issues/1038 -->
<rule ref="SlevomatCodingStandard.Namespaces.UnusedUses">
<exclude-pattern>tests/Functional/Driver/IBMDB2/DB2StatementTest.php</exclude-pattern>
</rule>
<!-- see https://github.com/doctrine/dbal/issues/3377 --> <!-- see https://github.com/doctrine/dbal/issues/3377 -->
<rule ref="SlevomatCodingStandard.Operators.DisallowEqualOperators.DisallowedNotEqualOperator"> <rule ref="SlevomatCodingStandard.Operators.DisallowEqualOperators.DisallowedNotEqualOperator">
<exclude-pattern>src/Schema/Comparator.php</exclude-pattern> <exclude-pattern>src/Schema/Comparator.php</exclude-pattern>
......
...@@ -189,6 +189,8 @@ class Connection implements DriverConnection ...@@ -189,6 +189,8 @@ class Connection implements DriverConnection
/** /**
* Gets the parameters used during instantiation. * Gets the parameters used during instantiation.
* *
* @internal
*
* @return mixed[] * @return mixed[]
*/ */
public function getParams() public function getParams()
...@@ -987,7 +989,7 @@ class Connection implements DriverConnection ...@@ -987,7 +989,7 @@ class Connection implements DriverConnection
throw CacheException::noResultDriverConfigured(); throw CacheException::noResultDriverConfigured();
} }
$connectionParams = $this->getParams(); $connectionParams = $this->params;
unset($connectionParams['platform']); unset($connectionParams['platform']);
[$cacheKey, $realKey] = $qcp->generateCacheKeys($query, $params, $types, $connectionParams); [$cacheKey, $realKey] = $qcp->generateCacheKeys($query, $params, $types, $connectionParams);
......
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Driver\AbstractSQLServerDriver;
use Doctrine\DBAL\Driver\AbstractDriverException;
/**
* @internal
*
* @psalm-immutable
*/
final class PortWithoutHost extends AbstractDriverException
{
public static function new(): self
{
return new self('Connection port specified without the host');
}
}
...@@ -2,6 +2,9 @@ ...@@ -2,6 +2,9 @@
namespace Doctrine\DBAL\Driver\IBMDB2; namespace Doctrine\DBAL\Driver\IBMDB2;
use Doctrine\DBAL\Driver\IBMDB2\Exception\ConnectionError;
use Doctrine\DBAL\Driver\IBMDB2\Exception\ConnectionFailed;
use Doctrine\DBAL\Driver\IBMDB2\Exception\PrepareFailed;
use Doctrine\DBAL\Driver\Result as ResultInterface; use Doctrine\DBAL\Driver\Result as ResultInterface;
use Doctrine\DBAL\Driver\ServerInfoAwareConnection; use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
use Doctrine\DBAL\Driver\Statement as DriverStatement; use Doctrine\DBAL\Driver\Statement as DriverStatement;
...@@ -11,7 +14,6 @@ use stdClass; ...@@ -11,7 +14,6 @@ use stdClass;
use function assert; use function assert;
use function db2_autocommit; use function db2_autocommit;
use function db2_commit; use function db2_commit;
use function db2_conn_errormsg;
use function db2_connect; use function db2_connect;
use function db2_escape_string; use function db2_escape_string;
use function db2_exec; use function db2_exec;
...@@ -21,7 +23,7 @@ use function db2_pconnect; ...@@ -21,7 +23,7 @@ use function db2_pconnect;
use function db2_prepare; use function db2_prepare;
use function db2_rollback; use function db2_rollback;
use function db2_server_info; use function db2_server_info;
use function db2_stmt_errormsg; use function error_get_last;
use function is_bool; use function is_bool;
use const DB2_AUTOCOMMIT_OFF; use const DB2_AUTOCOMMIT_OFF;
...@@ -51,7 +53,7 @@ class DB2Connection implements ServerInfoAwareConnection ...@@ -51,7 +53,7 @@ class DB2Connection implements ServerInfoAwareConnection
} }
if ($conn === false) { if ($conn === false) {
throw new DB2Exception(db2_conn_errormsg()); throw ConnectionFailed::new();
} }
$this->conn = $conn; $this->conn = $conn;
...@@ -79,8 +81,9 @@ class DB2Connection implements ServerInfoAwareConnection ...@@ -79,8 +81,9 @@ class DB2Connection implements ServerInfoAwareConnection
public function prepare(string $sql): DriverStatement public function prepare(string $sql): DriverStatement
{ {
$stmt = @db2_prepare($this->conn, $sql); $stmt = @db2_prepare($this->conn, $sql);
if ($stmt === false) { if ($stmt === false) {
throw new DB2Exception(db2_stmt_errormsg()); throw PrepareFailed::new(error_get_last()['message']);
} }
return new DB2Statement($stmt); return new DB2Statement($stmt);
...@@ -110,7 +113,7 @@ class DB2Connection implements ServerInfoAwareConnection ...@@ -110,7 +113,7 @@ class DB2Connection implements ServerInfoAwareConnection
$stmt = @db2_exec($this->conn, $statement); $stmt = @db2_exec($this->conn, $statement);
if ($stmt === false) { if ($stmt === false) {
throw new DB2Exception(db2_stmt_errormsg()); throw ConnectionError::new($this->conn);
} }
return db2_num_rows($stmt); return db2_num_rows($stmt);
...@@ -141,7 +144,7 @@ class DB2Connection implements ServerInfoAwareConnection ...@@ -141,7 +144,7 @@ class DB2Connection implements ServerInfoAwareConnection
public function commit() public function commit()
{ {
if (! db2_commit($this->conn)) { if (! db2_commit($this->conn)) {
throw new DB2Exception(db2_conn_errormsg($this->conn)); throw ConnectionError::new($this->conn);
} }
$result = db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON); $result = db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON);
...@@ -156,7 +159,7 @@ class DB2Connection implements ServerInfoAwareConnection ...@@ -156,7 +159,7 @@ class DB2Connection implements ServerInfoAwareConnection
public function rollBack() public function rollBack()
{ {
if (! db2_rollback($this->conn)) { if (! db2_rollback($this->conn)) {
throw new DB2Exception(db2_conn_errormsg($this->conn)); throw ConnectionError::new($this->conn);
} }
$result = db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON); $result = db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON);
......
...@@ -2,11 +2,11 @@ ...@@ -2,11 +2,11 @@
namespace Doctrine\DBAL\Driver\IBMDB2; namespace Doctrine\DBAL\Driver\IBMDB2;
use Exception; use Doctrine\DBAL\Driver\AbstractDriverException;
/** /**
* @psalm-immutable * @psalm-immutable
*/ */
class DB2Exception extends Exception class DB2Exception extends AbstractDriverException
{ {
} }
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace Doctrine\DBAL\Driver\IBMDB2; namespace Doctrine\DBAL\Driver\IBMDB2;
use Doctrine\DBAL\Driver\IBMDB2\Exception\StatementError;
use Doctrine\DBAL\Driver\Result as ResultInterface; use Doctrine\DBAL\Driver\Result as ResultInterface;
use Doctrine\DBAL\Driver\Statement; use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\ParameterType;
...@@ -9,7 +10,6 @@ use Doctrine\DBAL\ParameterType; ...@@ -9,7 +10,6 @@ use Doctrine\DBAL\ParameterType;
use function assert; use function assert;
use function db2_bind_param; use function db2_bind_param;
use function db2_execute; use function db2_execute;
use function db2_stmt_errormsg;
use function error_get_last; use function error_get_last;
use function fclose; use function fclose;
use function fwrite; use function fwrite;
...@@ -103,7 +103,7 @@ class DB2Statement implements Statement ...@@ -103,7 +103,7 @@ class DB2Statement implements Statement
$this->bindParam[$position] =& $variable; $this->bindParam[$position] =& $variable;
if (! db2_bind_param($this->stmt, $position, 'variable', $parameterType, $dataType)) { if (! db2_bind_param($this->stmt, $position, 'variable', $parameterType, $dataType)) {
throw new DB2Exception(db2_stmt_errormsg()); throw StatementError::new($this->stmt);
} }
} }
...@@ -141,7 +141,7 @@ class DB2Statement implements Statement ...@@ -141,7 +141,7 @@ class DB2Statement implements Statement
$this->lobs = []; $this->lobs = [];
if ($result === false) { if ($result === false) {
throw new DB2Exception(db2_stmt_errormsg()); throw StatementError::new($this->stmt);
} }
return new Result($this->stmt); return new Result($this->stmt);
......
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Driver\IBMDB2\Exception;
use Doctrine\DBAL\Driver\IBMDB2\DB2Exception;
use function db2_conn_error;
use function db2_conn_errormsg;
/**
* @psalm-immutable
*/
final class ConnectionError extends DB2Exception
{
/**
* @param resource $connection
*/
public static function new($connection): self
{
return new self(db2_conn_errormsg($connection), db2_conn_error($connection));
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Driver\IBMDB2\Exception;
use Doctrine\DBAL\Driver\IBMDB2\DB2Exception;
use function db2_conn_error;
use function db2_conn_errormsg;
/**
* @psalm-immutable
*/
final class ConnectionFailed extends DB2Exception
{
public static function new(): self
{
return new self(db2_conn_errormsg(), db2_conn_error());
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Driver\IBMDB2\Exception;
use Doctrine\DBAL\Driver\IBMDB2\DB2Exception;
/**
* @psalm-immutable
*/
final class PrepareFailed extends DB2Exception
{
public static function new(string $message): self
{
return new self($message);
}
}
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Driver\IBMDB2\Exception;
use Doctrine\DBAL\Driver\IBMDB2\DB2Exception;
use function db2_stmt_error;
use function db2_stmt_errormsg;
/**
* @psalm-immutable
*/
final class StatementError extends DB2Exception
{
/**
* @param resource $statement
*/
public static function new($statement): self
{
return new self(db2_stmt_errormsg($statement), db2_stmt_error($statement));
}
}
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
namespace Doctrine\DBAL\Driver\PDOSqlsrv; namespace Doctrine\DBAL\Driver\PDOSqlsrv;
use Doctrine\DBAL\Driver\AbstractSQLServerDriver; use Doctrine\DBAL\Driver\AbstractSQLServerDriver;
use Doctrine\DBAL\Driver\AbstractSQLServerDriver\PortWithoutHost;
use PDO; use PDO;
use function is_int; use function is_int;
...@@ -56,10 +57,12 @@ class Driver extends AbstractSQLServerDriver ...@@ -56,10 +57,12 @@ class Driver extends AbstractSQLServerDriver
if (isset($params['host'])) { if (isset($params['host'])) {
$dsn .= $params['host']; $dsn .= $params['host'];
}
if (isset($params['port'])) { if (isset($params['port'])) {
$dsn .= ',' . $params['port']; $dsn .= ',' . $params['port'];
}
} elseif (isset($params['port'])) {
throw PortWithoutHost::new();
} }
if (isset($params['dbname'])) { if (isset($params['dbname'])) {
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
namespace Doctrine\DBAL\Driver\SQLSrv; namespace Doctrine\DBAL\Driver\SQLSrv;
use Doctrine\DBAL\Driver\AbstractSQLServerDriver; use Doctrine\DBAL\Driver\AbstractSQLServerDriver;
use Doctrine\DBAL\Driver\AbstractSQLServerDriver\PortWithoutHost;
/** /**
* Driver for ext/sqlsrv. * Driver for ext/sqlsrv.
...@@ -14,13 +15,16 @@ class Driver extends AbstractSQLServerDriver ...@@ -14,13 +15,16 @@ class Driver extends AbstractSQLServerDriver
*/ */
public function connect(array $params) public function connect(array $params)
{ {
if (! isset($params['host'])) { $serverName = '';
throw new SQLSrvException("Missing 'host' in configuration for sqlsrv driver.");
} if (isset($params['host'])) {
$serverName = $params['host'];
$serverName = $params['host']; if (isset($params['port'])) {
if (isset($params['port'])) { $serverName .= ',' . $params['port'];
$serverName .= ', ' . $params['port']; }
} elseif (isset($params['port'])) {
throw PortWithoutHost::new();
} }
$driverOptions = $params['driver_options'] ?? []; $driverOptions = $params['driver_options'] ?? [];
......
...@@ -4,6 +4,7 @@ namespace Doctrine\DBAL\Id; ...@@ -4,6 +4,7 @@ namespace Doctrine\DBAL\Id;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException; use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\DriverManager; use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\LockMode; use Doctrine\DBAL\LockMode;
use Throwable; use Throwable;
...@@ -69,12 +70,16 @@ class TableGenerator ...@@ -69,12 +70,16 @@ class TableGenerator
*/ */
public function __construct(Connection $conn, $generatorTableName = 'sequences') public function __construct(Connection $conn, $generatorTableName = 'sequences')
{ {
$params = $conn->getParams(); if ($conn->getDriver() instanceof Driver\PDOSqlite\Driver) {
if ($params['driver'] === 'pdo_sqlite') {
throw new DBALException('Cannot use TableGenerator with SQLite.'); throw new DBALException('Cannot use TableGenerator with SQLite.');
} }
$this->conn = DriverManager::getConnection($params, $conn->getConfiguration(), $conn->getEventManager()); $this->conn = DriverManager::getConnection(
$conn->getParams(),
$conn->getConfiguration(),
$conn->getEventManager()
);
$this->generatorTableName = $generatorTableName; $this->generatorTableName = $generatorTableName;
} }
......
...@@ -3,20 +3,14 @@ ...@@ -3,20 +3,14 @@
namespace Doctrine\DBAL\Tests\Driver; namespace Doctrine\DBAL\Tests\Driver;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver; use Doctrine\DBAL\Driver\AbstractSQLServerDriver\PortWithoutHost;
use Doctrine\DBAL\Driver\AbstractSQLServerDriver;
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\SQLServer2012Platform; use Doctrine\DBAL\Platforms\SQLServer2012Platform;
use Doctrine\DBAL\Schema\AbstractSchemaManager; use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\SQLServerSchemaManager; use Doctrine\DBAL\Schema\SQLServerSchemaManager;
class AbstractSQLServerDriverTest extends AbstractDriverTest abstract class AbstractSQLServerDriverTest extends AbstractDriverTest
{ {
protected function createDriver(): Driver
{
return $this->getMockForAbstractClass(AbstractSQLServerDriver::class);
}
protected function createPlatform(): AbstractPlatform protected function createPlatform(): AbstractPlatform
{ {
return new SQLServer2012Platform(); return new SQLServer2012Platform();
...@@ -36,4 +30,10 @@ class AbstractSQLServerDriverTest extends AbstractDriverTest ...@@ -36,4 +30,10 @@ class AbstractSQLServerDriverTest extends AbstractDriverTest
['12', SQLServer2012Platform::class], ['12', SQLServer2012Platform::class],
]; ];
} }
public function testPortWithoutHost(): void
{
$this->expectException(PortWithoutHost::class);
$this->driver->connect(['port' => 1433]);
}
} }
...@@ -5,6 +5,7 @@ namespace Doctrine\DBAL\Tests\Functional; ...@@ -5,6 +5,7 @@ namespace Doctrine\DBAL\Tests\Functional;
use DateTime; use DateTime;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException; use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\IBMDB2\DB2Driver;
use Doctrine\DBAL\Driver\Mysqli\Driver as MySQLiDriver; use Doctrine\DBAL\Driver\Mysqli\Driver as MySQLiDriver;
use Doctrine\DBAL\Driver\SQLSrv\Driver as SQLSrvDriver; use Doctrine\DBAL\Driver\SQLSrv\Driver as SQLSrvDriver;
use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\ParameterType;
...@@ -191,8 +192,9 @@ class DataAccessTest extends FunctionalTestCase ...@@ -191,8 +192,9 @@ class DataAccessTest extends FunctionalTestCase
/** /**
* @group DBAL-209 * @group DBAL-209
* @dataProvider fetchProvider
*/ */
public function testFetchAllWithMissingTypes(): void public function testFetchAllWithMissingTypes(callable $fetch): void
{ {
if ( if (
$this->connection->getDriver() instanceof MySQLiDriver || $this->connection->getDriver() instanceof MySQLiDriver ||
...@@ -201,13 +203,51 @@ class DataAccessTest extends FunctionalTestCase ...@@ -201,13 +203,51 @@ class DataAccessTest extends FunctionalTestCase
self::markTestSkipped('mysqli and sqlsrv actually supports this'); self::markTestSkipped('mysqli and sqlsrv actually supports this');
} }
if (
$this->connection->getDriver() instanceof DB2Driver
) {
$this->markTestSkipped(
'ibm_ibm2 may or may not report the error depending on the PHP version and the connection state'
);
}
$datetimeString = '2010-01-01 10:10:10'; $datetimeString = '2010-01-01 10:10:10';
$datetime = new DateTime($datetimeString); $datetime = new DateTime($datetimeString);
$sql = 'SELECT test_int, test_datetime FROM fetch_table WHERE test_int = ? AND test_datetime = ?'; $sql = 'SELECT test_int, test_datetime FROM fetch_table WHERE test_int = ? AND test_datetime = ?';
$this->expectException(DBALException::class); $this->expectException(DBALException::class);
$this->connection->fetchAllAssociative($sql, [1, $datetime]); $fetch($this->connection, $sql, [1, $datetime]);
}
/**
* @return iterable<string,array{0:callable}>
*/
public static function fetchProvider(): iterable
{
yield 'fetch-all-associative' => [
static function (Connection $connection, string $query, array $params): void {
$connection->fetchAllAssociative($query, $params);
},
];
yield 'fetch-numeric' => [
static function (Connection $connection, string $query, array $params): void {
$connection->fetchNumeric($query, $params);
},
];
yield 'fetch-associative' => [
static function (Connection $connection, string $query, array $params): void {
$connection->fetchAssociative($query, $params);
},
];
yield 'fetch-one' => [
static function (Connection $connection, string $query, array $params): void {
$connection->fetchOne($query, $params);
},
];
} }
public function testFetchNoResult(): void public function testFetchNoResult(): void
...@@ -250,24 +290,6 @@ class DataAccessTest extends FunctionalTestCase ...@@ -250,24 +290,6 @@ class DataAccessTest extends FunctionalTestCase
self::assertStringStartsWith($datetimeString, $row['test_datetime']); self::assertStringStartsWith($datetimeString, $row['test_datetime']);
} }
public function testFetchAssocWithMissingTypes(): void
{
if (
$this->connection->getDriver() instanceof MySQLiDriver ||
$this->connection->getDriver() instanceof SQLSrvDriver
) {
self::markTestSkipped('mysqli and sqlsrv actually supports this');
}
$datetimeString = '2010-01-01 10:10:10';
$datetime = new DateTime($datetimeString);
$sql = 'SELECT test_int, test_datetime FROM fetch_table WHERE test_int = ? AND test_datetime = ?';
$this->expectException(DBALException::class);
$this->connection->fetchAssociative($sql, [1, $datetime]);
}
public function testFetchArray(): void public function testFetchArray(): void
{ {
$sql = 'SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?'; $sql = 'SELECT test_int, test_string FROM fetch_table WHERE test_int = ? AND test_string = ?';
...@@ -297,25 +319,7 @@ class DataAccessTest extends FunctionalTestCase ...@@ -297,25 +319,7 @@ class DataAccessTest extends FunctionalTestCase
self::assertStringStartsWith($datetimeString, $row[1]); self::assertStringStartsWith($datetimeString, $row[1]);
} }
public function testFetchArrayWithMissingTypes(): void public function testFetchColumn(): void
{
if (
$this->connection->getDriver() instanceof MySQLiDriver ||
$this->connection->getDriver() instanceof SQLSrvDriver
) {
self::markTestSkipped('mysqli and sqlsrv actually supports this');
}
$datetimeString = '2010-01-01 10:10:10';
$datetime = new DateTime($datetimeString);
$sql = 'SELECT test_int, test_datetime FROM fetch_table WHERE test_int = ? AND test_datetime = ?';
$this->expectException(DBALException::class);
$this->connection->fetchNumeric($sql, [1, $datetime]);
}
public function testFetchOne(): void
{ {
$sql = 'SELECT test_int FROM fetch_table WHERE test_int = ? AND test_string = ?'; $sql = 'SELECT test_int FROM fetch_table WHERE test_int = ? AND test_string = ?';
$testInt = $this->connection->fetchOne($sql, [1, 'foo']); $testInt = $this->connection->fetchOne($sql, [1, 'foo']);
...@@ -345,24 +349,6 @@ class DataAccessTest extends FunctionalTestCase ...@@ -345,24 +349,6 @@ class DataAccessTest extends FunctionalTestCase
self::assertStringStartsWith($datetimeString, $column); self::assertStringStartsWith($datetimeString, $column);
} }
public function testFetchOneWithMissingTypes(): void
{
if (
$this->connection->getDriver() instanceof MySQLiDriver ||
$this->connection->getDriver() instanceof SQLSrvDriver
) {
self::markTestSkipped('mysqli and sqlsrv actually supports this');
}
$datetimeString = '2010-01-01 10:10:10';
$datetime = new DateTime($datetimeString);
$sql = 'SELECT test_int, test_datetime FROM fetch_table WHERE test_int = ? AND test_datetime = ?';
$this->expectException(DBALException::class);
$this->connection->fetchOne($sql, [1, $datetime]);
}
/** /**
* @group DDC-697 * @group DDC-697
*/ */
......
<?php
namespace Doctrine\DBAL\Tests\Functional\Driver\IBMDB2;
use Doctrine\DBAL\Driver\IBMDB2\DB2Connection;
use Doctrine\DBAL\Driver\IBMDB2\DB2Driver;
use Doctrine\DBAL\Driver\IBMDB2\Exception\ConnectionFailed;
use Doctrine\DBAL\Driver\IBMDB2\Exception\PrepareFailed;
use Doctrine\DBAL\Tests\FunctionalTestCase;
use ReflectionProperty;
use function db2_close;
use function extension_loaded;
class ConnectionTest extends FunctionalTestCase
{
protected function setUp(): void
{
if (! extension_loaded('ibm_db2')) {
$this->markTestSkipped('ibm_db2 is not installed.');
}
parent::setUp();
if ($this->connection->getDriver() instanceof DB2Driver) {
return;
}
$this->markTestSkipped('ibm_db2 only test.');
}
protected function tearDown(): void
{
$this->resetSharedConn();
}
public function testConnectionFailure(): void
{
$this->expectException(ConnectionFailed::class);
new DB2Connection('garbage', false, '', '');
}
public function testPrepareFailure(): void
{
$driverConnection = $this->connection->getWrappedConnection();
$re = new ReflectionProperty($driverConnection, 'conn');
$re->setAccessible(true);
$conn = $re->getValue($driverConnection);
db2_close($conn);
$this->expectException(PrepareFailed::class);
$driverConnection->prepare('SELECT 1');
}
}
...@@ -5,10 +5,15 @@ declare(strict_types=1); ...@@ -5,10 +5,15 @@ declare(strict_types=1);
namespace Doctrine\DBAL\Tests\Functional\Driver\IBMDB2; namespace Doctrine\DBAL\Tests\Functional\Driver\IBMDB2;
use Doctrine\DBAL\Driver\IBMDB2\DB2Driver; use Doctrine\DBAL\Driver\IBMDB2\DB2Driver;
use Doctrine\DBAL\Driver\IBMDB2\Exception\StatementError;
use Doctrine\DBAL\Tests\FunctionalTestCase; use Doctrine\DBAL\Tests\FunctionalTestCase;
use function extension_loaded; use function extension_loaded;
use const E_ALL;
use const E_NOTICE;
use const E_WARNING;
class DB2StatementTest extends FunctionalTestCase class DB2StatementTest extends FunctionalTestCase
{ {
protected function setUp(): void protected function setUp(): void
...@@ -28,12 +33,14 @@ class DB2StatementTest extends FunctionalTestCase ...@@ -28,12 +33,14 @@ class DB2StatementTest extends FunctionalTestCase
public function testExecutionErrorsAreNotSuppressed(): void public function testExecutionErrorsAreNotSuppressed(): void
{ {
$stmt = $this->connection->prepare('SELECT * FROM SYSIBM.SYSDUMMY1 WHERE \'foo\' = ?'); $driverConnection = $this->connection->getWrappedConnection();
$stmt = $driverConnection->prepare('SELECT * FROM SYSIBM.SYSDUMMY1 WHERE \'foo\' = ?');
// unwrap the statement to prevent the wrapper from handling the PHPUnit-originated exception // prevent the PHPUnit error handler from handling the errors that db2_execute() may trigger
$wrappedStmt = $stmt->getWrappedStatement(); $this->iniSet('error_reporting', (string) (E_ALL & ~E_WARNING & ~E_NOTICE));
$this->expectNotice(); $this->expectException(StatementError::class);
$wrappedStmt->execute([[]]); $stmt->execute([[]]);
} }
} }
...@@ -378,27 +378,19 @@ class SchemaTest extends TestCase ...@@ -378,27 +378,19 @@ class SchemaTest extends TestCase
->method('acceptSchema') ->method('acceptSchema')
->with($schema); ->with($schema);
$visitor->expects(self::at(1)) $visitor->expects(self::exactly(2))
->method('acceptTable')
->with($schema->getTable('baz'));
$visitor->expects(self::at(2))
->method('acceptTable') ->method('acceptTable')
->with($schema->getTable('bla.bloo')); ->withConsecutive(
[$schema->getTable('baz')],
[$schema->getTable('bla.bloo')]
);
$visitor->expects(self::exactly(2)) $visitor->expects(self::exactly(2))
->method('acceptTable');
$visitor->expects(self::at(3))
->method('acceptSequence')
->with($schema->getSequence('moo'));
$visitor->expects(self::at(4))
->method('acceptSequence') ->method('acceptSequence')
->with($schema->getSequence('war')); ->withConsecutive(
[$schema->getSequence('moo')],
$visitor->expects(self::exactly(2)) [$schema->getSequence('war')]
->method('acceptSequence'); );
self::assertNull($schema->visit($visitor)); self::assertNull($schema->visit($visitor));
} }
...@@ -436,10 +428,7 @@ class SchemaTest extends TestCase ...@@ -436,10 +428,7 @@ class SchemaTest extends TestCase
->method('acceptNamespace') ->method('acceptNamespace')
->with('bla'); ->with('bla');
$visitor->expects(self::exactly(3)) $visitor->expects($this->at(4))
->method('acceptNamespace');
$visitor->expects(self::at(4))
->method('acceptTable') ->method('acceptTable')
->with($schema->getTable('baz')); ->with($schema->getTable('baz'));
...@@ -447,10 +436,7 @@ class SchemaTest extends TestCase ...@@ -447,10 +436,7 @@ class SchemaTest extends TestCase
->method('acceptTable') ->method('acceptTable')
->with($schema->getTable('bla.bloo')); ->with($schema->getTable('bla.bloo'));
$visitor->expects(self::exactly(2)) $visitor->expects($this->at(6))
->method('acceptTable');
$visitor->expects(self::at(6))
->method('acceptSequence') ->method('acceptSequence')
->with($schema->getSequence('moo')); ->with($schema->getSequence('moo'));
...@@ -458,9 +444,6 @@ class SchemaTest extends TestCase ...@@ -458,9 +444,6 @@ class SchemaTest extends TestCase
->method('acceptSequence') ->method('acceptSequence')
->with($schema->getSequence('war')); ->with($schema->getSequence('war'));
$visitor->expects(self::exactly(2))
->method('acceptSequence');
self::assertNull($schema->visit($visitor)); self::assertNull($schema->visit($visitor));
} }
} }
...@@ -29,15 +29,11 @@ class DropSchemaSqlCollectorTest extends TestCase ...@@ -29,15 +29,11 @@ class DropSchemaSqlCollectorTest extends TestCase
$collector = new DropSchemaSqlCollector($platform); $collector = new DropSchemaSqlCollector($platform);
$platform->expects(self::exactly(2)) $platform->expects(self::exactly(2))
->method('getDropForeignKeySQL');
$platform->expects(self::at(0))
->method('getDropForeignKeySQL')
->with($keyConstraintOne, $tableOne);
$platform->expects(self::at(1))
->method('getDropForeignKeySQL') ->method('getDropForeignKeySQL')
->with($keyConstraintTwo, $tableTwo); ->withConsecutive(
[$keyConstraintOne, $tableOne],
[$keyConstraintTwo, $tableTwo]
);
$collector->acceptForeignKey($tableOne, $keyConstraintOne); $collector->acceptForeignKey($tableOne, $keyConstraintOne);
$collector->acceptForeignKey($tableTwo, $keyConstraintTwo); $collector->acceptForeignKey($tableTwo, $keyConstraintTwo);
......
...@@ -25,10 +25,6 @@ class RunSqlCommandTest extends TestCase ...@@ -25,10 +25,6 @@ class RunSqlCommandTest extends TestCase
protected function setUp(): void protected function setUp(): void
{ {
$this->connectionMock = $this->createMock(Connection::class); $this->connectionMock = $this->createMock(Connection::class);
$this->connectionMock->method('fetchAllAssociative')
->willReturn([[1]]);
$this->connectionMock->method('executeUpdate')
->willReturn(42);
$application = new Application(); $application = new Application();
$application->add(new RunSqlCommand(new SingleConnectionProvider($this->connectionMock))); $application->add(new RunSqlCommand(new SingleConnectionProvider($this->connectionMock)));
...@@ -94,21 +90,25 @@ class RunSqlCommandTest extends TestCase ...@@ -94,21 +90,25 @@ class RunSqlCommandTest extends TestCase
private function expectConnectionExecuteUpdate(): void private function expectConnectionExecuteUpdate(): void
{ {
$this->connectionMock $this->connectionMock
->expects(self::exactly(1)) ->expects(self::once())
->method('executeUpdate'); ->method('executeUpdate')
->willReturn(42);
$this->connectionMock $this->connectionMock
->expects(self::exactly(0)) ->expects(self::never())
->method('fetchAllAssociative'); ->method('fetchAllAssociative');
} }
private function expectConnectionFetchAllAssociative(): void private function expectConnectionFetchAllAssociative(): void
{ {
$this->connectionMock $this->connectionMock
->expects(self::exactly(0)) ->expects(self::once())
->method('executeUpdate'); ->method('fetchAllAssociative')
->willReturn([[1]]);
$this->connectionMock $this->connectionMock
->expects(self::exactly(1)) ->expects(self::never())
->method('fetchAllAssociative'); ->method('executeUpdate');
} }
public function testStatementsWithFetchResultPrintsResult(): void public function testStatementsWithFetchResultPrintsResult(): void
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment