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

60 61 62 63 64 65 66
            $dbname = $realConn->getDatabase();
            $realConn->close();
            
            $tmpConn->getSchemaManager()->dropDatabase($dbname);
            $tmpConn->getSchemaManager()->createDatabase($dbname);
            
            $tmpConn->close();
67 68 69 70 71 72 73 74 75

            $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);
                }
            }
76
            
77
            $conn = \Doctrine\DBAL\DriverManager::getConnection($realDbParams, null, $eventManager);
78
            
romanb's avatar
romanb committed
79
        } else {
80
            $params = array(
romanb's avatar
romanb committed
81
                'driver' => 'pdo_sqlite',
82
                'memory' => true
83
            );
84
            $conn = \Doctrine\DBAL\DriverManager::getConnection($params);
85
        }
86 87

        return $conn;
romanb's avatar
romanb committed
88
    }
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106

    /**
     * @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);
    }
romanb's avatar
romanb committed
107
}