Commit 548e0746 authored by romanb's avatar romanb

[2.0] Solved the hassle with dropping/creating the test database through...

[2.0] Solved the hassle with dropping/creating the test database through introducing a second test database for temporary connections. Please check the dbproperties.xml.dev file for reference. The 2 test databases need to be created *once* before the very first time of running the tests. New procedure successfully tested against mysql, postgresql, oracle.
parent a333c85c
...@@ -212,6 +212,46 @@ class Connection ...@@ -212,6 +212,46 @@ class Connection
return $this->_driver->getDatabase($this); return $this->_driver->getDatabase($this);
} }
/**
* Gets the hostname of the currently connected database.
*
* @return string
*/
public function getHost()
{
return $this->_params['host'];
}
/**
* Gets the port of the currently connected database.
*
* @return mixed
*/
public function getPort()
{
return $this->_params['port'];
}
/**
* Gets the username used by this connection.
*
* @return string
*/
public function getUsername()
{
return $this->_params['user'];
}
/**
* Gets the password used by this connection.
*
* @return string
*/
public function getPassword()
{
return $this->_params['password'];
}
/** /**
* Gets the DBAL driver instance. * Gets the DBAL driver instance.
* *
...@@ -578,8 +618,7 @@ class Connection ...@@ -578,8 +618,7 @@ class Connection
* *
* @param string $query sql query * @param string $query sql query
* @param array $params query parameters * @param array $params query parameters
* * @return integer
* @return PDOStatement
* @todo Rename to executeUpdate(). * @todo Rename to executeUpdate().
*/ */
public function exec($query, array $params = array()) public function exec($query, array $params = array())
......
...@@ -227,19 +227,15 @@ abstract class AbstractSchemaManager ...@@ -227,19 +227,15 @@ abstract class AbstractSchemaManager
} }
/** /**
* Drop the database for this connection * Drops a database.
*
* NOTE: You can not drop the database this SchemaManager is currently connected to.
* *
* @param string $database The name of the database to drop * @param string $database The name of the database to drop
* @return boolean $result
*/ */
public function dropDatabase($database = null) public function dropDatabase($database)
{ {
if (is_null($database)) { $this->_conn->exec($this->_platform->getDropDatabaseSql($database));
$database = $this->_conn->getDatabase();
}
$sql = $this->_platform->getDropDatabaseSql($database);
return $this->_executeSql($sql, 'execute');
} }
/** /**
......
...@@ -264,26 +264,4 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager ...@@ -264,26 +264,4 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager
return array_merge($decl, $description); return array_merge($decl, $description);
} }
/**
* Drops a database.
* If no database name is given, then the database this SchemaManager is
* currently connected to is dropped. In order to do this, the connection is
* closed and reopened on the "template1" database.
*
* @param string $database The name of the database to drop.
* @return boolean $result
* @override
*/
public function dropDatabase($database = null)
{
if (is_null($database)) {
//TODO: How to deal with this? We need to connect to another database
// in order to drop this one!
$database = $this->_conn->getDatabase();
}
$sql = $this->_platform->getDropDatabaseSql($database);
return $this->_executeSql($sql, 'execute');
}
} }
\ No newline at end of file
...@@ -13,6 +13,7 @@ class DbalFunctionalTestSuite extends DbalTestSuite ...@@ -13,6 +13,7 @@ class DbalFunctionalTestSuite extends DbalTestSuite
protected function tearDown() protected function tearDown()
{ {
$this->sharedFixture['conn']->close();
$this->sharedFixture = null; $this->sharedFixture = null;
} }
} }
\ No newline at end of file
...@@ -33,8 +33,10 @@ class TestUtil ...@@ -33,8 +33,10 @@ class TestUtil
public static function getConnection() public static function getConnection()
{ {
if (isset($GLOBALS['db_type'], $GLOBALS['db_username'], $GLOBALS['db_password'], if (isset($GLOBALS['db_type'], $GLOBALS['db_username'], $GLOBALS['db_password'],
$GLOBALS['db_host'], $GLOBALS['db_name'], $GLOBALS['db_port'])) { $GLOBALS['db_host'], $GLOBALS['db_name'], $GLOBALS['db_port']) &&
$params = array( isset($GLOBALS['tmpdb_type'], $GLOBALS['tmpdb_username'], $GLOBALS['tmpdb_password'],
$GLOBALS['tmpdb_host'], $GLOBALS['tmpdb_name'], $GLOBALS['tmpdb_port'])) {
$realDbParams = array(
'driver' => $GLOBALS['db_type'], 'driver' => $GLOBALS['db_type'],
'user' => $GLOBALS['db_username'], 'user' => $GLOBALS['db_username'],
'password' => $GLOBALS['db_password'], 'password' => $GLOBALS['db_password'],
...@@ -42,10 +44,29 @@ class TestUtil ...@@ -42,10 +44,29 @@ class TestUtil
'dbname' => $GLOBALS['db_name'], 'dbname' => $GLOBALS['db_name'],
'port' => $GLOBALS['db_port'] 'port' => $GLOBALS['db_port']
); );
$conn = \Doctrine\DBAL\DriverManager::getConnection($params); $tmpDbParams = array(
$conn->getSchemaManager()->dropAndCreateDatabase(); 'driver' => $GLOBALS['tmpdb_type'],
$conn->close(); 'user' => $GLOBALS['tmpdb_username'],
$conn->connect(); 'password' => $GLOBALS['tmpdb_password'],
'host' => $GLOBALS['tmpdb_host'],
'dbname' => $GLOBALS['tmpdb_name'],
'port' => $GLOBALS['tmpdb_port']
);
// Connect to tmpdb in order to drop and create the real test db.
$tmpConn = \Doctrine\DBAL\DriverManager::getConnection($tmpDbParams);
$realConn = \Doctrine\DBAL\DriverManager::getConnection($realDbParams);
$dbname = $realConn->getDatabase();
$realConn->close();
$tmpConn->getSchemaManager()->dropDatabase($dbname);
$tmpConn->getSchemaManager()->createDatabase($dbname);
$tmpConn->close();
$conn = \Doctrine\DBAL\DriverManager::getConnection($realDbParams);
} else { } else {
$params = array( $params = array(
'driver' => 'pdo_sqlite', 'driver' => 'pdo_sqlite',
......
...@@ -13,11 +13,20 @@ ...@@ -13,11 +13,20 @@
--> -->
<phpunit> <phpunit>
<php> <php>
<!-- "Real" test database -->
<var name="db_type" value="pdo_mysql"/> <var name="db_type" value="pdo_mysql"/>
<var name="db_host" value="localhost" /> <var name="db_host" value="localhost" />
<var name="db_username" value="root" /> <var name="db_username" value="root" />
<var name="db_password" value="" /> <var name="db_password" value="" />
<var name="db_name" value="doctrine_tests" /> <var name="db_name" value="doctrine_tests" />
<var name="db_port" value="3306"/> <var name="db_port" value="3306"/>
<!-- Database for temporary connections (i.e. to drop/create the main database) -->
<var name="tmpdb_type" value="pdo_mysql"/>
<var name="tmpdb_host" value="localhost" />
<var name="tmpdb_username" value="root" />
<var name="tmpdb_password" value="" />
<var name="tmpdb_name" value="doctrine_tests_tmp" />
<var name="tmpdb_port" value="3306"/>
</php> </php>
</phpunit> </phpunit>
\ No newline at end of file
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