Commit dcdf744e authored by Steve Müller's avatar Steve Müller

initialize database schema only once per phpunit run

parent dfb44bda
...@@ -12,6 +12,11 @@ use Doctrine\DBAL\DriverManager; ...@@ -12,6 +12,11 @@ use Doctrine\DBAL\DriverManager;
*/ */
class TestUtil 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 * Gets a <b>real</b> database connection using the following parameters
* of the $GLOBALS array: * of the $GLOBALS array:
...@@ -29,9 +34,7 @@ class TestUtil ...@@ -29,9 +34,7 @@ class TestUtil
* on an XML configuration file. If no such parameters exist, an SQLite * on an XML configuration file. If no such parameters exist, an SQLite
* in-memory database is used. * in-memory database is used.
* *
* IMPORTANT: * IMPORTANT: Each invocation of this method returns a NEW database connection.
* 1) Each invocation of this method returns a NEW database connection.
* 2) The database is dropped and recreated to ensure it's clean.
* *
* @return Connection The database connection instance. * @return Connection The database connection instance.
*/ */
...@@ -82,22 +85,26 @@ class TestUtil ...@@ -82,22 +85,26 @@ class TestUtil
$platform = $tmpConn->getDatabasePlatform(); $platform = $tmpConn->getDatabasePlatform();
if ($platform->supportsCreateDropDatabase()) { if (! self::$initialized) {
$dbname = $realConn->getDatabase(); if ($platform->supportsCreateDropDatabase()) {
$realConn->close(); $dbname = $realConn->getDatabase();
$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; return $realDbParams;
......
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