Unverified Commit f90c5001 authored by Sergei Morozov's avatar Sergei Morozov

Merge pull request #3568 from morozov/connection-types

Enforced parameter and return value types in Connection classes
parents ab8d709f 88f7feae
# Upgrade to 3.0
## BC BREAK: Changes in the `Doctrine\DBAL\Connection` API
- The following methods have been removed as leaking internal implementation details: `::getHost()`, `::getPort()`, `::getUsername()`, `::getPassword()`.
- The `::getDatabase()` method can now return null which means that no database is currently selected.
## BC BREAK: Changes in `Doctrine\DBAL\Driver\SQLSrv\LastInsertId`
- The class stores the last inserted ID as a nullable string, not an integer, which is reflected in the method signatures.
## BC BREAK: Changes in the `Doctrine\DBAL\Schema` API
- Method `Doctrine\DBAL\Schema\AbstractSchemaManager::_getPortableViewDefinition()` no longer optionally returns false. It will always return a `Doctrine\DBAL\Schema\View` instance.
......
This diff is collapsed.
......@@ -67,14 +67,15 @@ use function count;
* )
* ));
*
* You can also pass 'driverOptions' and any other documented option to each of this drivers to pass additional information.
* You can also pass 'driverOptions' and any other documented option to each of this drivers
* to pass additional information.
*/
class MasterSlaveConnection extends Connection
{
/**
* Master and slave connection (one of the randomly picked slaves).
*
* @var DriverConnection[]|null[]
* @var array<string, DriverConnection|null>
*/
protected $connections = ['master' => null, 'slave' => null];
......@@ -89,12 +90,16 @@ class MasterSlaveConnection extends Connection
/**
* Creates Master Slave Connection.
*
* @param mixed[] $params
* @param array<string, mixed> $params
*
* @throws InvalidArgumentException
*/
public function __construct(array $params, Driver $driver, ?Configuration $config = null, ?EventManager $eventManager = null)
{
public function __construct(
array $params,
Driver $driver,
?Configuration $config = null,
?EventManager $eventManager = null
) {
if (! isset($params['slaves'], $params['master'])) {
throw new InvalidArgumentException('master or slaves configuration missing');
}
......@@ -114,10 +119,8 @@ class MasterSlaveConnection extends Connection
/**
* Checks if the connection is currently towards the master or not.
*
* @return bool
*/
public function isConnectedToMaster()
public function isConnectedToMaster() : bool
{
return $this->_conn !== null && $this->_conn === $this->connections['master'];
}
......@@ -179,12 +182,8 @@ class MasterSlaveConnection extends Connection
/**
* Connects to a specific connection.
*
* @param string $connectionName
*
* @return DriverConnection
*/
protected function connectTo($connectionName)
protected function connectTo(string $connectionName) : DriverConnection
{
$params = $this->getParams();
......@@ -199,12 +198,11 @@ class MasterSlaveConnection extends Connection
}
/**
* @param string $connectionName
* @param mixed[] $params
* @param array<string, mixed> $params
*
* @return mixed
* @return array<string, mixed>
*/
protected function chooseConnectionConfiguration($connectionName, $params)
protected function chooseConnectionConfiguration(string $connectionName, array $params) : array
{
if ($connectionName === 'master') {
return $params['master'];
......@@ -262,17 +260,17 @@ class MasterSlaveConnection extends Connection
/**
* {@inheritDoc}
*/
public function delete($tableName, array $identifier, array $types = [])
public function delete(string $table, array $identifier, array $types = []) : int
{
$this->connect('master');
return parent::delete($tableName, $identifier, $types);
return parent::delete($table, $identifier, $types);
}
/**
* {@inheritDoc}
*/
public function close()
public function close() : void
{
unset($this->connections['master'], $this->connections['slave']);
......@@ -285,21 +283,21 @@ class MasterSlaveConnection extends Connection
/**
* {@inheritDoc}
*/
public function update($tableName, array $data, array $identifier, array $types = [])
public function update(string $table, array $data, array $identifier, array $types = []) : int
{
$this->connect('master');
return parent::update($tableName, $data, $identifier, $types);
return parent::update($table, $data, $identifier, $types);
}
/**
* {@inheritDoc}
*/
public function insert($tableName, array $data, array $types = [])
public function insert(string $table, array $data, array $types = []) : int
{
$this->connect('master');
return parent::insert($tableName, $data, $types);
return parent::insert($table, $data, $types);
}
/**
......@@ -315,7 +313,7 @@ class MasterSlaveConnection extends Connection
/**
* {@inheritDoc}
*/
public function createSavepoint($savepoint)
public function createSavepoint(string $savepoint) : void
{
$this->connect('master');
......@@ -325,7 +323,7 @@ class MasterSlaveConnection extends Connection
/**
* {@inheritDoc}
*/
public function releaseSavepoint($savepoint)
public function releaseSavepoint(string $savepoint) : void
{
$this->connect('master');
......@@ -335,7 +333,7 @@ class MasterSlaveConnection extends Connection
/**
* {@inheritDoc}
*/
public function rollbackSavepoint($savepoint)
public function rollbackSavepoint(string $savepoint) : void
{
$this->connect('master');
......
......@@ -48,7 +48,7 @@ interface Driver
/**
* Gets the name of the database connected to for this driver.
*
* @return string The name of the database or NULL if no database is currently selected.
* @return string|null The name of the database or NULL if no database is currently selected.
*/
public function getDatabase(Connection $conn) : ?string;
}
......@@ -40,12 +40,8 @@ interface Connection
/**
* Returns the ID of the last inserted row or sequence value.
*
* @param string|null $name
*
* @return string
*/
public function lastInsertId($name = null);
public function lastInsertId(?string $name = null) : string;
/**
* Initiates a transaction.
......
......@@ -29,18 +29,14 @@ class DB2Connection implements Connection, ServerInfoAwareConnection
private $conn = null;
/**
* @param mixed[] $params
* @param string $username
* @param string $password
* @param mixed[] $driverOptions
* @param array<string, mixed> $params
* @param array<string, mixed> $driverOptions
*
* @throws DB2Exception
*/
public function __construct(array $params, $username, $password, $driverOptions = [])
public function __construct(array $params, string $username, string $password, array $driverOptions = [])
{
$isPersistent = (isset($params['persistent']) && $params['persistent'] === true);
if ($isPersistent) {
if (isset($params['persistent']) && $params['persistent'] === true) {
$conn = db2_pconnect($params['dbname'], $username, $password, $driverOptions);
} else {
$conn = db2_connect($params['dbname'], $username, $password, $driverOptions);
......@@ -56,7 +52,7 @@ class DB2Connection implements Connection, ServerInfoAwareConnection
/**
* {@inheritdoc}
*/
public function getServerVersion()
public function getServerVersion() : string
{
/** @var stdClass $serverInfo */
$serverInfo = db2_server_info($this->conn);
......@@ -67,7 +63,7 @@ class DB2Connection implements Connection, ServerInfoAwareConnection
/**
* {@inheritdoc}
*/
public function requiresQueryForServerVersion()
public function requiresQueryForServerVersion() : bool
{
return false;
}
......@@ -121,7 +117,7 @@ class DB2Connection implements Connection, ServerInfoAwareConnection
/**
* {@inheritdoc}
*/
public function lastInsertId($name = null)
public function lastInsertId(?string $name = null) : string
{
return db2_last_insert_id($this->conn);
}
......
......@@ -39,14 +39,12 @@ class MysqliConnection implements Connection, PingableConnection, ServerInfoAwar
private $conn;
/**
* @param mixed[] $params
* @param string $username
* @param string $password
* @param mixed[] $driverOptions
* @param array<string, mixed> $params
* @param array<int, mixed> $driverOptions
*
* @throws MysqliException
*/
public function __construct(array $params, $username, $password, array $driverOptions = [])
public function __construct(array $params, string $username, string $password, array $driverOptions = [])
{
$port = $params['port'] ?? (int) ini_get('mysqli.default_port');
......@@ -70,7 +68,8 @@ class MysqliConnection implements Connection, PingableConnection, ServerInfoAwar
$this->setSecureConnection($params);
$this->setDriverOptions($driverOptions);
set_error_handler(static function () {
set_error_handler(static function () : bool {
return true;
});
try {
if (! $this->conn->real_connect($host, $username, $password, $dbname, $port, $socket, $flags)) {
......@@ -91,10 +90,8 @@ class MysqliConnection implements Connection, PingableConnection, ServerInfoAwar
* Retrieves mysqli native resource handle.
*
* Could be used if part of your application is not using DBAL.
*
* @return mysqli
*/
public function getWrappedResourceHandle()
public function getWrappedResourceHandle() : mysqli
{
return $this->conn;
}
......@@ -107,7 +104,7 @@ class MysqliConnection implements Connection, PingableConnection, ServerInfoAwar
*
* @link https://jira.mariadb.org/browse/MDEV-4088
*/
public function getServerVersion()
public function getServerVersion() : string
{
$serverInfos = $this->conn->get_server_info();
if (stripos($serverInfos, 'mariadb') !== false) {
......@@ -124,7 +121,7 @@ class MysqliConnection implements Connection, PingableConnection, ServerInfoAwar
/**
* {@inheritdoc}
*/
public function requiresQueryForServerVersion()
public function requiresQueryForServerVersion() : bool
{
return false;
}
......@@ -171,9 +168,9 @@ class MysqliConnection implements Connection, PingableConnection, ServerInfoAwar
/**
* {@inheritdoc}
*/
public function lastInsertId($name = null)
public function lastInsertId(?string $name = null) : string
{
return $this->conn->insert_id;
return (string) $this->conn->insert_id;
}
/**
......@@ -207,12 +204,12 @@ class MysqliConnection implements Connection, PingableConnection, ServerInfoAwar
/**
* Apply the driver options to the connection.
*
* @param mixed[] $driverOptions
* @param array<int, 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 = [])
private function setDriverOptions(array $driverOptions = []) : void
{
$supportedDriverOptions = [
MYSQLI_OPT_CONNECT_TIMEOUT,
......@@ -262,11 +259,11 @@ class MysqliConnection implements Connection, PingableConnection, ServerInfoAwar
/**
* Establish a secure connection
*
* @param mixed[] $params
* @param array<string, mixed> $params
*
* @throws MysqliException
*/
private function setSecureConnection(array $params)
private function setSecureConnection(array $params) : void
{
if (! isset($params['ssl_key']) &&
! isset($params['ssl_cert']) &&
......
......@@ -37,22 +37,15 @@ class OCI8Connection implements Connection, ServerInfoAwareConnection
/**
* Creates a Connection to an Oracle Database using oci8 extension.
*
* @param string $username
* @param string $password
* @param string $db
* @param string $charset
* @param int $sessionMode
* @param bool $persistent
*
* @throws OCI8Exception
*/
public function __construct(
$username,
$password,
$db,
$charset = '',
$sessionMode = OCI_DEFAULT,
$persistent = false
string $username,
string $password,
string $db,
string $charset = '',
int $sessionMode = OCI_DEFAULT,
bool $persistent = false
) {
$dbh = $persistent
? @oci_pconnect($username, $password, $db, $charset, $sessionMode)
......@@ -71,7 +64,7 @@ class OCI8Connection implements Connection, ServerInfoAwareConnection
* @throws UnexpectedValueException If the version string returned by the database server
* does not contain a parsable version number.
*/
public function getServerVersion()
public function getServerVersion() : string
{
$version = oci_server_version($this->dbh);
......@@ -95,7 +88,7 @@ class OCI8Connection implements Connection, ServerInfoAwareConnection
/**
* {@inheritdoc}
*/
public function requiresQueryForServerVersion()
public function requiresQueryForServerVersion() : bool
{
return false;
}
......@@ -141,10 +134,10 @@ class OCI8Connection implements Connection, ServerInfoAwareConnection
/**
* {@inheritdoc}
*/
public function lastInsertId($name = null)
public function lastInsertId(?string $name = null) : string
{
if ($name === null) {
return false;
throw new OCI8Exception('The driver does not support identity columns.');
}
$sql = 'SELECT ' . $name . '.CURRVAL FROM DUAL';
......@@ -155,15 +148,13 @@ class OCI8Connection implements Connection, ServerInfoAwareConnection
throw new OCI8Exception('lastInsertId failed: Query was executed but no result was returned.');
}
return (int) $result;
return $result;
}
/**
* Returns the current execution mode.
*
* @return int
*/
public function getExecuteMode()
public function getExecuteMode() : int
{
return $this->executeMode;
}
......
......@@ -18,17 +18,14 @@ class PDOConnection implements Connection, ServerInfoAwareConnection
private $connection;
/**
* @param string $dsn
* @param string|null $user
* @param string|null $password
* @param mixed[]|null $options
* @param array<int, mixed> $options
*
* @throws PDOException In case of an error.
*/
public function __construct($dsn, $user = null, $password = null, ?array $options = null)
public function __construct(string $dsn, string $username = '', string $password = '', array $options = [])
{
try {
$this->connection = new PDO($dsn, (string) $user, (string) $password, (array) $options);
$this->connection = new PDO($dsn, $username, $password, $options);
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (\PDOException $exception) {
throw new PDOException($exception);
......@@ -50,7 +47,7 @@ class PDOConnection implements Connection, ServerInfoAwareConnection
/**
* {@inheritdoc}
*/
public function getServerVersion()
public function getServerVersion() : string
{
return $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION);
}
......@@ -95,7 +92,7 @@ class PDOConnection implements Connection, ServerInfoAwareConnection
/**
* {@inheritdoc}
*/
public function lastInsertId($name = null)
public function lastInsertId(?string $name = null) : string
{
try {
if ($name === null) {
......@@ -111,7 +108,7 @@ class PDOConnection implements Connection, ServerInfoAwareConnection
/**
* {@inheritdoc}
*/
public function requiresQueryForServerVersion()
public function requiresQueryForServerVersion() : bool
{
return false;
}
......
......@@ -17,7 +17,7 @@ class Connection extends PDOConnection
/**
* {@inheritDoc}
*/
public function lastInsertId($name = null)
public function lastInsertId(?string $name = null) : string
{
if ($name === null) {
return parent::lastInsertId($name);
......
......@@ -37,7 +37,7 @@ class SQLAnywhereConnection implements Connection, ServerInfoAwareConnection
*
* @throws SQLAnywhereException
*/
public function __construct($dsn, $persistent = false)
public function __construct(string $dsn, bool $persistent = false)
{
$this->connection = $persistent ? @sasql_pconnect($dsn) : @sasql_connect($dsn);
......@@ -97,7 +97,7 @@ class SQLAnywhereConnection implements Connection, ServerInfoAwareConnection
/**
* {@inheritdoc}
*/
public function getServerVersion()
public function getServerVersion() : string
{
$version = $this->query("SELECT PROPERTY('ProductVersion')")->fetchColumn();
......@@ -109,7 +109,7 @@ class SQLAnywhereConnection implements Connection, ServerInfoAwareConnection
/**
* {@inheritdoc}
*/
public function lastInsertId($name = null)
public function lastInsertId(?string $name = null) : string
{
if ($name === null) {
return sasql_insert_id($this->connection);
......@@ -148,7 +148,7 @@ class SQLAnywhereConnection implements Connection, ServerInfoAwareConnection
/**
* {@inheritdoc}
*/
public function requiresQueryForServerVersion()
public function requiresQueryForServerVersion() : bool
{
return true;
}
......
......@@ -9,21 +9,15 @@ namespace Doctrine\DBAL\Driver\SQLSrv;
*/
class LastInsertId
{
/** @var int */
/** @var string|null */
private $id;
/**
* @param int $id
*/
public function setId($id)
public function setId(?string $id) : void
{
$this->id = $id;
}
/**
* @return int
*/
public function getId()
public function getId() : ?string
{
return $this->id;
}
......
......@@ -30,12 +30,11 @@ class SQLSrvConnection implements Connection, ServerInfoAwareConnection
protected $lastInsertId;
/**
* @param string $serverName
* @param mixed[] $connectionOptions
* @param array<string, mixed> $connectionOptions
*
* @throws SQLSrvException
*/
public function __construct($serverName, $connectionOptions)
public function __construct(string $serverName, array $connectionOptions)
{
if (! sqlsrv_configure('WarningsReturnAsErrors', 0)) {
throw SQLSrvException::fromSqlSrvErrors();
......@@ -54,7 +53,7 @@ class SQLSrvConnection implements Connection, ServerInfoAwareConnection
/**
* {@inheritdoc}
*/
public function getServerVersion()
public function getServerVersion() : string
{
$serverInfo = sqlsrv_server_info($this->conn);
......@@ -64,7 +63,7 @@ class SQLSrvConnection implements Connection, ServerInfoAwareConnection
/**
* {@inheritdoc}
*/
public function requiresQueryForServerVersion()
public function requiresQueryForServerVersion() : bool
{
return false;
}
......@@ -119,7 +118,7 @@ class SQLSrvConnection implements Connection, ServerInfoAwareConnection
/**
* {@inheritDoc}
*/
public function lastInsertId($name = null)
public function lastInsertId(?string $name = null) : string
{
if ($name !== null) {
$stmt = $this->prepare('SELECT CONVERT(VARCHAR(MAX), current_value) FROM sys.sequences WHERE name = ?');
......
......@@ -11,15 +11,13 @@ interface ServerInfoAwareConnection
{
/**
* Returns the version number of the database server connected to.
*
* @return string
*/
public function getServerVersion();
public function getServerVersion() : string;
/**
* Checks whether a query is required to retrieve the database server version.
*
* @return bool True if a query is required to retrieve the database server version, false otherwise.
*/
public function requiresQueryForServerVersion();
public function requiresQueryForServerVersion() : bool;
}
......@@ -82,18 +82,12 @@ class Connection extends \Doctrine\DBAL\Connection
}
}
/**
* @return int
*/
public function getPortability()
public function getPortability() : int
{
return $this->portability;
}
/**
* @return int
*/
public function getFetchCase()
public function getFetchCase() : ?int
{
return $this->case;
}
......
......@@ -957,7 +957,13 @@ abstract class AbstractSchemaManager
*/
public function getSchemaSearchPaths() : array
{
return [$this->_conn->getDatabase()];
$database = $this->_conn->getDatabase();
if ($database !== null) {
return [$database];
}
return [];
}
/**
......
......@@ -8,9 +8,7 @@ use Doctrine\DBAL\Platforms\DB2Platform;
use Doctrine\DBAL\Types\Type;
use const CASE_LOWER;
use function array_change_key_case;
use function assert;
use function is_resource;
use function is_string;
use function preg_match;
use function str_replace;
use function strpos;
......@@ -30,12 +28,9 @@ class DB2SchemaManager extends AbstractSchemaManager
*/
public function listTableNames() : array
{
$username = $this->_conn->getUsername();
assert(is_string($username));
$sql = $this->_platform->getListTablesSQL() . ' AND CREATOR = CURRENT_USER';
$sql = $this->_platform->getListTablesSQL() . ' AND CREATOR = UPPER(?)';
$tables = $this->_conn->fetchAll($sql, [$username]);
$tables = $this->_conn->fetchAll($sql);
return $this->filterAssetNames($this->_getPortableTablesList($tables));
}
......
......@@ -53,13 +53,13 @@ use function sprintf;
*/
class PoolingShardConnection extends Connection
{
/** @var DriverConnection[] */
/** @var array<int, DriverConnection>|array<string, DriverConnection> */
private $activeConnections = [];
/** @var string|int|null */
private $activeShardId;
/** @var mixed[] */
/** @var array<int, array<string, mixed>>|array<string, array<string, mixed>> */
private $connectionParameters = [];
/**
......@@ -67,8 +67,12 @@ class PoolingShardConnection extends Connection
*
* @throws InvalidArgumentException
*/
public function __construct(array $params, Driver $driver, ?Configuration $config = null, ?EventManager $eventManager = null)
{
public function __construct(
array $params,
Driver $driver,
?Configuration $config = null,
?EventManager $eventManager = null
) {
if (! isset($params['global'], $params['shards'])) {
throw new InvalidArgumentException('Connection Parameters require "global" and "shards" configurations.');
}
......@@ -119,51 +123,11 @@ class PoolingShardConnection extends Connection
/**
* {@inheritdoc}
*/
public function getParams()
public function getParams() : array
{
return $this->activeShardId ? $this->connectionParameters[$this->activeShardId] : $this->connectionParameters[0];
}
/**
* {@inheritdoc}
*/
public function getHost()
{
$params = $this->getParams();
return $params['host'] ?? parent::getHost();
}
/**
* {@inheritdoc}
*/
public function getPort()
{
$params = $this->getParams();
return $params['port'] ?? parent::getPort();
}
/**
* {@inheritdoc}
*/
public function getUsername()
{
$params = $this->getParams();
return $params['user'] ?? parent::getUsername();
}
/**
* {@inheritdoc}
*/
public function getPassword()
{
$params = $this->getParams();
return $params['password'] ?? parent::getPassword();
}
/**
* Connects to a given shard.
*
......@@ -207,10 +171,8 @@ class PoolingShardConnection extends Connection
* Connects to a specific connection.
*
* @param string|int $shardId
*
* @return \Doctrine\DBAL\Driver\Connection
*/
protected function connectTo($shardId)
protected function connectTo($shardId) : DriverConnection
{
$params = $this->getParams();
......@@ -226,10 +188,8 @@ class PoolingShardConnection extends Connection
/**
* @param string|int|null $shardId
*
* @return bool
*/
public function isConnected($shardId = null)
public function isConnected($shardId = null) : bool
{
if ($shardId === null) {
return $this->_conn !== null;
......@@ -238,10 +198,7 @@ class PoolingShardConnection extends Connection
return isset($this->activeConnections[$shardId]);
}
/**
* @return void
*/
public function close()
public function close() : void
{
$this->_conn = null;
$this->activeConnections = [];
......
......@@ -38,13 +38,13 @@ class ConnectionTest extends DbalTestCase
/** @var Connection */
private $connection;
/** @var string[] */
/** @var array<string, mixed> */
protected $params = [
'driver' => 'pdo_mysql',
'host' => 'localhost',
'user' => 'root',
'password' => 'password',
'port' => '1234',
'port' => 1234,
];
protected function setUp() : void
......@@ -114,26 +114,6 @@ class ConnectionTest extends DbalTestCase
self::assertInstanceOf(Configuration::class, $config);
}
public function testGetHost() : void
{
self::assertEquals('localhost', $this->connection->getHost());
}
public function testGetPort() : void
{
self::assertEquals('1234', $this->connection->getPort());
}
public function testGetUsername() : void
{
self::assertEquals('root', $this->connection->getUsername());
}
public function testGetPassword() : void
{
self::assertEquals('password', $this->connection->getPassword());
}
public function testGetDriver() : void
{
self::assertInstanceOf(\Doctrine\DBAL\Driver\PDOMySql\Driver::class, $this->connection->getDriver());
......
......@@ -49,6 +49,6 @@ class OCI8ConnectionTest extends DbalFunctionalTestCase
$schema = $this->connection->getDatabase();
$sequence = $platform->getIdentitySequenceName($schema . '.DBAL2595', 'id');
self::assertSame(1, $this->driverConnection->lastInsertId($sequence));
self::assertEquals(1, $this->driverConnection->lastInsertId($sequence));
}
}
......@@ -237,7 +237,7 @@ class OracleSchemaManagerTest extends SchemaManagerFunctionalTestCase
$otherTable->addColumn('id', Types::STRING);
TestUtil::getTempConnection()->getSchemaManager()->dropAndCreateTable($otherTable);
$columns = $this->schemaManager->listTableColumns($table->getName(), $this->connection->getUsername());
$columns = $this->schemaManager->listTableColumns($table->getName(), $this->connection->getDatabase());
self::assertCount(7, $columns);
}
......
......@@ -190,7 +190,8 @@ class WriteTest extends DbalFunctionalTestCase
$this->markTestSkipped("Test only works consistently on platforms that support sequences and don't support identity columns.");
}
self::assertFalse($this->lastInsertId());
$this->expectException(DriverException::class);
$this->lastInsertId();
}
/**
......
......@@ -33,14 +33,10 @@ final class DB2SchemaManagerTest extends TestCase
$platform = $this->createMock(DB2Platform::class);
$this->conn = $this
->getMockBuilder(Connection::class)
->onlyMethods(['fetchAll', 'getUsername'])
->onlyMethods(['fetchAll'])
->setConstructorArgs([['platform' => $platform], $driverMock, new Configuration(), $eventManager])
->getMock();
$this->conn->expects($this->any())
->method('getUsername')
->willReturn('db2inst1');
$this->manager = new DB2SchemaManager($this->conn);
}
......
......@@ -243,80 +243,4 @@ class PoolingShardConnectionTest extends TestCase
'host' => 'foo',
], $conn->getParams());
}
public function testGetHostOverride() : void
{
$conn = DriverManager::getConnection([
'wrapperClass' => PoolingShardConnection::class,
'driver' => 'pdo_sqlite',
'host' => 'localhost',
'global' => ['memory' => true],
'shards' => [
['id' => 1, 'memory' => true, 'host' => 'foo'],
],
'shardChoser' => MultiTenantShardChoser::class,
]);
self::assertEquals('localhost', $conn->getHost());
$conn->connect(1);
self::assertEquals('foo', $conn->getHost());
}
public function testGetPortOverride() : void
{
$conn = DriverManager::getConnection([
'wrapperClass' => PoolingShardConnection::class,
'driver' => 'pdo_sqlite',
'port' => 3306,
'global' => ['memory' => true],
'shards' => [
['id' => 1, 'memory' => true, 'port' => 3307],
],
'shardChoser' => MultiTenantShardChoser::class,
]);
self::assertEquals(3306, $conn->getPort());
$conn->connect(1);
self::assertEquals(3307, $conn->getPort());
}
public function testGetUsernameOverride() : void
{
$conn = DriverManager::getConnection([
'wrapperClass' => PoolingShardConnection::class,
'driver' => 'pdo_sqlite',
'user' => 'foo',
'global' => ['memory' => true],
'shards' => [
['id' => 1, 'memory' => true, 'user' => 'bar'],
],
'shardChoser' => MultiTenantShardChoser::class,
]);
self::assertEquals('foo', $conn->getUsername());
$conn->connect(1);
self::assertEquals('bar', $conn->getUsername());
}
public function testGetPasswordOverride() : void
{
$conn = DriverManager::getConnection([
'wrapperClass' => PoolingShardConnection::class,
'driver' => 'pdo_sqlite',
'password' => 'foo',
'global' => ['memory' => true],
'shards' => [
['id' => 1, 'memory' => true, 'password' => 'bar'],
],
'shardChoser' => MultiTenantShardChoser::class,
]);
self::assertEquals('foo', $conn->getPassword());
$conn->connect(1);
self::assertEquals('bar', $conn->getPassword());
}
}
......@@ -48,7 +48,14 @@ class PoolingShardManagerTest extends TestCase
public function testSelectGlobal() : void
{
$conn = $this->createConnectionMock();
$conn->expects($this->once())->method('connect')->with($this->equalTo(0));
$conn->expects($this->at(0))
->method('getParams')
->will(
$this->returnValue([
'shardChoser' => $this->createMock(ShardChoser::class),
])
);
$conn->expects($this->at(1))->method('connect')->with($this->equalTo(0));
$conn->method('getParams')
->willReturn([
'shardChoser' => $this->createMock(ShardChoser::class),
......
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