Skip to content

Commit

Permalink
initialize database schema only once per phpunit run
Browse files Browse the repository at this point in the history
  • Loading branch information
deeky666 committed Jan 18, 2015
1 parent dfb44bd commit dcdf744
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions tests/Doctrine/Tests/TestUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
*/
class TestUtil
{
/**
* @var boolean Whether the database schema is initialized.
*/
private static $initialized = false;

/**
* Gets a <b>real</b> database connection using the following parameters
* of the $GLOBALS array:
Expand All @@ -29,9 +34,7 @@ class TestUtil
* on an XML configuration file. If no such parameters exist, an SQLite
* in-memory database is used.
*
* IMPORTANT:
* 1) Each invocation of this method returns a NEW database connection.
* 2) The database is dropped and recreated to ensure it's clean.
* IMPORTANT: Each invocation of this method returns a NEW database connection.
*
* @return Connection The database connection instance.
*/
Expand Down Expand Up @@ -82,22 +85,26 @@ private static function getSpecifiedConnectionParams() {

$platform = $tmpConn->getDatabasePlatform();

if ($platform->supportsCreateDropDatabase()) {
$dbname = $realConn->getDatabase();
$realConn->close();
if (! self::$initialized) {
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;
Expand Down

0 comments on commit dcdf744

Please sign in to comment.