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

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

5
use Doctrine\DBAL\Connection;
6 7
use Doctrine\DBAL\DriverManager;

romanb's avatar
romanb committed
8 9
/**
 * TestUtil is a class with static utility methods used during tests.
10
 *
romanb's avatar
romanb committed
11 12
 * @author robo
 */
13
class TestUtil
14
{
romanb's avatar
romanb committed
15 16 17
    /**
     * Gets a <b>real</b> database connection using the following parameters
     * of the $GLOBALS array:
18
     *
romanb's avatar
romanb committed
19 20 21 22
     * '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.
23 24
     * '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
25 26
     * 'db_name' : The name of the database to connect to.
     * 'db_port' : The port of the database to connect to.
27
     *
romanb's avatar
romanb committed
28 29 30
     * 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.
31
     *
romanb's avatar
romanb committed
32 33 34
     * IMPORTANT:
     * 1) Each invocation of this method returns a NEW database connection.
     * 2) The database is dropped and recreated to ensure it's clean.
35
     *
36
     * @return Connection The database connection instance.
romanb's avatar
romanb committed
37
     */
romanb's avatar
romanb committed
38 39
    public static function getConnection()
    {
40
        $conn = DriverManager::getConnection(self::getConnectionParams());
41

42
        self::addDbEventSubscribers($conn);
43

44 45
        return $conn;
    }
46

47 48 49
    private static function getConnectionParams() {
        if (self::hasRequiredConnectionParams()) {
            return self::getSpecifiedConnectionParams();
50 51
        }

52
        return self::getFallbackConnectionParams();
romanb's avatar
romanb committed
53
    }
54

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    private static function hasRequiredConnectionParams()
    {
        return isset(
            $GLOBALS['db_type'],
            $GLOBALS['db_username'],
            $GLOBALS['db_password'],
            $GLOBALS['db_host'],
            $GLOBALS['db_name'],
            $GLOBALS['db_port']
        )
        && isset(
            $GLOBALS['tmpdb_type'],
            $GLOBALS['tmpdb_username'],
            $GLOBALS['tmpdb_password'],
            $GLOBALS['tmpdb_host'],
            $GLOBALS['tmpdb_port']
        );
    }

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    private static function getSpecifiedConnectionParams() {
        $realDbParams = self::getParamsForMainConnection();
        $tmpDbParams = self::getParamsForTemporaryConnection();

        $realConn = DriverManager::getConnection($realDbParams);

        // Connect to tmpdb in order to drop and create the real test db.
        $tmpConn = DriverManager::getConnection($tmpDbParams);

        $platform  = $tmpConn->getDatabasePlatform();

        if ($platform->supportsCreateDropDatabase()) {
            $dbname = $realConn->getDatabase();
            $realConn->close();

            $tmpConn->getSchemaManager()->dropAndCreateDatabase($dbname);

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

            $schema = $sm->createSchema();
            $stmts = $schema->toDropSql($realConn->getDatabasePlatform());

            foreach ($stmts as $stmt) {
                $realConn->exec($stmt);
            }
        }

        return $realDbParams;
    }

    private static function getFallbackConnectionParams() {
        $params = array(
            'driver' => 'pdo_sqlite',
            'memory' => true
        );

        if (isset($GLOBALS['db_path'])) {
            $params['path'] = $GLOBALS['db_path'];
            unlink($GLOBALS['db_path']);
        }

        return $params;
    }

    private static function addDbEventSubscribers(Connection $conn) {
        if (isset($GLOBALS['db_event_subscribers'])) {
            $evm = $conn->getEventManager();
            foreach (explode(",", $GLOBALS['db_event_subscribers']) as $subscriberClass) {
                $subscriberInstance = new $subscriberClass();
                $evm->addEventSubscriber($subscriberInstance);
            }
        }
    }

    private static function getParamsForTemporaryConnection()
131
    {
132
        $connectionParams = array(
133 134 135 136
            'driver' => $GLOBALS['tmpdb_type'],
            'user' => $GLOBALS['tmpdb_username'],
            'password' => $GLOBALS['tmpdb_password'],
            'host' => $GLOBALS['tmpdb_host'],
137
            'dbname' => null,
138 139 140
            'port' => $GLOBALS['tmpdb_port']
        );

141 142 143 144
        if (isset($GLOBALS['tmpdb_name'])) {
            $connectionParams['dbname'] = $GLOBALS['tmpdb_name'];
        }

145
        if (isset($GLOBALS['tmpdb_server'])) {
146 147 148 149 150 151 152 153 154 155
            $connectionParams['server'] = $GLOBALS['tmpdb_server'];
        }

        if (isset($GLOBALS['tmpdb_unix_socket'])) {
            $connectionParams['unix_socket'] = $GLOBALS['tmpdb_unix_socket'];
        }

        return $connectionParams;
    }

156
    private static function getParamsForMainConnection()
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
    {
        $connectionParams = array(
            'driver' => $GLOBALS['db_type'],
            'user' => $GLOBALS['db_username'],
            'password' => $GLOBALS['db_password'],
            'host' => $GLOBALS['db_host'],
            'dbname' => $GLOBALS['db_name'],
            'port' => $GLOBALS['db_port']
        );

        if (isset($GLOBALS['db_server'])) {
            $connectionParams['server'] = $GLOBALS['db_server'];
        }

        if (isset($GLOBALS['db_unix_socket'])) {
            $connectionParams['unix_socket'] = $GLOBALS['db_unix_socket'];
173 174
        }

175 176 177 178
        return $connectionParams;
    }

    /**
179
     * @return Connection
180 181 182
     */
    public static function getTempConnection()
    {
183
        return DriverManager::getConnection(self::getParamsForTemporaryConnection());
184
    }
185
}