Avoid database connection from PHPUnit data providers

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