Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / dbal / 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             if (isset($GLOBALS['db_unix_socket'])) {
57                 $realDbParams['unix_socket'] = $GLOBALS['db_unix_socket'];
58             }
59
60             if (isset($GLOBALS['tmpdb_unix_socket'])) {
61                 $tmpDbParams['unix_socket'] = $GLOBALS['tmpdb_unix_socket'];
62             }
63
64             $realConn = \Doctrine\DBAL\DriverManager::getConnection($realDbParams);
65
66             $platform  = $realConn->getDatabasePlatform();
67
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);
72                 $realConn->close();
73
74                 $tmpConn->getSchemaManager()->dropAndCreateDatabase($dbname);
75
76                 $tmpConn->close();
77             } else {
78                 $sm = $realConn->getSchemaManager();
79
80                 /* @var $schema Schema */
81                 $schema = $sm->createSchema();
82                 $stmts = $schema->toDropSql($realConn->getDatabasePlatform());
83
84                 foreach ($stmts AS $stmt) {
85                     $realConn->exec($stmt);
86                 }
87             }
88
89             $conn = \Doctrine\DBAL\DriverManager::getConnection($realDbParams, null, null);
90         } else {
91             $params = array(
92                 'driver' => 'pdo_sqlite',
93                 'memory' => true
94             );
95             if (isset($GLOBALS['db_path'])) {
96                 $params['path'] = $GLOBALS['db_path'];
97                 unlink($GLOBALS['db_path']);
98             }
99             $conn = \Doctrine\DBAL\DriverManager::getConnection($params);
100         }
101
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);
107             }
108         }
109
110         return $conn;
111     }
112
113     /**
114      * @return \Doctrine\DBAL\Connection
115      */
116     public static function getTempConnection()
117     {
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']
125         );
126
127         // Connect to tmpdb in order to drop and create the real test db.
128         return \Doctrine\DBAL\DriverManager::getConnection($tmpDbParams);
129     }
130 }