Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / TestUtil.php
1 <?php
2
3 namespace Doctrine\Tests;
4
5 /**
6  * TestUtil is a class with static utility methods used during tests.
7  *
8  * @author robo
9  */
10 class TestUtil
11 {
12     /**
13      * Gets a <b>real</b> database connection using the following parameters
14      * of the $GLOBALS array:
15      *
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.
22      *
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.
26      *
27      * IMPORTANT:
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.
30      *
31      * @return Doctrine\DBAL\Connection The database connection instance.
32      */
33     public static function getConnection()
34     {
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']
46             );
47             $tmpDbParams = array(
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']
54             );
55
56             $realConn = \Doctrine\DBAL\DriverManager::getConnection($realDbParams);
57
58             $platform  = $realConn->getDatabasePlatform();
59
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);
64                 $realConn->close();
65
66                 $tmpConn->getSchemaManager()->dropDatabase($dbname);
67                 $tmpConn->getSchemaManager()->createDatabase($dbname);
68
69                 $tmpConn->close();
70             } else {
71                 $sm = $realConn->getSchemaManager();
72
73                 /* @var $schema Schema */
74                 $schema = $sm->createSchema();
75                 $stmts = $schema->toDropSql($realConn->getDatabasePlatform());
76
77                 foreach ($stmts AS $stmt) {
78                     try {
79                         $realConn->exec($stmt);
80                     } catch(\Exception $e) {
81                         // TODO: Now is this a real good idea?
82                     }
83                 }
84             }
85
86             $conn = \Doctrine\DBAL\DriverManager::getConnection($realDbParams, null, null);
87         } else {
88             $params = array(
89                 'driver' => 'pdo_sqlite',
90                 'memory' => true
91             );
92             if (isset($GLOBALS['db_path'])) {
93                 $params['path'] = $GLOBALS['db_path'];
94                 unlink($GLOBALS['db_path']);
95             }
96             $conn = \Doctrine\DBAL\DriverManager::getConnection($params);
97         }
98
99         return $conn;
100     }
101
102     /**
103      * @return \Doctrine\DBAL\Connection
104      */
105     public static function getTempConnection()
106     {
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']
114         );
115
116         // Connect to tmpdb in order to drop and create the real test db.
117         return \Doctrine\DBAL\DriverManager::getConnection($tmpDbParams);
118     }
119 }