TestUtil.php 5.33 KB
Newer Older
1
<?php
romanb's avatar
romanb committed
2

3
namespace Doctrine\Tests;
romanb's avatar
romanb committed
4

romanb's avatar
romanb committed
5 6
/**
 * TestUtil is a class with static utility methods used during tests.
7
 *
romanb's avatar
romanb committed
8 9
 * @author robo
 */
10
class TestUtil
11
{
romanb's avatar
romanb committed
12 13 14
    /**
     * Gets a <b>real</b> database connection using the following parameters
     * of the $GLOBALS array:
15
     *
romanb's avatar
romanb committed
16 17 18 19
     * '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.
20 21
     * 'db_server' : The server name of the database to connect to
     *               (optional, some vendors allow multiple server instances with different names on the same host).
romanb's avatar
romanb committed
22 23
     * 'db_name' : The name of the database to connect to.
     * 'db_port' : The port of the database to connect to.
24
     *
romanb's avatar
romanb committed
25 26 27
     * 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.
28
     *
romanb's avatar
romanb committed
29 30 31
     * IMPORTANT:
     * 1) Each invocation of this method returns a NEW database connection.
     * 2) The database is dropped and recreated to ensure it's clean.
32
     *
33
     * @return \Doctrine\DBAL\Connection The database connection instance.
romanb's avatar
romanb committed
34
     */
romanb's avatar
romanb committed
35 36 37
    public static function getConnection()
    {
        if (isset($GLOBALS['db_type'], $GLOBALS['db_username'], $GLOBALS['db_password'],
38 39 40 41
                $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(
42 43 44 45
                'driver' => $GLOBALS['db_type'],
                'user' => $GLOBALS['db_username'],
                'password' => $GLOBALS['db_password'],
                'host' => $GLOBALS['db_host'],
46 47
                'dbname' => $GLOBALS['db_name'],
                'port' => $GLOBALS['db_port']
48
            );
49 50 51 52 53 54 55 56
            $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']
            );
57

58 59 60 61
            if (isset($GLOBALS['db_server'])) {
                $realDbParams['server'] = $GLOBALS['db_server'];
            }

62 63 64 65
            if (isset($GLOBALS['db_unix_socket'])) {
                $realDbParams['unix_socket'] = $GLOBALS['db_unix_socket'];
            }

66 67 68 69
            if (isset($GLOBALS['tmpdb_server'])) {
                $tmpDbParams['server'] = $GLOBALS['tmpdb_server'];
            }

70 71 72 73
            if (isset($GLOBALS['tmpdb_unix_socket'])) {
                $tmpDbParams['unix_socket'] = $GLOBALS['tmpdb_unix_socket'];
            }

74
            $realConn = \Doctrine\DBAL\DriverManager::getConnection($realDbParams);
75

76 77 78 79 80 81 82 83
            $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();

84
                $tmpConn->getSchemaManager()->dropAndCreateDatabase($dbname);
85 86 87

                $tmpConn->close();
            } else {
88 89
                $sm = $realConn->getSchemaManager();

90 91 92
                /* @var $schema Schema */
                $schema = $sm->createSchema();
                $stmts = $schema->toDropSql($realConn->getDatabasePlatform());
93

94 95
                foreach ($stmts AS $stmt) {
                    $realConn->exec($stmt);
96 97
                }
            }
98

99
            $conn = \Doctrine\DBAL\DriverManager::getConnection($realDbParams, null, null);
romanb's avatar
romanb committed
100
        } else {
101
            $params = array(
romanb's avatar
romanb committed
102
                'driver' => 'pdo_sqlite',
103
                'memory' => true
104
            );
105 106 107 108
            if (isset($GLOBALS['db_path'])) {
                $params['path'] = $GLOBALS['db_path'];
                unlink($GLOBALS['db_path']);
            }
109
            $conn = \Doctrine\DBAL\DriverManager::getConnection($params);
110
        }
111

112 113 114 115 116 117 118 119
        if (isset($GLOBALS['db_event_subscribers'])) {
            $evm = $conn->getEventManager();
            foreach (explode(",", $GLOBALS['db_event_subscribers']) AS $subscriberClass) {
                $subscriberInstance = new $subscriberClass();
                $evm->addEventSubscriber($subscriberInstance);
            }
        }

120
        return $conn;
romanb's avatar
romanb committed
121
    }
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

    /**
     * @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']
        );

137 138 139 140
        if (isset($GLOBALS['tmpdb_server'])) {
            $tmpDbParams['server'] = $GLOBALS['tmpdb_server'];
        }

141 142 143
        // Connect to tmpdb in order to drop and create the real test db.
        return \Doctrine\DBAL\DriverManager::getConnection($tmpDbParams);
    }
144
}