2009-05-30 13:53:36 +04:00
|
|
|
<?php
|
2008-02-10 22:08:31 +03:00
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
namespace Doctrine\Tests;
|
2009-01-04 19:15:32 +03:00
|
|
|
|
2009-05-30 15:33:06 +04:00
|
|
|
/**
|
|
|
|
* TestUtil is a class with static utility methods used during tests.
|
2010-04-07 22:39:34 +04:00
|
|
|
*
|
2009-05-30 15:33:06 +04:00
|
|
|
* @author robo
|
|
|
|
*/
|
2009-01-22 22:38:10 +03:00
|
|
|
class TestUtil
|
2009-05-30 06:27:50 +04:00
|
|
|
{
|
2009-05-30 15:33:06 +04:00
|
|
|
/**
|
|
|
|
* Gets a <b>real</b> database connection using the following parameters
|
|
|
|
* of the $GLOBALS array:
|
2010-04-07 22:39:34 +04:00
|
|
|
*
|
2009-05-30 15:33:06 +04:00
|
|
|
* 'db_type' : The name of the Doctrine DBAL database driver to use.
|
|
|
|
* 'db_username' : The username to use for connecting.
|
|
|
|
* 'db_password' : The password to use for connecting.
|
|
|
|
* 'db_host' : The hostname of the database to connect to.
|
|
|
|
* 'db_name' : The name of the database to connect to.
|
|
|
|
* 'db_port' : The port of the database to connect to.
|
2010-04-07 22:39:34 +04:00
|
|
|
*
|
2009-05-30 15:33:06 +04:00
|
|
|
* Usually these variables of the $GLOBALS array are filled by PHPUnit based
|
|
|
|
* on an XML configuration file. If no such parameters exist, an SQLite
|
|
|
|
* in-memory database is used.
|
2010-04-07 22:39:34 +04:00
|
|
|
*
|
2009-05-30 15:33:06 +04:00
|
|
|
* IMPORTANT:
|
|
|
|
* 1) Each invocation of this method returns a NEW database connection.
|
|
|
|
* 2) The database is dropped and recreated to ensure it's clean.
|
2010-04-07 22:39:34 +04:00
|
|
|
*
|
2009-05-30 15:33:06 +04:00
|
|
|
* @return Doctrine\DBAL\Connection The database connection instance.
|
|
|
|
*/
|
2008-02-10 22:08:31 +03:00
|
|
|
public static function getConnection()
|
|
|
|
{
|
|
|
|
if (isset($GLOBALS['db_type'], $GLOBALS['db_username'], $GLOBALS['db_password'],
|
2009-05-30 17:57:57 +04:00
|
|
|
$GLOBALS['db_host'], $GLOBALS['db_name'], $GLOBALS['db_port']) &&
|
|
|
|
isset($GLOBALS['tmpdb_type'], $GLOBALS['tmpdb_username'], $GLOBALS['tmpdb_password'],
|
|
|
|
$GLOBALS['tmpdb_host'], $GLOBALS['tmpdb_name'], $GLOBALS['tmpdb_port'])) {
|
|
|
|
$realDbParams = array(
|
2008-06-15 19:56:28 +04:00
|
|
|
'driver' => $GLOBALS['db_type'],
|
|
|
|
'user' => $GLOBALS['db_username'],
|
|
|
|
'password' => $GLOBALS['db_password'],
|
|
|
|
'host' => $GLOBALS['db_host'],
|
2009-01-12 16:34:41 +03:00
|
|
|
'dbname' => $GLOBALS['db_name'],
|
|
|
|
'port' => $GLOBALS['db_port']
|
2008-06-15 19:56:28 +04:00
|
|
|
);
|
2009-05-30 17:57:57 +04:00
|
|
|
$tmpDbParams = array(
|
|
|
|
'driver' => $GLOBALS['tmpdb_type'],
|
|
|
|
'user' => $GLOBALS['tmpdb_username'],
|
|
|
|
'password' => $GLOBALS['tmpdb_password'],
|
|
|
|
'host' => $GLOBALS['tmpdb_host'],
|
|
|
|
'dbname' => $GLOBALS['tmpdb_name'],
|
|
|
|
'port' => $GLOBALS['tmpdb_port']
|
|
|
|
);
|
2010-04-07 22:39:34 +04:00
|
|
|
|
2009-05-30 17:57:57 +04:00
|
|
|
$realConn = \Doctrine\DBAL\DriverManager::getConnection($realDbParams);
|
2009-11-21 21:52:02 +03:00
|
|
|
|
2010-04-07 22:39:34 +04:00
|
|
|
$platform = $realConn->getDatabasePlatform();
|
|
|
|
|
|
|
|
if ($platform->supportsCreateDropDatabase()) {
|
|
|
|
$dbname = $realConn->getDatabase();
|
|
|
|
// Connect to tmpdb in order to drop and create the real test db.
|
|
|
|
$tmpConn = \Doctrine\DBAL\DriverManager::getConnection($tmpDbParams);
|
|
|
|
$realConn->close();
|
|
|
|
|
|
|
|
$tmpConn->getSchemaManager()->dropDatabase($dbname);
|
|
|
|
$tmpConn->getSchemaManager()->createDatabase($dbname);
|
|
|
|
|
|
|
|
$tmpConn->close();
|
|
|
|
} else {
|
2010-04-09 00:40:53 +04:00
|
|
|
$sm = $realConn->getSchemaManager();
|
|
|
|
|
|
|
|
$tableNames = $sm->listTableNames();
|
|
|
|
|
|
|
|
foreach ($tableNames AS $tableName) {
|
|
|
|
$sm->dropTable($tableName);
|
|
|
|
}
|
2010-04-07 22:39:34 +04:00
|
|
|
}
|
2010-02-01 00:51:15 +03:00
|
|
|
|
|
|
|
$eventManager = null;
|
|
|
|
if (isset($GLOBALS['db_event_subscribers'])) {
|
|
|
|
$eventManager = new \Doctrine\Common\EventManager();
|
|
|
|
foreach (explode(",", $GLOBALS['db_event_subscribers']) AS $subscriberClass) {
|
|
|
|
$subscriberInstance = new $subscriberClass();
|
|
|
|
$eventManager->addEventSubscriber($subscriberInstance);
|
|
|
|
}
|
|
|
|
}
|
2010-04-07 22:39:34 +04:00
|
|
|
|
2010-02-01 00:51:15 +03:00
|
|
|
$conn = \Doctrine\DBAL\DriverManager::getConnection($realDbParams, null, $eventManager);
|
2010-04-07 22:39:34 +04:00
|
|
|
|
2008-02-10 22:08:31 +03:00
|
|
|
} else {
|
2008-06-15 19:56:28 +04:00
|
|
|
$params = array(
|
2008-09-12 21:25:38 +04:00
|
|
|
'driver' => 'pdo_sqlite',
|
2008-07-04 20:32:19 +04:00
|
|
|
'memory' => true
|
2008-06-15 19:56:28 +04:00
|
|
|
);
|
2011-01-01 20:17:19 +03:00
|
|
|
if (isset($GLOBALS['db_path'])) {
|
|
|
|
$params['path'] = $GLOBALS['db_path'];
|
|
|
|
unlink($GLOBALS['db_path']);
|
|
|
|
}
|
2009-05-30 13:53:36 +04:00
|
|
|
$conn = \Doctrine\DBAL\DriverManager::getConnection($params);
|
2008-06-15 19:56:28 +04:00
|
|
|
}
|
2009-05-30 06:27:50 +04:00
|
|
|
|
|
|
|
return $conn;
|
2008-02-10 22:08:31 +03:00
|
|
|
}
|
2009-11-03 19:56:05 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \Doctrine\DBAL\Connection
|
|
|
|
*/
|
|
|
|
public static function getTempConnection()
|
|
|
|
{
|
|
|
|
$tmpDbParams = array(
|
|
|
|
'driver' => $GLOBALS['tmpdb_type'],
|
|
|
|
'user' => $GLOBALS['tmpdb_username'],
|
|
|
|
'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.
|
|
|
|
return \Doctrine\DBAL\DriverManager::getConnection($tmpDbParams);
|
|
|
|
}
|
2008-02-10 22:08:31 +03:00
|
|
|
}
|