Avoid database connection from PHPUnit data providers

parent f5ddd097
......@@ -55,7 +55,7 @@ abstract class AbstractDriverTest extends DbalFunctionalTestCase
);
self::assertSame(
$this->getDatabaseNameForConnectionWithoutDatabaseNameParameter(),
static::getDatabaseNameForConnectionWithoutDatabaseNameParameter(),
$this->driver->getDatabase($connection)
);
}
......@@ -65,10 +65,7 @@ abstract class AbstractDriverTest extends DbalFunctionalTestCase
*/
abstract protected function createDriver();
/**
* @return string|null
*/
protected function getDatabaseNameForConnectionWithoutDatabaseNameParameter()
protected static function getDatabaseNameForConnectionWithoutDatabaseNameParameter() : ?string
{
return null;
}
......
......@@ -50,9 +50,12 @@ class DriverTest extends AbstractDriverTest
);
}
public function getDatabaseParameter()
/**
* @return mixed[][]
*/
public static function getDatabaseParameter() : iterable
{
$params = TestUtil::getConnection()->getParams();
$params = TestUtil::getConnectionParams();
$realDatabaseName = $params['dbname'] ?? '';
$dummyDatabaseName = $realDatabaseName . 'a';
......@@ -61,7 +64,7 @@ class DriverTest extends AbstractDriverTest
[$realDatabaseName, null, $realDatabaseName],
[$realDatabaseName, $dummyDatabaseName, $realDatabaseName],
[null, $realDatabaseName, $realDatabaseName],
[null, null, $this->getDatabaseNameForConnectionWithoutDatabaseNameParameter()],
[null, null, static::getDatabaseNameForConnectionWithoutDatabaseNameParameter()],
];
}
......@@ -108,7 +111,7 @@ class DriverTest extends AbstractDriverTest
/**
* {@inheritdoc}
*/
protected function getDatabaseNameForConnectionWithoutDatabaseNameParameter()
protected static function getDatabaseNameForConnectionWithoutDatabaseNameParameter() : ?string
{
return 'postgres';
}
......
......@@ -36,7 +36,7 @@ class DriverTest extends AbstractDriverTest
/**
* {@inheritdoc}
*/
protected function getDatabaseNameForConnectionWithoutDatabaseNameParameter()
protected static function getDatabaseNameForConnectionWithoutDatabaseNameParameter() : ?string
{
return 'master';
}
......
......@@ -34,7 +34,7 @@ class DriverTest extends AbstractDriverTest
/**
* {@inheritdoc}
*/
protected function getDatabaseNameForConnectionWithoutDatabaseNameParameter()
protected static function getDatabaseNameForConnectionWithoutDatabaseNameParameter() : ?string
{
return 'master';
}
......
......@@ -40,6 +40,11 @@ class TestUtil
*/
public static function getConnection()
{
if (self::hasRequiredConnectionParams() && ! self::$initialized) {
self::initializeDatabase();
self::$initialized = true;
}
$conn = DriverManager::getConnection(self::getConnectionParams());
self::addDbEventSubscribers($conn);
......@@ -47,10 +52,10 @@ class TestUtil
return $conn;
}
private static function getConnectionParams()
public static function getConnectionParams()
{
if (self::hasRequiredConnectionParams()) {
return self::getSpecifiedConnectionParams();
return self::getParamsForMainConnection();
}
return self::getFallbackConnectionParams();
......@@ -75,7 +80,7 @@ class TestUtil
);
}
private static function getSpecifiedConnectionParams()
private static function initializeDatabase() : void
{
$realDbParams = self::getParamsForMainConnection();
$tmpDbParams = self::getParamsForTemporaryConnection();
......@@ -87,29 +92,23 @@ class TestUtil
$platform = $tmpConn->getDatabasePlatform();
if (! self::$initialized) {
if ($platform->supportsCreateDropDatabase()) {
$dbname = $realConn->getDatabase();
$realConn->close();
if ($platform->supportsCreateDropDatabase()) {
$dbname = $realConn->getDatabase();
$realConn->close();
$tmpConn->getSchemaManager()->dropAndCreateDatabase($dbname);
$tmpConn->getSchemaManager()->dropAndCreateDatabase($dbname);
$tmpConn->close();
} else {
$sm = $realConn->getSchemaManager();
$tmpConn->close();
} else {
$sm = $realConn->getSchemaManager();
$schema = $sm->createSchema();
$stmts = $schema->toDropSql($realConn->getDatabasePlatform());
$schema = $sm->createSchema();
$stmts = $schema->toDropSql($realConn->getDatabasePlatform());
foreach ($stmts as $stmt) {
$realConn->exec($stmt);
}
foreach ($stmts as $stmt) {
$realConn->exec($stmt);
}
self::$initialized = true;
}
return $realDbParams;
}
private static function getFallbackConnectionParams()
......
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