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 if (isset($GLOBALS['db_unix_socket'])) {
57 $realDbParams['unix_socket'] = $GLOBALS['db_unix_socket'];
60 if (isset($GLOBALS['tmpdb_unix_socket'])) {
61 $tmpDbParams['unix_socket'] = $GLOBALS['tmpdb_unix_socket'];
64 $realConn = \Doctrine\DBAL\DriverManager::getConnection($realDbParams);
66 $platform = $realConn->getDatabasePlatform();
68 if ($platform->supportsCreateDropDatabase()) {
69 $dbname = $realConn->getDatabase();
70 // Connect to tmpdb in order to drop and create the real test db.
71 $tmpConn = \Doctrine\DBAL\DriverManager::getConnection($tmpDbParams);
74 $tmpConn->getSchemaManager()->dropAndCreateDatabase($dbname);
78 $sm = $realConn->getSchemaManager();
80 /* @var $schema Schema */
81 $schema = $sm->createSchema();
82 $stmts = $schema->toDropSql($realConn->getDatabasePlatform());
84 foreach ($stmts AS $stmt) {
85 $realConn->exec($stmt);
89 $conn = \Doctrine\DBAL\DriverManager::getConnection($realDbParams, null, null);
92 'driver' => 'pdo_sqlite',
95 if (isset($GLOBALS['db_path'])) {
96 $params['path'] = $GLOBALS['db_path'];
97 unlink($GLOBALS['db_path']);
99 $conn = \Doctrine\DBAL\DriverManager::getConnection($params);
102 if (isset($GLOBALS['db_event_subscribers'])) {
103 $evm = $conn->getEventManager();
104 foreach (explode(",", $GLOBALS['db_event_subscribers']) AS $subscriberClass) {
105 $subscriberInstance = new $subscriberClass();
106 $evm->addEventSubscriber($subscriberInstance);
114 * @return \Doctrine\DBAL\Connection
116 public static function getTempConnection()
118 $tmpDbParams = array(
119 'driver' => $GLOBALS['tmpdb_type'],
120 'user' => $GLOBALS['tmpdb_username'],
121 'password' => $GLOBALS['tmpdb_password'],
122 'host' => $GLOBALS['tmpdb_host'],
123 'dbname' => $GLOBALS['tmpdb_name'],
124 'port' => $GLOBALS['tmpdb_port']
127 // Connect to tmpdb in order to drop and create the real test db.
128 return \Doctrine\DBAL\DriverManager::getConnection($tmpDbParams);