3 namespace Doctrine\Tests;
6 * TestUtil is a class with static utility methods used during tests.
13 * Gets a <b>real</b> database connection using the following parameters
14 * of the $GLOBALS array:
16 * 'db_type' : The name of the Doctrine DBAL database driver to use.
17 * 'db_username' : The username to use for connecting.
18 * 'db_password' : The password to use for connecting.
19 * 'db_host' : The hostname of the database to connect to.
20 * 'db_name' : The name of the database to connect to.
21 * 'db_port' : The port of the database to connect to.
23 * Usually these variables of the $GLOBALS array are filled by PHPUnit based
24 * on an XML configuration file. If no such parameters exist, an SQLite
25 * in-memory database is used.
28 * 1) Each invocation of this method returns a NEW database connection.
29 * 2) The database is dropped and recreated to ensure it's clean.
31 * @return Doctrine\DBAL\Connection The database connection instance.
33 public static function getConnection()
35 if (isset($GLOBALS['db_type'], $GLOBALS['db_username'], $GLOBALS['db_password'],
36 $GLOBALS['db_host'], $GLOBALS['db_name'], $GLOBALS['db_port']) &&
37 isset($GLOBALS['tmpdb_type'], $GLOBALS['tmpdb_username'], $GLOBALS['tmpdb_password'],
38 $GLOBALS['tmpdb_host'], $GLOBALS['tmpdb_name'], $GLOBALS['tmpdb_port'])) {
39 $realDbParams = array(
40 'driver' => $GLOBALS['db_type'],
41 'user' => $GLOBALS['db_username'],
42 'password' => $GLOBALS['db_password'],
43 'host' => $GLOBALS['db_host'],
44 'dbname' => $GLOBALS['db_name'],
45 'port' => $GLOBALS['db_port']
48 'driver' => $GLOBALS['tmpdb_type'],
49 'user' => $GLOBALS['tmpdb_username'],
50 'password' => $GLOBALS['tmpdb_password'],
51 'host' => $GLOBALS['tmpdb_host'],
52 'dbname' => $GLOBALS['tmpdb_name'],
53 'port' => $GLOBALS['tmpdb_port']
56 $realConn = \Doctrine\DBAL\DriverManager::getConnection($realDbParams);
58 $platform = $realConn->getDatabasePlatform();
60 if ($platform->supportsCreateDropDatabase()) {
61 $dbname = $realConn->getDatabase();
62 // Connect to tmpdb in order to drop and create the real test db.
63 $tmpConn = \Doctrine\DBAL\DriverManager::getConnection($tmpDbParams);
66 $tmpConn->getSchemaManager()->dropDatabase($dbname);
67 $tmpConn->getSchemaManager()->createDatabase($dbname);
71 $sm = $realConn->getSchemaManager();
73 /* @var $schema Schema */
74 $schema = $sm->createSchema();
75 $stmts = $schema->toDropSql($realConn->getDatabasePlatform());
77 foreach ($stmts AS $stmt) {
79 $realConn->exec($stmt);
80 } catch(\Exception $e) {
81 // TODO: Now is this a real good idea?
86 $conn = \Doctrine\DBAL\DriverManager::getConnection($realDbParams, null, null);
89 'driver' => 'pdo_sqlite',
92 if (isset($GLOBALS['db_path'])) {
93 $params['path'] = $GLOBALS['db_path'];
94 unlink($GLOBALS['db_path']);
96 $conn = \Doctrine\DBAL\DriverManager::getConnection($params);
103 * @return \Doctrine\DBAL\Connection
105 public static function getTempConnection()
107 $tmpDbParams = array(
108 'driver' => $GLOBALS['tmpdb_type'],
109 'user' => $GLOBALS['tmpdb_username'],
110 'password' => $GLOBALS['tmpdb_password'],
111 'host' => $GLOBALS['tmpdb_host'],
112 'dbname' => $GLOBALS['tmpdb_name'],
113 'port' => $GLOBALS['tmpdb_port']
116 // Connect to tmpdb in order to drop and create the real test db.
117 return \Doctrine\DBAL\DriverManager::getConnection($tmpDbParams);