Simplify Driver::connect() signature

parent 57aab098
# Upgrade to 3.0 # Upgrade to 3.0
## BC BREAK changes the `Driver::connect()` signature
The method no longer accepts the `$username`, `$password` and `$driverOptions` arguments. The corresponding values are expected to be passed as the "user", "password" and "driver_options" keys of the `$params` argument respectively.
## Removed `MasterSlaveConnection` ## Removed `MasterSlaveConnection`
This class was deprecated in favor of `PrimaryReadReplicaConnection` This class was deprecated in favor of `PrimaryReadReplicaConnection`
......
...@@ -83,11 +83,12 @@ ...@@ -83,11 +83,12 @@
<!-- https://github.com/squizlabs/PHP_CodeSniffer/issues/2837 --> <!-- https://github.com/squizlabs/PHP_CodeSniffer/issues/2837 -->
<rule ref="Squiz.NamingConventions.ValidVariableName.NotCamelCaps"> <rule ref="Squiz.NamingConventions.ValidVariableName.NotCamelCaps">
<!-- <!--
This file uses the return value db2_server_info(), which does not follow conventions These files use the underlying driver APIs that don't comply with the coding standard
phpcs wrongly complains about it, and that has been reported here: phpcs wrongly complains about them, and that has been reported here:
https://github.com/squizlabs/PHP_CodeSniffer/issues/2950 https://github.com/squizlabs/PHP_CodeSniffer/issues/2950
--> -->
<exclude-pattern>src/Driver/IBMDB2/DB2Connection.php</exclude-pattern> <exclude-pattern>src/Driver/IBMDB2/DB2Connection.php</exclude-pattern>
<exclude-pattern>src/Driver/Mysqli/MysqliConnection.php</exclude-pattern>
<!-- See https://github.com/squizlabs/PHP_CodeSniffer/issues/2837 --> <!-- See https://github.com/squizlabs/PHP_CodeSniffer/issues/2837 -->
<exclude-pattern>src/SQLParserUtils.php</exclude-pattern> <exclude-pattern>src/SQLParserUtils.php</exclude-pattern>
<exclude-pattern>src/Tools/Dumper.php</exclude-pattern> <exclude-pattern>src/Tools/Dumper.php</exclude-pattern>
......
...@@ -352,12 +352,8 @@ class Connection implements DriverConnection ...@@ -352,12 +352,8 @@ class Connection implements DriverConnection
return false; return false;
} }
$driverOptions = $this->params['driverOptions'] ?? [];
$user = $this->params['user'] ?? null;
$password = $this->params['password'] ?? null;
try { try {
$this->_conn = $this->_driver->connect($this->params, $user, $password, $driverOptions); $this->_conn = $this->_driver->connect($this->params);
} catch (DriverException $e) { } catch (DriverException $e) {
throw DBALException::driverException($this->_driver, $e); throw DBALException::driverException($this->_driver, $e);
} }
......
...@@ -225,15 +225,10 @@ class PrimaryReadReplicaConnection extends Connection ...@@ -225,15 +225,10 @@ class PrimaryReadReplicaConnection extends Connection
{ {
$params = $this->getParams(); $params = $this->getParams();
$driverOptions = $params['driverOptions'] ?? [];
$connectionParams = $this->chooseConnectionConfiguration($connectionName, $params); $connectionParams = $this->chooseConnectionConfiguration($connectionName, $params);
$user = $connectionParams['user'] ?? null;
$password = $connectionParams['password'] ?? null;
try { try {
return $this->_driver->connect($connectionParams, $user, $password, $driverOptions); return $this->_driver->connect($connectionParams);
} catch (DriverException $e) { } catch (DriverException $e) {
throw DBALException::driverException($this->_driver, $e); throw DBALException::driverException($this->_driver, $e);
} }
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace Doctrine\DBAL; namespace Doctrine\DBAL;
use Doctrine\DBAL\Driver\Connection as DriverConnection;
use Doctrine\DBAL\Driver\DriverException; use Doctrine\DBAL\Driver\DriverException;
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\AbstractSchemaManager; use Doctrine\DBAL\Schema\AbstractSchemaManager;
...@@ -15,18 +16,13 @@ interface Driver ...@@ -15,18 +16,13 @@ interface Driver
/** /**
* Attempts to create a connection with the database. * Attempts to create a connection with the database.
* *
* The usage of NULL to indicate empty username or password is deprecated. Use an empty string instead. * @param mixed[] $params All connection parameters.
* *
* @param mixed[] $params All connection parameters passed by the user. * @return DriverConnection The database connection.
* @param string|null $username The username to use when connecting.
* @param string|null $password The password to use when connecting.
* @param mixed[] $driverOptions The driver options to use when connecting.
*
* @return \Doctrine\DBAL\Driver\Connection The database connection.
* *
* @throws DriverException * @throws DriverException
*/ */
public function connect(array $params, $username = null, $password = null, array $driverOptions = []); public function connect(array $params);
/** /**
* Gets the DatabasePlatform instance that provides all the metadata about * Gets the DatabasePlatform instance that provides all the metadata about
......
...@@ -33,21 +33,21 @@ class DB2Connection implements ServerInfoAwareConnection ...@@ -33,21 +33,21 @@ class DB2Connection implements ServerInfoAwareConnection
private $conn = null; private $conn = null;
/** /**
* @param mixed[] $params * @param array<string,mixed> $driverOptions
* @param string $username
* @param string $password
* @param mixed[] $driverOptions
* *
* @throws DB2Exception * @throws DB2Exception
*/ */
public function __construct(array $params, $username, $password, $driverOptions = []) public function __construct(
{ string $database,
$isPersistent = (isset($params['persistent']) && $params['persistent'] === true); bool $persistent,
string $username,
if ($isPersistent) { string $password,
$conn = db2_pconnect($params['dbname'], $username, $password, $driverOptions); array $driverOptions = []
) {
if ($persistent) {
$conn = db2_pconnect($database, $username, $password, $driverOptions);
} else { } else {
$conn = db2_connect($params['dbname'], $username, $password, $driverOptions); $conn = db2_connect($database, $username, $password, $driverOptions);
} }
if ($conn === false) { if ($conn === false) {
......
...@@ -12,17 +12,14 @@ class DB2Driver extends AbstractDB2Driver ...@@ -12,17 +12,14 @@ class DB2Driver extends AbstractDB2Driver
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function connect(array $params, $username = null, $password = null, array $driverOptions = []) public function connect(array $params)
{ {
$params['user'] = $username;
$params['password'] = $password;
$params['dbname'] = DataSourceName::fromConnectionParameters($params)->toString();
return new DB2Connection( return new DB2Connection(
$params, DataSourceName::fromConnectionParameters($params)->toString(),
(string) $username, isset($params['persistent']) && $params['persistent'] === true,
(string) $password, $params['user'] ?? '',
$driverOptions $params['password'] ?? '',
$params['driver_options'] ?? []
); );
} }
......
...@@ -3,15 +3,115 @@ ...@@ -3,15 +3,115 @@
namespace Doctrine\DBAL\Driver\Mysqli; namespace Doctrine\DBAL\Driver\Mysqli;
use Doctrine\DBAL\Driver\AbstractMySQLDriver; use Doctrine\DBAL\Driver\AbstractMySQLDriver;
use Doctrine\DBAL\Driver\Mysqli\Initializer\Charset;
use Doctrine\DBAL\Driver\Mysqli\Initializer\Options;
use Doctrine\DBAL\Driver\Mysqli\Initializer\Secure;
use function count;
class Driver extends AbstractMySQLDriver class Driver extends AbstractMySQLDriver
{ {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function connect(array $params, $username = null, $password = null, array $driverOptions = []) public function connect(array $params)
{
if (! empty($params['persistent'])) {
if (! isset($params['host'])) {
throw HostRequired::forPersistentConnection();
}
$host = 'p:' . $params['host'];
} else {
$host = $params['host'] ?? null;
}
$flags = null;
$preInitializers = $postInitializers = [];
if (isset($params['driver_options'])) {
$driverOptions = $params['driver_options'];
if (isset($driverOptions[MysqliConnection::OPTION_FLAGS])) {
$flags = $driverOptions[MysqliConnection::OPTION_FLAGS];
unset($driverOptions[MysqliConnection::OPTION_FLAGS]);
}
$preInitializers = $this->withOptions($preInitializers, $driverOptions);
}
$preInitializers = $this->withSecure($preInitializers, $params);
$postInitializers = $this->withCharset($postInitializers, $params);
return new MysqliConnection(
$host,
$params['user'] ?? null,
$params['password'] ?? null,
$params['dbname'] ?? null,
$params['port'] ?? null,
$params['unix_socket'] ?? null,
$flags,
$preInitializers,
$postInitializers
);
}
/**
* @param list<Initializer> $initializers
* @param array<int,mixed> $options
*
* @return list<Initializer>
*/
private function withOptions(array $initializers, array $options): array
{ {
return new MysqliConnection($params, (string) $username, (string) $password, $driverOptions); if (count($options) !== 0) {
$initializers[] = new Options($options);
}
return $initializers;
}
/**
* @param list<Initializer> $initializers
* @param array<string,mixed> $params
*
* @return list<Initializer>
*/
private function withSecure(array $initializers, array $params): array
{
if (
isset($params['ssl_key']) ||
isset($params['ssl_cert']) ||
isset($params['ssl_ca']) ||
isset($params['ssl_capath']) ||
isset($params['ssl_cipher'])
) {
$initializers[] = new Secure(
$params['ssl_key'] ?? null,
$params['ssl_cert'] ?? null,
$params['ssl_ca'] ?? null,
$params['ssl_capath'] ?? null,
$params['ssl_cipher'] ?? null
);
}
return $initializers;
}
/**
* @param list<Initializer> $initializers
* @param array<string,mixed> $params
*
* @return list<Initializer>
*/
private function withCharset(array $initializers, array $params): array
{
if (isset($params['charset'])) {
$initializers[] = new Charset($params['charset']);
}
return $initializers;
} }
/** /**
......
...@@ -2,9 +2,6 @@ ...@@ -2,9 +2,6 @@
namespace Doctrine\DBAL\Driver\Mysqli; namespace Doctrine\DBAL\Driver\Mysqli;
use Doctrine\DBAL\Driver\Mysqli\Initializer\Charset;
use Doctrine\DBAL\Driver\Mysqli\Initializer\Options;
use Doctrine\DBAL\Driver\Mysqli\Initializer\Secure;
use Doctrine\DBAL\Driver\PingableConnection; use Doctrine\DBAL\Driver\PingableConnection;
use Doctrine\DBAL\Driver\Result as ResultInterface; use Doctrine\DBAL\Driver\Result as ResultInterface;
use Doctrine\DBAL\Driver\ServerInfoAwareConnection; use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
...@@ -12,9 +9,7 @@ use Doctrine\DBAL\Driver\Statement as DriverStatement; ...@@ -12,9 +9,7 @@ use Doctrine\DBAL\Driver\Statement as DriverStatement;
use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\ParameterType;
use mysqli; use mysqli;
use function count;
use function floor; use function floor;
use function ini_get;
use function mysqli_init; use function mysqli_init;
use function stripos; use function stripos;
...@@ -29,55 +24,41 @@ class MysqliConnection implements PingableConnection, ServerInfoAwareConnection ...@@ -29,55 +24,41 @@ class MysqliConnection implements PingableConnection, ServerInfoAwareConnection
private $conn; private $conn;
/** /**
* @param mixed[] $params * @param iterable<Initializer> $preInitializers
* @param string $username * @param iterable<Initializer> $postInitializers
* @param string $password
* @param mixed[] $driverOptions
* *
* @throws MysqliException * @throws MysqliException
*/ */
public function __construct(array $params, $username, $password, array $driverOptions = []) public function __construct(
{ ?string $host = null,
$socket = $params['unix_socket'] ?? ini_get('mysqli.default_socket'); ?string $username = null,
$dbname = $params['dbname'] ?? null; ?string $password = null,
$port = $params['port'] ?? null; ?string $database = null,
?int $port = null,
if (! empty($params['persistent'])) { ?string $socket = null,
if (! isset($params['host'])) { ?int $flags = null,
throw HostRequired::forPersistentConnection(); iterable $preInitializers = [],
} iterable $postInitializers = []
) {
$host = 'p:' . $params['host']; $connection = mysqli_init();
} else {
$host = $params['host'] ?? null;
}
$flags = $driverOptions[static::OPTION_FLAGS] ?? null;
unset($driverOptions[static::OPTION_FLAGS]);
$this->conn = mysqli_init();
$preInitializers = $postInitializers = [];
$preInitializers = $this->withOptions($preInitializers, $driverOptions);
$preInitializers = $this->withSecure($preInitializers, $params);
$postInitializers = $this->withCharset($postInitializers, $params);
foreach ($preInitializers as $initializer) { foreach ($preInitializers as $initializer) {
$initializer->initialize($this->conn); $initializer->initialize($connection);
} }
if (! @$this->conn->real_connect($host, $username, $password, $dbname, $port, $socket, $flags)) { if (! @$connection->real_connect($host, $username, $password, $database, $port, $socket, $flags)) {
throw new MysqliException( throw new MysqliException(
$this->conn->connect_error, $connection->connect_error,
$this->conn->sqlstate ?? 'HY000', 'HY000',
$this->conn->connect_errno $connection->connect_errno
); );
} }
foreach ($postInitializers as $initializer) { foreach ($postInitializers as $initializer) {
$initializer->initialize($this->conn); $initializer->initialize($connection);
} }
$this->conn = $connection;
} }
/** /**
...@@ -192,61 +173,4 @@ class MysqliConnection implements PingableConnection, ServerInfoAwareConnection ...@@ -192,61 +173,4 @@ class MysqliConnection implements PingableConnection, ServerInfoAwareConnection
{ {
return $this->conn->ping(); return $this->conn->ping();
} }
/**
* @param list<Initializer> $initializers
* @param array<int,mixed> $options
*
* @return list<Initializer>
*/
private function withOptions(array $initializers, array $options): array
{
if (count($options) !== 0) {
$initializers[] = new Options($options);
}
return $initializers;
}
/**
* @param list<Initializer> $initializers
* @param array<string,mixed> $params
*
* @return list<Initializer>
*/
private function withSecure(array $initializers, array $params): array
{
if (
isset($params['ssl_key']) ||
isset($params['ssl_cert']) ||
isset($params['ssl_ca']) ||
isset($params['ssl_capath']) ||
isset($params['ssl_cipher'])
) {
$initializers[] = new Secure(
$params['ssl_key'] ?? null,
$params['ssl_cert'] ?? null,
$params['ssl_ca'] ?? null,
$params['ssl_capath'] ?? null,
$params['ssl_cipher'] ?? null
);
}
return $initializers;
}
/**
* @param list<Initializer> $initializers
* @param array<string,mixed> $params
*
* @return list<Initializer>
*/
private function withCharset(array $initializers, array $params): array
{
if (isset($params['charset'])) {
$initializers[] = new Charset($params['charset']);
}
return $initializers;
}
} }
...@@ -14,11 +14,11 @@ class Driver extends AbstractOracleDriver ...@@ -14,11 +14,11 @@ class Driver extends AbstractOracleDriver
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function connect(array $params, $username = null, $password = null, array $driverOptions = []) public function connect(array $params)
{ {
return new OCI8Connection( return new OCI8Connection(
(string) $username, $params['user'] ?? '',
(string) $password, $params['password'] ?? '',
$this->_constructDsn($params), $this->_constructDsn($params),
$params['charset'] ?? '', $params['charset'] ?? '',
$params['sessionMode'] ?? OCI_NO_AUTO_COMMIT, $params['sessionMode'] ?? OCI_NO_AUTO_COMMIT,
......
...@@ -14,16 +14,18 @@ class Driver extends AbstractMySQLDriver ...@@ -14,16 +14,18 @@ class Driver extends AbstractMySQLDriver
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function connect(array $params, $username = null, $password = null, array $driverOptions = []) public function connect(array $params)
{ {
$driverOptions = $params['driver_options'] ?? [];
if (! empty($params['persistent'])) { if (! empty($params['persistent'])) {
$driverOptions[PDO::ATTR_PERSISTENT] = true; $driverOptions[PDO::ATTR_PERSISTENT] = true;
} }
return new PDOConnection( return new PDOConnection(
$this->constructPdoDsn($params), $this->constructPdoDsn($params),
$username, $params['user'] ?? '',
$password, $params['password'] ?? '',
$driverOptions $driverOptions
); );
} }
......
...@@ -19,16 +19,18 @@ class Driver extends AbstractOracleDriver ...@@ -19,16 +19,18 @@ class Driver extends AbstractOracleDriver
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function connect(array $params, $username = null, $password = null, array $driverOptions = []) public function connect(array $params)
{ {
$driverOptions = $params['driver_options'] ?? [];
if (! empty($params['persistent'])) { if (! empty($params['persistent'])) {
$driverOptions[PDO::ATTR_PERSISTENT] = true; $driverOptions[PDO::ATTR_PERSISTENT] = true;
} }
return new PDOConnection( return new PDOConnection(
$this->constructPdoDsn($params), $this->constructPdoDsn($params),
$username, $params['user'] ?? '',
$password, $params['password'] ?? '',
$driverOptions $driverOptions
); );
} }
......
...@@ -16,17 +16,19 @@ class Driver extends AbstractPostgreSQLDriver ...@@ -16,17 +16,19 @@ class Driver extends AbstractPostgreSQLDriver
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function connect(array $params, $username = null, $password = null, array $driverOptions = []) public function connect(array $params)
{ {
$driverOptions = $params['driver_options'] ?? [];
if (! empty($params['persistent'])) { if (! empty($params['persistent'])) {
$driverOptions[PDO::ATTR_PERSISTENT] = true; $driverOptions[PDO::ATTR_PERSISTENT] = true;
} }
$connection = new PDOConnection( $connection = new PDOConnection(
$this->_constructPdoDsn($params), $this->_constructPdoDsn($params),
$username, $params['user'] ?? '',
$password, $params['password'] ?? '',
$driverOptions $driverOptions,
); );
if ( if (
......
...@@ -23,8 +23,10 @@ class Driver extends AbstractSQLiteDriver ...@@ -23,8 +23,10 @@ class Driver extends AbstractSQLiteDriver
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function connect(array $params, $username = null, $password = null, array $driverOptions = []) public function connect(array $params)
{ {
$driverOptions = $params['driver_options'] ?? [];
if (isset($driverOptions['userDefinedFunctions'])) { if (isset($driverOptions['userDefinedFunctions'])) {
$this->_userDefinedFunctions = array_merge( $this->_userDefinedFunctions = array_merge(
$this->_userDefinedFunctions, $this->_userDefinedFunctions,
...@@ -35,8 +37,8 @@ class Driver extends AbstractSQLiteDriver ...@@ -35,8 +37,8 @@ class Driver extends AbstractSQLiteDriver
$connection = new PDOConnection( $connection = new PDOConnection(
$this->_constructPdoDsn($params), $this->_constructPdoDsn($params),
$username, $params['user'] ?? '',
$password, $params['password'] ?? '',
$driverOptions $driverOptions
); );
......
...@@ -16,17 +16,19 @@ class Driver extends AbstractSQLServerDriver ...@@ -16,17 +16,19 @@ class Driver extends AbstractSQLServerDriver
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function connect(array $params, $username = null, $password = null, array $driverOptions = []) public function connect(array $params)
{ {
$pdoOptions = $dsnOptions = []; $pdoOptions = $dsnOptions = [];
foreach ($driverOptions as $option => $value) { if (isset($params['driver_options'])) {
foreach ($params['driver_options'] as $option => $value) {
if (is_int($option)) { if (is_int($option)) {
$pdoOptions[$option] = $value; $pdoOptions[$option] = $value;
} else { } else {
$dsnOptions[$option] = $value; $dsnOptions[$option] = $value;
} }
} }
}
if (! empty($params['persistent'])) { if (! empty($params['persistent'])) {
$pdoOptions[PDO::ATTR_PERSISTENT] = true; $pdoOptions[PDO::ATTR_PERSISTENT] = true;
...@@ -34,8 +36,8 @@ class Driver extends AbstractSQLServerDriver ...@@ -34,8 +36,8 @@ class Driver extends AbstractSQLServerDriver
return new Connection( return new Connection(
$this->_constructPdoDsn($params, $dsnOptions), $this->_constructPdoDsn($params, $dsnOptions),
$username, $params['user'] ?? '',
$password, $params['password'] ?? '',
$pdoOptions $pdoOptions
); );
} }
......
...@@ -12,7 +12,7 @@ class Driver extends AbstractSQLServerDriver ...@@ -12,7 +12,7 @@ class Driver extends AbstractSQLServerDriver
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function connect(array $params, $username = null, $password = null, array $driverOptions = []) public function connect(array $params)
{ {
if (! isset($params['host'])) { if (! isset($params['host'])) {
throw new SQLSrvException("Missing 'host' in configuration for sqlsrv driver."); throw new SQLSrvException("Missing 'host' in configuration for sqlsrv driver.");
...@@ -23,6 +23,8 @@ class Driver extends AbstractSQLServerDriver ...@@ -23,6 +23,8 @@ class Driver extends AbstractSQLServerDriver
$serverName .= ', ' . $params['port']; $serverName .= ', ' . $params['port'];
} }
$driverOptions = $params['driver_options'] ?? [];
if (isset($params['dbname'])) { if (isset($params['dbname'])) {
$driverOptions['Database'] = $params['dbname']; $driverOptions['Database'] = $params['dbname'];
} }
...@@ -31,12 +33,12 @@ class Driver extends AbstractSQLServerDriver ...@@ -31,12 +33,12 @@ class Driver extends AbstractSQLServerDriver
$driverOptions['CharacterSet'] = $params['charset']; $driverOptions['CharacterSet'] = $params['charset'];
} }
if ($username !== null) { if (isset($params['user'])) {
$driverOptions['UID'] = $username; $driverOptions['UID'] = $params['user'];
} }
if ($password !== null) { if (isset($params['password'])) {
$driverOptions['PWD'] = $password; $driverOptions['PWD'] = $params['password'];
} }
if (! isset($driverOptions['ReturnDatesAsStrings'])) { if (! isset($driverOptions['ReturnDatesAsStrings'])) {
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace Doctrine\DBAL\Tests\Driver\Mysqli; namespace Doctrine\DBAL\Tests\Driver\Mysqli;
use Doctrine\DBAL\Driver\Mysqli\Driver;
use Doctrine\DBAL\Driver\Mysqli\HostRequired; use Doctrine\DBAL\Driver\Mysqli\HostRequired;
use Doctrine\DBAL\Driver\Mysqli\MysqliConnection; use Doctrine\DBAL\Driver\Mysqli\MysqliConnection;
use Doctrine\DBAL\Platforms\MySqlPlatform; use Doctrine\DBAL\Platforms\MySqlPlatform;
...@@ -44,6 +45,6 @@ class MysqliConnectionTest extends FunctionalTestCase ...@@ -44,6 +45,6 @@ class MysqliConnectionTest extends FunctionalTestCase
public function testHostnameIsRequiredForPersistentConnection(): void public function testHostnameIsRequiredForPersistentConnection(): void
{ {
$this->expectException(HostRequired::class); $this->expectException(HostRequired::class);
new MysqliConnection(['persistent' => 'true'], '', ''); (new Driver())->connect(['persistent' => 'true']);
} }
} }
...@@ -3,13 +3,15 @@ ...@@ -3,13 +3,15 @@
namespace Doctrine\DBAL\Tests\Driver\PDOPgSql; namespace Doctrine\DBAL\Tests\Driver\PDOPgSql;
use Doctrine\DBAL\Driver as DriverInterface; use Doctrine\DBAL\Driver as DriverInterface;
use Doctrine\DBAL\Driver\PDOConnection; use Doctrine\DBAL\Driver\Connection;
use Doctrine\DBAL\Driver\PDOPgSql\Driver; use Doctrine\DBAL\Driver\PDOPgSql\Driver;
use Doctrine\DBAL\Tests\Driver\AbstractPostgreSQLDriverTest; use Doctrine\DBAL\Tests\Driver\AbstractPostgreSQLDriverTest;
use Doctrine\DBAL\Tests\TestUtil; use Doctrine\DBAL\Tests\TestUtil;
use PDO; use PDO;
use PDOException; use PDOException;
use function array_merge;
class DriverTest extends AbstractPostgreSQLDriverTest class DriverTest extends AbstractPostgreSQLDriverTest
{ {
public function testReturnsName(): void public function testReturnsName(): void
...@@ -92,15 +94,13 @@ class DriverTest extends AbstractPostgreSQLDriverTest ...@@ -92,15 +94,13 @@ class DriverTest extends AbstractPostgreSQLDriverTest
/** /**
* @param array<int,mixed> $driverOptions * @param array<int,mixed> $driverOptions
*/ */
private function connect(array $driverOptions): PDOConnection private function connect(array $driverOptions): Connection
{ {
$params = TestUtil::getConnectionParams();
return $this->createDriver()->connect( return $this->createDriver()->connect(
$params, array_merge(
$params['user'] ?? '', TestUtil::getConnectionParams(),
$params['password'] ?? '', ['driver_options' => $driverOptions]
$driverOptions )
); );
} }
} }
...@@ -31,10 +31,7 @@ abstract class AbstractDriverTest extends FunctionalTestCase ...@@ -31,10 +31,7 @@ abstract class AbstractDriverTest extends FunctionalTestCase
$params = $this->connection->getParams(); $params = $this->connection->getParams();
unset($params['dbname']); unset($params['dbname']);
$user = $params['user'] ?? null; $connection = $this->driver->connect($params);
$password = $params['password'] ?? null;
$connection = $this->driver->connect($params, $user, $password);
self::assertInstanceOf(DriverConnection::class, $connection); self::assertInstanceOf(DriverConnection::class, $connection);
} }
......
...@@ -8,6 +8,7 @@ use Doctrine\DBAL\Driver\Mysqli\MysqliException; ...@@ -8,6 +8,7 @@ use Doctrine\DBAL\Driver\Mysqli\MysqliException;
use Doctrine\DBAL\Tests\FunctionalTestCase; use Doctrine\DBAL\Tests\FunctionalTestCase;
use Doctrine\DBAL\Tests\TestUtil; use Doctrine\DBAL\Tests\TestUtil;
use function array_merge;
use function extension_loaded; use function extension_loaded;
use const MYSQLI_OPT_CONNECT_TIMEOUT; use const MYSQLI_OPT_CONNECT_TIMEOUT;
...@@ -73,11 +74,11 @@ class ConnectionTest extends FunctionalTestCase ...@@ -73,11 +74,11 @@ class ConnectionTest extends FunctionalTestCase
{ {
$params = TestUtil::getConnectionParams(); $params = TestUtil::getConnectionParams();
return new MysqliConnection( return (new Driver())->connect(
array_merge(
$params, $params,
$params['user'] ?? '', ['driver_options' => $driverOptions]
$params['password'] ?? '', )
$driverOptions
); );
} }
} }
...@@ -78,10 +78,7 @@ class DriverTest extends AbstractDriverTest ...@@ -78,10 +78,7 @@ class DriverTest extends AbstractDriverTest
$parameters = $this->connection->getParams(); $parameters = $this->connection->getParams();
$parameters['application_name'] = 'doctrine'; $parameters['application_name'] = 'doctrine';
$user = $parameters['user'] ?? null; $connection = $this->driver->connect($parameters);
$password = $parameters['password'] ?? null;
$connection = $this->driver->connect($parameters, $user, $password);
$hash = microtime(true); // required to identify the record in the results uniquely $hash = microtime(true); // required to identify the record in the results uniquely
$sql = sprintf('SELECT * FROM pg_stat_activity WHERE %d = %d', $hash, $hash); $sql = sprintf('SELECT * FROM pg_stat_activity WHERE %d = %d', $hash, $hash);
......
...@@ -10,8 +10,8 @@ use Doctrine\DBAL\Tests\Functional\Driver\AbstractDriverTest; ...@@ -10,8 +10,8 @@ use Doctrine\DBAL\Tests\Functional\Driver\AbstractDriverTest;
use Doctrine\DBAL\Tests\TestUtil; use Doctrine\DBAL\Tests\TestUtil;
use PDO; use PDO;
use function array_merge;
use function assert; use function assert;
use function extension_loaded; use function extension_loaded;
class DriverTest extends AbstractDriverTest class DriverTest extends AbstractDriverTest
...@@ -46,13 +46,11 @@ class DriverTest extends AbstractDriverTest ...@@ -46,13 +46,11 @@ class DriverTest extends AbstractDriverTest
*/ */
protected function getConnection(array $driverOptions): Connection protected function getConnection(array $driverOptions): Connection
{ {
$params = TestUtil::getConnectionParams();
return $this->connection->getDriver()->connect( return $this->connection->getDriver()->connect(
$params, array_merge(
$params['user'] ?? '', TestUtil::getConnectionParams(),
$params['password'] ?? '', ['driver_options' => $driverOptions]
$driverOptions )
); );
} }
......
...@@ -117,10 +117,7 @@ abstract class SchemaManagerFunctionalTestCase extends FunctionalTestCase ...@@ -117,10 +117,7 @@ abstract class SchemaManagerFunctionalTestCase extends FunctionalTestCase
$params['dbname'] = 'test_drop_database'; $params['dbname'] = 'test_drop_database';
} }
$user = $params['user'] ?? null; $connection = $this->connection->getDriver()->connect($params);
$password = $params['password'] ?? null;
$connection = $this->connection->getDriver()->connect($params, $user, $password);
self::assertInstanceOf(Connection::class, $connection); self::assertInstanceOf(Connection::class, $connection);
......
...@@ -50,10 +50,7 @@ class SqliteSchemaManagerTest extends SchemaManagerFunctionalTestCase ...@@ -50,10 +50,7 @@ class SqliteSchemaManagerTest extends SchemaManagerFunctionalTestCase
$params = $this->connection->getParams(); $params = $this->connection->getParams();
$params['dbname'] = 'test_drop_database'; $params['dbname'] = 'test_drop_database';
$user = $params['user'] ?? null; $connection = $this->connection->getDriver()->connect($params);
$password = $params['password'] ?? null;
$connection = $this->connection->getDriver()->connect($params, $user, $password);
self::assertInstanceOf(Connection::class, $connection); self::assertInstanceOf(Connection::class, $connection);
......
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