Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid database connection from PHPUnit data providers #3473

Merged
merged 1 commit into from
Mar 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function testReturnsDatabaseNameWithoutDatabaseNameParameter()
);

self::assertSame(
$this->getDatabaseNameForConnectionWithoutDatabaseNameParameter(),
static::getDatabaseNameForConnectionWithoutDatabaseNameParameter(),
$this->driver->getDatabase($connection)
);
}
Expand All @@ -65,10 +65,7 @@ public function testReturnsDatabaseNameWithoutDatabaseNameParameter()
*/
abstract protected function createDriver();

/**
* @return string|null
*/
protected function getDatabaseNameForConnectionWithoutDatabaseNameParameter()
protected static function getDatabaseNameForConnectionWithoutDatabaseNameParameter() : ?string
{
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,12 @@ public function testDatabaseParameters($databaseName, $defaultDatabaseName, $exp
);
}

public function getDatabaseParameter()
/**
* @return mixed[][]
*/
public static function getDatabaseParameter() : iterable
{
$params = TestUtil::getConnection()->getParams();
$params = TestUtil::getConnectionParams();
$realDatabaseName = $params['dbname'] ?? '';
$dummyDatabaseName = $realDatabaseName . 'a';

Expand All @@ -61,7 +64,7 @@ public function getDatabaseParameter()
[$realDatabaseName, null, $realDatabaseName],
[$realDatabaseName, $dummyDatabaseName, $realDatabaseName],
[null, $realDatabaseName, $realDatabaseName],
[null, null, $this->getDatabaseNameForConnectionWithoutDatabaseNameParameter()],
[null, null, static::getDatabaseNameForConnectionWithoutDatabaseNameParameter()],
];
}

Expand Down Expand Up @@ -108,7 +111,7 @@ protected function createDriver()
/**
* {@inheritdoc}
*/
protected function getDatabaseNameForConnectionWithoutDatabaseNameParameter()
protected static function getDatabaseNameForConnectionWithoutDatabaseNameParameter() : ?string
{
return 'postgres';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected function createDriver()
/**
* {@inheritdoc}
*/
protected function getDatabaseNameForConnectionWithoutDatabaseNameParameter()
protected static function getDatabaseNameForConnectionWithoutDatabaseNameParameter() : ?string
{
return 'master';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected function createDriver()
/**
* {@inheritdoc}
*/
protected function getDatabaseNameForConnectionWithoutDatabaseNameParameter()
protected static function getDatabaseNameForConnectionWithoutDatabaseNameParameter() : ?string
{
return 'master';
}
Expand Down
39 changes: 19 additions & 20 deletions tests/Doctrine/Tests/TestUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,22 @@ class TestUtil
*/
public static function getConnection()
{
if (self::hasRequiredConnectionParams() && ! self::$initialized) {
self::initializeDatabase();
self::$initialized = true;
}

$conn = DriverManager::getConnection(self::getConnectionParams());

self::addDbEventSubscribers($conn);

return $conn;
}

private static function getConnectionParams()
public static function getConnectionParams()
{
if (self::hasRequiredConnectionParams()) {
return self::getSpecifiedConnectionParams();
return self::getParamsForMainConnection();
}

return self::getFallbackConnectionParams();
Expand All @@ -75,7 +80,7 @@ private static function hasRequiredConnectionParams()
);
}

private static function getSpecifiedConnectionParams()
private static function initializeDatabase() : void
{
$realDbParams = self::getParamsForMainConnection();
$tmpDbParams = self::getParamsForTemporaryConnection();
Expand All @@ -87,29 +92,23 @@ private static function getSpecifiedConnectionParams()

$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()
Expand Down