diff --git a/UPGRADE.md b/UPGRADE.md index 6c832deb1e7..c945adf5d0d 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -1,5 +1,9 @@ # 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` This class was deprecated in favor of `PrimaryReadReplicaConnection` diff --git a/phpcs.xml.dist b/phpcs.xml.dist index a9607273e9d..c7917559c5f 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -83,11 +83,12 @@ src/Driver/IBMDB2/DB2Connection.php + src/Driver/Mysqli/MysqliConnection.php src/SQLParserUtils.php src/Tools/Dumper.php diff --git a/src/Connection.php b/src/Connection.php index 4f741bb3c3d..f07d9f49151 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -352,12 +352,8 @@ public function connect() return false; } - $driverOptions = $this->params['driverOptions'] ?? []; - $user = $this->params['user'] ?? null; - $password = $this->params['password'] ?? null; - try { - $this->_conn = $this->_driver->connect($this->params, $user, $password, $driverOptions); + $this->_conn = $this->_driver->connect($this->params); } catch (DriverException $e) { throw DBALException::driverException($this->_driver, $e); } diff --git a/src/Connections/PrimaryReadReplicaConnection.php b/src/Connections/PrimaryReadReplicaConnection.php index 8e3c6ee136b..d1d9810d965 100644 --- a/src/Connections/PrimaryReadReplicaConnection.php +++ b/src/Connections/PrimaryReadReplicaConnection.php @@ -225,15 +225,10 @@ protected function connectTo($connectionName) { $params = $this->getParams(); - $driverOptions = $params['driverOptions'] ?? []; - $connectionParams = $this->chooseConnectionConfiguration($connectionName, $params); - $user = $connectionParams['user'] ?? null; - $password = $connectionParams['password'] ?? null; - try { - return $this->_driver->connect($connectionParams, $user, $password, $driverOptions); + return $this->_driver->connect($connectionParams); } catch (DriverException $e) { throw DBALException::driverException($this->_driver, $e); } diff --git a/src/Driver.php b/src/Driver.php index f0b300aee98..ec8fbfb7022 100644 --- a/src/Driver.php +++ b/src/Driver.php @@ -2,6 +2,7 @@ namespace Doctrine\DBAL; +use Doctrine\DBAL\Driver\Connection as DriverConnection; use Doctrine\DBAL\Driver\DriverException; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Schema\AbstractSchemaManager; @@ -15,18 +16,13 @@ interface Driver /** * 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. - * @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. + * @return DriverConnection The database connection. * * @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 diff --git a/src/Driver/IBMDB2/DB2Connection.php b/src/Driver/IBMDB2/DB2Connection.php index c6a120cc916..1e8bdf51a05 100644 --- a/src/Driver/IBMDB2/DB2Connection.php +++ b/src/Driver/IBMDB2/DB2Connection.php @@ -33,21 +33,21 @@ class DB2Connection implements ServerInfoAwareConnection private $conn = null; /** - * @param mixed[] $params - * @param string $username - * @param string $password - * @param mixed[] $driverOptions + * @param array $driverOptions * * @throws DB2Exception */ - public function __construct(array $params, $username, $password, $driverOptions = []) - { - $isPersistent = (isset($params['persistent']) && $params['persistent'] === true); - - if ($isPersistent) { - $conn = db2_pconnect($params['dbname'], $username, $password, $driverOptions); + public function __construct( + string $database, + bool $persistent, + string $username, + string $password, + array $driverOptions = [] + ) { + if ($persistent) { + $conn = db2_pconnect($database, $username, $password, $driverOptions); } else { - $conn = db2_connect($params['dbname'], $username, $password, $driverOptions); + $conn = db2_connect($database, $username, $password, $driverOptions); } if ($conn === false) { diff --git a/src/Driver/IBMDB2/DB2Driver.php b/src/Driver/IBMDB2/DB2Driver.php index 650d75ceac7..4712e144aef 100644 --- a/src/Driver/IBMDB2/DB2Driver.php +++ b/src/Driver/IBMDB2/DB2Driver.php @@ -12,17 +12,14 @@ class DB2Driver extends AbstractDB2Driver /** * {@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( - $params, - (string) $username, - (string) $password, - $driverOptions + DataSourceName::fromConnectionParameters($params)->toString(), + isset($params['persistent']) && $params['persistent'] === true, + $params['user'] ?? '', + $params['password'] ?? '', + $params['driver_options'] ?? [] ); } diff --git a/src/Driver/Mysqli/Driver.php b/src/Driver/Mysqli/Driver.php index 4430482dfc9..c13a3f81709 100644 --- a/src/Driver/Mysqli/Driver.php +++ b/src/Driver/Mysqli/Driver.php @@ -3,15 +3,115 @@ namespace Doctrine\DBAL\Driver\Mysqli; 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 { /** * {@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 $initializers + * @param array $options + * + * @return list + */ + 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 $initializers + * @param array $params + * + * @return list + */ + 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 $initializers + * @param array $params + * + * @return list + */ + private function withCharset(array $initializers, array $params): array + { + if (isset($params['charset'])) { + $initializers[] = new Charset($params['charset']); + } + + return $initializers; } /** diff --git a/src/Driver/Mysqli/Exception/InvalidCharset.php b/src/Driver/Mysqli/Exception/InvalidCharset.php new file mode 100644 index 00000000000..e866f200523 --- /dev/null +++ b/src/Driver/Mysqli/Exception/InvalidCharset.php @@ -0,0 +1,27 @@ +error), + $connection->sqlstate, + $connection->errno + ); + } +} diff --git a/src/Driver/Mysqli/Exception/InvalidOption.php b/src/Driver/Mysqli/Exception/InvalidOption.php new file mode 100644 index 00000000000..210ce09259c --- /dev/null +++ b/src/Driver/Mysqli/Exception/InvalidOption.php @@ -0,0 +1,27 @@ +charset = $charset; + } + + public function initialize(mysqli $connection): void + { + if ($connection->set_charset($this->charset)) { + return; + } + + throw InvalidCharset::fromCharset($connection, $this->charset); + } +} diff --git a/src/Driver/Mysqli/Initializer/Options.php b/src/Driver/Mysqli/Initializer/Options.php new file mode 100644 index 00000000000..bcf2fc2d117 --- /dev/null +++ b/src/Driver/Mysqli/Initializer/Options.php @@ -0,0 +1,34 @@ + */ + private $options; + + /** + * @param array $options + */ + public function __construct(array $options) + { + $this->options = $options; + } + + public function initialize(mysqli $connection): void + { + foreach ($this->options as $option => $value) { + if (! mysqli_options($connection, $option, $value)) { + throw InvalidOption::fromOption($option, $value); + } + } + } +} diff --git a/src/Driver/Mysqli/Initializer/Secure.php b/src/Driver/Mysqli/Initializer/Secure.php new file mode 100644 index 00000000000..c8b2a558e4c --- /dev/null +++ b/src/Driver/Mysqli/Initializer/Secure.php @@ -0,0 +1,40 @@ +key = $key; + $this->cert = $cert; + $this->ca = $ca; + $this->capath = $capath; + $this->cipher = $cipher; + } + + public function initialize(mysqli $connection): void + { + $connection->ssl_set($this->key, $this->cert, $this->ca, $this->capath, $this->cipher); + } +} diff --git a/src/Driver/Mysqli/MysqliConnection.php b/src/Driver/Mysqli/MysqliConnection.php index 6da2e729152..de3e3c71c4c 100644 --- a/src/Driver/Mysqli/MysqliConnection.php +++ b/src/Driver/Mysqli/MysqliConnection.php @@ -9,26 +9,10 @@ use Doctrine\DBAL\ParameterType; use mysqli; -use function defined; use function floor; -use function in_array; -use function ini_get; -use function mysqli_errno; -use function mysqli_error; use function mysqli_init; -use function mysqli_options; -use function restore_error_handler; -use function set_error_handler; -use function sprintf; use function stripos; -use const MYSQLI_INIT_COMMAND; -use const MYSQLI_OPT_CONNECT_TIMEOUT; -use const MYSQLI_OPT_LOCAL_INFILE; -use const MYSQLI_READ_DEFAULT_FILE; -use const MYSQLI_READ_DEFAULT_GROUP; -use const MYSQLI_SERVER_PUBLIC_KEY; - class MysqliConnection implements PingableConnection, ServerInfoAwareConnection { /** @@ -40,53 +24,41 @@ class MysqliConnection implements PingableConnection, ServerInfoAwareConnection private $conn; /** - * @param mixed[] $params - * @param string $username - * @param string $password - * @param mixed[] $driverOptions + * @param iterable $preInitializers + * @param iterable $postInitializers * * @throws MysqliException */ - public function __construct(array $params, $username, $password, array $driverOptions = []) - { - $socket = $params['unix_socket'] ?? ini_get('mysqli.default_socket'); - $dbname = $params['dbname'] ?? null; - $port = $params['port'] ?? null; - - if (! empty($params['persistent'])) { - if (! isset($params['host'])) { - throw HostRequired::forPersistentConnection(); - } - - $host = 'p:' . $params['host']; - } else { - $host = $params['host'] ?? null; + public function __construct( + ?string $host = null, + ?string $username = null, + ?string $password = null, + ?string $database = null, + ?int $port = null, + ?string $socket = null, + ?int $flags = null, + iterable $preInitializers = [], + iterable $postInitializers = [] + ) { + $connection = mysqli_init(); + + foreach ($preInitializers as $initializer) { + $initializer->initialize($connection); } - $flags = $driverOptions[static::OPTION_FLAGS] ?? null; - - $this->conn = mysqli_init(); - - $this->setSecureConnection($params); - $this->setDriverOptions($driverOptions); - - set_error_handler(static function (): bool { - return true; - }); - - try { - if (! $this->conn->real_connect($host, $username, $password, $dbname, $port, $socket, $flags)) { - throw new MysqliException($this->conn->connect_error, $this->conn->sqlstate ?? 'HY000', $this->conn->connect_errno); - } - } finally { - restore_error_handler(); + if (! @$connection->real_connect($host, $username, $password, $database, $port, $socket, $flags)) { + throw new MysqliException( + $connection->connect_error, + 'HY000', + $connection->connect_errno + ); } - if (! isset($params['charset'])) { - return; + foreach ($postInitializers as $initializer) { + $initializer->initialize($connection); } - $this->conn->set_charset($params['charset']); + $this->conn = $connection; } /** @@ -192,56 +164,6 @@ public function rollBack() return $this->conn->rollback(); } - /** - * Apply the driver options to the connection. - * - * @param mixed[] $driverOptions - * - * @throws MysqliException When one of of the options is not supported. - * @throws MysqliException When applying doesn't work - e.g. due to incorrect value. - */ - private function setDriverOptions(array $driverOptions = []): void - { - $supportedDriverOptions = [ - MYSQLI_OPT_CONNECT_TIMEOUT, - MYSQLI_OPT_LOCAL_INFILE, - MYSQLI_INIT_COMMAND, - MYSQLI_READ_DEFAULT_FILE, - MYSQLI_READ_DEFAULT_GROUP, - ]; - - if (defined('MYSQLI_SERVER_PUBLIC_KEY')) { - $supportedDriverOptions[] = MYSQLI_SERVER_PUBLIC_KEY; - } - - $exceptionMsg = "%s option '%s' with value '%s'"; - - foreach ($driverOptions as $option => $value) { - if ($option === static::OPTION_FLAGS) { - continue; - } - - if (! in_array($option, $supportedDriverOptions, true)) { - throw new MysqliException( - sprintf($exceptionMsg, 'Unsupported', $option, $value) - ); - } - - if (@mysqli_options($this->conn, $option, $value)) { - continue; - } - - $msg = sprintf($exceptionMsg, 'Failed to set', $option, $value); - $msg .= sprintf(', error: %s (%d)', mysqli_error($this->conn), mysqli_errno($this->conn)); - - throw new MysqliException( - $msg, - $this->conn->sqlstate, - $this->conn->errno - ); - } - } - /** * Pings the server and re-connects when `mysqli.reconnect = 1` * @@ -251,32 +173,4 @@ public function ping() { return $this->conn->ping(); } - - /** - * Establish a secure connection - * - * @param mixed[] $params - * - * @throws MysqliException - */ - private function setSecureConnection(array $params): void - { - if ( - ! isset($params['ssl_key']) && - ! isset($params['ssl_cert']) && - ! isset($params['ssl_ca']) && - ! isset($params['ssl_capath']) && - ! isset($params['ssl_cipher']) - ) { - return; - } - - $this->conn->ssl_set( - $params['ssl_key'] ?? null, - $params['ssl_cert'] ?? null, - $params['ssl_ca'] ?? null, - $params['ssl_capath'] ?? null, - $params['ssl_cipher'] ?? null - ); - } } diff --git a/src/Driver/OCI8/Driver.php b/src/Driver/OCI8/Driver.php index e2df60e2d52..88436af21ff 100644 --- a/src/Driver/OCI8/Driver.php +++ b/src/Driver/OCI8/Driver.php @@ -14,11 +14,11 @@ class Driver extends AbstractOracleDriver /** * {@inheritdoc} */ - public function connect(array $params, $username = null, $password = null, array $driverOptions = []) + public function connect(array $params) { return new OCI8Connection( - (string) $username, - (string) $password, + $params['user'] ?? '', + $params['password'] ?? '', $this->_constructDsn($params), $params['charset'] ?? '', $params['sessionMode'] ?? OCI_NO_AUTO_COMMIT, diff --git a/src/Driver/PDOMySql/Driver.php b/src/Driver/PDOMySql/Driver.php index 3beb43bc7a1..6d65a2b6686 100644 --- a/src/Driver/PDOMySql/Driver.php +++ b/src/Driver/PDOMySql/Driver.php @@ -14,16 +14,18 @@ class Driver extends AbstractMySQLDriver /** * {@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'])) { $driverOptions[PDO::ATTR_PERSISTENT] = true; } return new PDOConnection( $this->constructPdoDsn($params), - $username, - $password, + $params['user'] ?? '', + $params['password'] ?? '', $driverOptions ); } diff --git a/src/Driver/PDOOracle/Driver.php b/src/Driver/PDOOracle/Driver.php index 3267b6bf823..c3bba5501f2 100644 --- a/src/Driver/PDOOracle/Driver.php +++ b/src/Driver/PDOOracle/Driver.php @@ -19,16 +19,18 @@ class Driver extends AbstractOracleDriver /** * {@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'])) { $driverOptions[PDO::ATTR_PERSISTENT] = true; } return new PDOConnection( $this->constructPdoDsn($params), - $username, - $password, + $params['user'] ?? '', + $params['password'] ?? '', $driverOptions ); } diff --git a/src/Driver/PDOPgSql/Driver.php b/src/Driver/PDOPgSql/Driver.php index cd352fc636f..fbeb0b5ee11 100644 --- a/src/Driver/PDOPgSql/Driver.php +++ b/src/Driver/PDOPgSql/Driver.php @@ -16,17 +16,19 @@ class Driver extends AbstractPostgreSQLDriver /** * {@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'])) { $driverOptions[PDO::ATTR_PERSISTENT] = true; } $connection = new PDOConnection( $this->_constructPdoDsn($params), - $username, - $password, - $driverOptions + $params['user'] ?? '', + $params['password'] ?? '', + $driverOptions, ); if ( diff --git a/src/Driver/PDOSqlite/Driver.php b/src/Driver/PDOSqlite/Driver.php index 779c2576cb8..75bad95a9d6 100644 --- a/src/Driver/PDOSqlite/Driver.php +++ b/src/Driver/PDOSqlite/Driver.php @@ -23,8 +23,10 @@ class Driver extends AbstractSQLiteDriver /** * {@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'])) { $this->_userDefinedFunctions = array_merge( $this->_userDefinedFunctions, @@ -35,8 +37,8 @@ public function connect(array $params, $username = null, $password = null, array $connection = new PDOConnection( $this->_constructPdoDsn($params), - $username, - $password, + $params['user'] ?? '', + $params['password'] ?? '', $driverOptions ); diff --git a/src/Driver/PDOSqlsrv/Driver.php b/src/Driver/PDOSqlsrv/Driver.php index e3aa4e1066b..aa94dcf4491 100644 --- a/src/Driver/PDOSqlsrv/Driver.php +++ b/src/Driver/PDOSqlsrv/Driver.php @@ -16,15 +16,17 @@ class Driver extends AbstractSQLServerDriver /** * {@inheritdoc} */ - public function connect(array $params, $username = null, $password = null, array $driverOptions = []) + public function connect(array $params) { $pdoOptions = $dsnOptions = []; - foreach ($driverOptions as $option => $value) { - if (is_int($option)) { - $pdoOptions[$option] = $value; - } else { - $dsnOptions[$option] = $value; + if (isset($params['driver_options'])) { + foreach ($params['driver_options'] as $option => $value) { + if (is_int($option)) { + $pdoOptions[$option] = $value; + } else { + $dsnOptions[$option] = $value; + } } } @@ -34,8 +36,8 @@ public function connect(array $params, $username = null, $password = null, array return new Connection( $this->_constructPdoDsn($params, $dsnOptions), - $username, - $password, + $params['user'] ?? '', + $params['password'] ?? '', $pdoOptions ); } diff --git a/src/Driver/SQLSrv/Driver.php b/src/Driver/SQLSrv/Driver.php index 848ab5eef91..91a98cd902a 100644 --- a/src/Driver/SQLSrv/Driver.php +++ b/src/Driver/SQLSrv/Driver.php @@ -12,7 +12,7 @@ class Driver extends AbstractSQLServerDriver /** * {@inheritdoc} */ - public function connect(array $params, $username = null, $password = null, array $driverOptions = []) + public function connect(array $params) { if (! isset($params['host'])) { throw new SQLSrvException("Missing 'host' in configuration for sqlsrv driver."); @@ -23,6 +23,8 @@ public function connect(array $params, $username = null, $password = null, array $serverName .= ', ' . $params['port']; } + $driverOptions = $params['driver_options'] ?? []; + if (isset($params['dbname'])) { $driverOptions['Database'] = $params['dbname']; } @@ -31,12 +33,12 @@ public function connect(array $params, $username = null, $password = null, array $driverOptions['CharacterSet'] = $params['charset']; } - if ($username !== null) { - $driverOptions['UID'] = $username; + if (isset($params['user'])) { + $driverOptions['UID'] = $params['user']; } - if ($password !== null) { - $driverOptions['PWD'] = $password; + if (isset($params['password'])) { + $driverOptions['PWD'] = $params['password']; } if (! isset($driverOptions['ReturnDatesAsStrings'])) { diff --git a/tests/Driver/Mysqli/MysqliConnectionTest.php b/tests/Driver/Mysqli/MysqliConnectionTest.php index 0e36890a2ed..5791fd5d859 100644 --- a/tests/Driver/Mysqli/MysqliConnectionTest.php +++ b/tests/Driver/Mysqli/MysqliConnectionTest.php @@ -2,16 +2,14 @@ namespace Doctrine\DBAL\Tests\Driver\Mysqli; +use Doctrine\DBAL\Driver\Mysqli\Driver; use Doctrine\DBAL\Driver\Mysqli\HostRequired; use Doctrine\DBAL\Driver\Mysqli\MysqliConnection; -use Doctrine\DBAL\Driver\Mysqli\MysqliException; use Doctrine\DBAL\Platforms\MySqlPlatform; use Doctrine\DBAL\Tests\FunctionalTestCase; use PHPUnit\Framework\MockObject\MockObject; use function extension_loaded; -use function restore_error_handler; -use function set_error_handler; class MysqliConnectionTest extends FunctionalTestCase { @@ -44,29 +42,9 @@ public function testDoesNotRequireQueryForServerVersion(): void self::assertFalse($this->connectionMock->requiresQueryForServerVersion()); } - public function testRestoresErrorHandlerOnException(): void - { - $handler = static function (): bool { - self::fail('Never expected this to be called'); - }; - - $defaultHandler = set_error_handler($handler); - - try { - new MysqliConnection(['host' => '255.255.255.255'], 'user', 'pass'); - self::fail('An exception was supposed to be raised'); - } catch (MysqliException $e) { - self::assertSame('Network is unreachable', $e->getMessage()); - } - - self::assertSame($handler, set_error_handler($defaultHandler), 'Restoring error handler failed.'); - restore_error_handler(); - restore_error_handler(); - } - public function testHostnameIsRequiredForPersistentConnection(): void { $this->expectException(HostRequired::class); - new MysqliConnection(['persistent' => 'true'], '', ''); + (new Driver())->connect(['persistent' => 'true']); } } diff --git a/tests/Driver/PDOPgSql/DriverTest.php b/tests/Driver/PDOPgSql/DriverTest.php index dda472dd950..1cd737e804c 100644 --- a/tests/Driver/PDOPgSql/DriverTest.php +++ b/tests/Driver/PDOPgSql/DriverTest.php @@ -3,13 +3,15 @@ namespace Doctrine\DBAL\Tests\Driver\PDOPgSql; 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\Tests\Driver\AbstractPostgreSQLDriverTest; use Doctrine\DBAL\Tests\TestUtil; use PDO; use PDOException; +use function array_merge; + class DriverTest extends AbstractPostgreSQLDriverTest { public function testReturnsName(): void @@ -92,15 +94,13 @@ private function skipWhenNotUsingPdoPgsql(): void /** * @param array $driverOptions */ - private function connect(array $driverOptions): PDOConnection + private function connect(array $driverOptions): Connection { - $params = TestUtil::getConnectionParams(); - return $this->createDriver()->connect( - $params, - $params['user'] ?? '', - $params['password'] ?? '', - $driverOptions + array_merge( + TestUtil::getConnectionParams(), + ['driver_options' => $driverOptions] + ) ); } } diff --git a/tests/Functional/Driver/AbstractDriverTest.php b/tests/Functional/Driver/AbstractDriverTest.php index 7ac7709fdea..86e2a73b755 100644 --- a/tests/Functional/Driver/AbstractDriverTest.php +++ b/tests/Functional/Driver/AbstractDriverTest.php @@ -31,10 +31,7 @@ public function testConnectsWithoutDatabaseNameParameter(): void $params = $this->connection->getParams(); unset($params['dbname']); - $user = $params['user'] ?? null; - $password = $params['password'] ?? null; - - $connection = $this->driver->connect($params, $user, $password); + $connection = $this->driver->connect($params); self::assertInstanceOf(DriverConnection::class, $connection); } diff --git a/tests/Functional/Driver/Mysqli/ConnectionTest.php b/tests/Functional/Driver/Mysqli/ConnectionTest.php index c54aae250bd..435a612b69c 100644 --- a/tests/Functional/Driver/Mysqli/ConnectionTest.php +++ b/tests/Functional/Driver/Mysqli/ConnectionTest.php @@ -8,6 +8,7 @@ use Doctrine\DBAL\Tests\FunctionalTestCase; use Doctrine\DBAL\Tests\TestUtil; +use function array_merge; use function extension_loaded; use const MYSQLI_OPT_CONNECT_TIMEOUT; @@ -44,7 +45,20 @@ public function testUnsupportedDriverOption(): void { $this->expectException(MysqliException::class); - $this->getConnection(['hello' => 'world']); // use local infile + $this->getConnection([12345 => 'world']); + } + + public function testInvalidCharset(): void + { + $params = TestUtil::getConnectionParams(); + + $this->expectException(MysqliException::class); + (new Driver())->connect( + array_merge( + $params, + ['charset' => 'invalid'] + ) + ); } public function testPing(): void @@ -60,11 +74,11 @@ private function getConnection(array $driverOptions): MysqliConnection { $params = TestUtil::getConnectionParams(); - return new MysqliConnection( - $params, - $params['user'] ?? '', - $params['password'] ?? '', - $driverOptions + return (new Driver())->connect( + array_merge( + $params, + ['driver_options' => $driverOptions] + ) ); } } diff --git a/tests/Functional/Driver/PDOPgSql/DriverTest.php b/tests/Functional/Driver/PDOPgSql/DriverTest.php index e5db1a085c2..02901d16301 100644 --- a/tests/Functional/Driver/PDOPgSql/DriverTest.php +++ b/tests/Functional/Driver/PDOPgSql/DriverTest.php @@ -78,10 +78,7 @@ public function testConnectsWithApplicationNameParameter(): void $parameters = $this->connection->getParams(); $parameters['application_name'] = 'doctrine'; - $user = $parameters['user'] ?? null; - $password = $parameters['password'] ?? null; - - $connection = $this->driver->connect($parameters, $user, $password); + $connection = $this->driver->connect($parameters); $hash = microtime(true); // required to identify the record in the results uniquely $sql = sprintf('SELECT * FROM pg_stat_activity WHERE %d = %d', $hash, $hash); diff --git a/tests/Functional/Driver/PDOSqlsrv/DriverTest.php b/tests/Functional/Driver/PDOSqlsrv/DriverTest.php index f568ff35c8f..65006c69c44 100644 --- a/tests/Functional/Driver/PDOSqlsrv/DriverTest.php +++ b/tests/Functional/Driver/PDOSqlsrv/DriverTest.php @@ -10,8 +10,8 @@ use Doctrine\DBAL\Tests\TestUtil; use PDO; +use function array_merge; use function assert; - use function extension_loaded; class DriverTest extends AbstractDriverTest @@ -46,13 +46,11 @@ protected static function getDatabaseNameForConnectionWithoutDatabaseNameParamet */ protected function getConnection(array $driverOptions): Connection { - $params = TestUtil::getConnectionParams(); - return $this->connection->getDriver()->connect( - $params, - $params['user'] ?? '', - $params['password'] ?? '', - $driverOptions + array_merge( + TestUtil::getConnectionParams(), + ['driver_options' => $driverOptions] + ) ); } diff --git a/tests/Functional/Schema/SchemaManagerFunctionalTestCase.php b/tests/Functional/Schema/SchemaManagerFunctionalTestCase.php index aa92bf9ab0d..f0ed94d17a5 100644 --- a/tests/Functional/Schema/SchemaManagerFunctionalTestCase.php +++ b/tests/Functional/Schema/SchemaManagerFunctionalTestCase.php @@ -117,10 +117,7 @@ public function testDropsDatabaseWithActiveConnections(): void $params['dbname'] = 'test_drop_database'; } - $user = $params['user'] ?? null; - $password = $params['password'] ?? null; - - $connection = $this->connection->getDriver()->connect($params, $user, $password); + $connection = $this->connection->getDriver()->connect($params); self::assertInstanceOf(Connection::class, $connection); diff --git a/tests/Functional/Schema/SqliteSchemaManagerTest.php b/tests/Functional/Schema/SqliteSchemaManagerTest.php index 0ea1a4e4433..5cb5c8522c3 100644 --- a/tests/Functional/Schema/SqliteSchemaManagerTest.php +++ b/tests/Functional/Schema/SqliteSchemaManagerTest.php @@ -50,10 +50,7 @@ public function testDropsDatabaseWithActiveConnections(): void $params = $this->connection->getParams(); $params['dbname'] = 'test_drop_database'; - $user = $params['user'] ?? null; - $password = $params['password'] ?? null; - - $connection = $this->connection->getDriver()->connect($params, $user, $password); + $connection = $this->connection->getDriver()->connect($params); self::assertInstanceOf(Connection::class, $connection);