]> git.mxchange.org Git - friendica.git/blob - tests/Util/Database/StaticDatabase.php
Remove Phpunit/Dbunit
[friendica.git] / tests / Util / Database / StaticDatabase.php
1 <?php
2
3 namespace Friendica\Test\Util\Database;
4
5 use Friendica\Database\Database;
6 use PDO;
7 use PDOException;
8
9 /**
10  * Overrides the Friendica database class for re-using the connection
11  * for different tests
12  *
13  * Overrides functionality to enforce one transaction per call (for nested transactions)
14  */
15 class StaticDatabase extends Database
16 {
17         /**
18          * @var ExtendedPDO
19          */
20         private static $staticConnection;
21
22         /**
23          * Override the behaviour of connect, due there is just one, static connection at all
24          *
25          * @return bool|void
26          */
27         public function connect()
28         {
29                 if (!is_null($this->connection) && $this->connected()) {
30                         return true;
31                 }
32
33                 if (!isset(self::$staticConnection)) {
34                         self::statConnect($_SERVER);
35                 }
36
37                 $this->driver = 'pdo';
38                 $this->connection = self::$staticConnection;
39                 $this->connected = true;
40
41                 return $this->connected;
42         }
43
44         /**
45          * Override the transaction since there are now hierachical transactions possible
46          *
47          * @return bool
48          */
49         public function transaction()
50         {
51                 if (!$this->in_transaction && !$this->connection->beginTransaction()) {
52                         return false;
53                 }
54
55                 $this->in_transaction = true;
56                 return true;
57         }
58
59         /**
60          * @brief Does a commit
61          *
62          * @return boolean Was the command executed successfully?
63          */
64         public function commit()
65         {
66                 if (!$this->performCommit()) {
67                         return false;
68                 }
69                 $this->in_transaction = false;
70                 return true;
71         }
72
73         /**
74          * Setup of the global, static connection
75          * Either through explicit calling or through implicit using the Database
76          *
77          * @param array $server $_SERVER variables
78          */
79         public static function statConnect(array $server)
80         {
81                 // Use environment variables for mysql if they are set beforehand
82                 if (!empty($server['MYSQL_HOST'])
83                     && !empty($server['MYSQL_USERNAME'] || !empty($server['MYSQL_USER']))
84                     && $server['MYSQL_PASSWORD'] !== false
85                     && !empty($server['MYSQL_DATABASE']))
86                 {
87                         $db_host = $server['MYSQL_HOST'];
88                         if (!empty($server['MYSQL_PORT'])) {
89                                 $db_host .= ':' . $server['MYSQL_PORT'];
90                         }
91
92                         if (!empty($server['MYSQL_USERNAME'])) {
93                                 $db_user = $server['MYSQL_USERNAME'];
94                         } else {
95                                 $db_user = $server['MYSQL_USER'];
96                         }
97                         $db_pw = (string) $server['MYSQL_PASSWORD'];
98                         $db_data = $server['MYSQL_DATABASE'];
99                 }
100
101                 $port       = 0;
102                 $serveraddr = trim($db_host);
103                 $serverdata = explode(':', $serveraddr);
104                 $server     = $serverdata[0];
105                 if (count($serverdata) > 1) {
106                         $port = trim($serverdata[1]);
107                 }
108                 $server  = trim($server);
109                 $user    = trim($db_user);
110                 $pass    = trim($db_pw);
111                 $db      = trim($db_data);
112
113                 if (!(strlen($server) && strlen($user))) {
114                         return;
115                 }
116
117                 $connect = "mysql:host=" . $server . ";dbname=" . $db;
118
119                 if ($port > 0) {
120                         $connect .= ";port=" . $port;
121                 }
122
123                 try {
124                         self::$staticConnection = @new ExtendedPDO($connect, $user, $pass);
125                         self::$staticConnection->setAttribute(PDO::ATTR_AUTOCOMMIT,0);
126                 } catch (PDOException $e) {
127                         /// @TODO At least log exception, don't ignore it!
128                 }
129         }
130
131         /**
132          * @return ExtendedPDO The global, static connection
133          */
134         public static function getGlobConnection()
135         {
136                 return self::$staticConnection;
137         }
138
139         /**
140          * Perform a global commit for every nested transaction of the static connection
141          */
142         public static function statCommit()
143         {
144                 if (isset(self::$staticConnection)) {
145                         while (self::$staticConnection->getTransactionDepth() > 0) {
146                                 self::$staticConnection->commit();
147                         }
148                 }
149         }
150
151         /**
152          * Perform a global rollback for every nested transaction of the static connection
153          */
154         public static function statRollback()
155         {
156                 if (isset(self::$staticConnection)) {
157                         while (self::$staticConnection->getTransactionDepth() > 0) {
158                                 self::$staticConnection->rollBack();
159                         }
160                 }
161         }
162 }