Skip to content

Commit

Permalink
Avoid database connection from PHPUnit data providers
Browse files Browse the repository at this point in the history
  • Loading branch information
morozov committed Feb 27, 2019
1 parent f5ddd09 commit 8a1b0a2
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 31 deletions.
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

0 comments on commit 8a1b0a2

Please sign in to comment.