]> git.mxchange.org Git - friendica.git/blob - tests/Util/Database/StaticDatabase.php
Merge pull request #9436 from nupplaphil/task/test_notices
[friendica.git] / tests / Util / Database / StaticDatabase.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Test\Util\Database;
23
24 use Friendica\Database\Database;
25 use Friendica\Database\DatabaseException;
26 use PDO;
27 use PDOException;
28
29 /**
30  * Overrides the Friendica database class for re-using the connection
31  * for different tests
32  *
33  * Overrides functionality to enforce one transaction per call (for nested transactions)
34  */
35 class StaticDatabase extends Database
36 {
37         /**
38          * @var ExtendedPDO
39          */
40         private static $staticConnection;
41
42         /**
43          * Override the behaviour of connect, due there is just one, static connection at all
44          *
45          * @return bool|void
46          */
47         public function connect()
48         {
49                 if (!is_null($this->connection) && $this->connected()) {
50                         return true;
51                 }
52
53                 if (!isset(self::$staticConnection)) {
54                         self::statConnect($_SERVER);
55                 }
56
57                 $this->driver = 'pdo';
58                 $this->connection = self::$staticConnection;
59                 $this->connected = true;
60                 $this->emulate_prepares = false;
61                 $this->pdo_emulate_prepares = false;
62
63                 return $this->connected;
64         }
65
66         /**
67          * Override the transaction since there are now hierachical transactions possible
68          *
69          * @return bool
70          */
71         public function transaction()
72         {
73                 if (!$this->in_transaction && !$this->connection->beginTransaction()) {
74                         return false;
75                 }
76
77                 $this->in_transaction = true;
78                 return true;
79         }
80
81         /**
82          * Does a commit
83          *
84          * @return bool Was the command executed successfully?
85          */
86         public function commit()
87         {
88                 if (!$this->performCommit()) {
89                         return false;
90                 }
91                 $this->in_transaction = false;
92                 return true;
93         }
94
95         /**
96          * Setup of the global, static connection
97          * Either through explicit calling or through implicit using the Database
98          *
99          * @param array $server $_SERVER variables
100          *
101          * @throws \Exception
102          */
103         public static function statConnect(array $server)
104         {
105                 // Use environment variables for mysql if they are set beforehand
106                 if (!empty($server['MYSQL_HOST'])
107                     && (!empty($server['MYSQL_USERNAME']) || !empty($server['MYSQL_USER']))
108                     && $server['MYSQL_PASSWORD'] !== false
109                     && !empty($server['MYSQL_DATABASE']))
110                 {
111                         $db_host = $server['MYSQL_HOST'];
112                         if (!empty($server['MYSQL_PORT'])) {
113                                 $db_host .= ':' . $server['MYSQL_PORT'];
114                         }
115
116                         if (!empty($server['MYSQL_USERNAME'])) {
117                                 $db_user = $server['MYSQL_USERNAME'];
118                         } else {
119                                 $db_user = $server['MYSQL_USER'];
120                         }
121                         $db_pw = (string) $server['MYSQL_PASSWORD'];
122                         $db_data = $server['MYSQL_DATABASE'];
123                 }
124
125                 if (empty($db_host) || empty($db_user) || empty($db_data)) {
126                         throw new DatabaseException('Either one of the following settings are missing: Host, User or Database', 999, 'CONNECT');
127                 }
128
129                 $port       = 0;
130                 $serveraddr = trim($db_host);
131                 $serverdata = explode(':', $serveraddr);
132                 $server     = $serverdata[0];
133                 if (count($serverdata) > 1) {
134                         $port = trim($serverdata[1]);
135                 }
136                 $server  = trim($server);
137                 $user    = trim($db_user);
138                 $pass    = trim($db_pw ?? '');
139                 $db      = trim($db_data);
140
141                 if (!(strlen($server) && strlen($user))) {
142                         return;
143                 }
144
145                 $connect = "mysql:host=" . $server . ";dbname=" . $db;
146
147                 if ($port > 0) {
148                         $connect .= ";port=" . $port;
149                 }
150
151                 try {
152                         self::$staticConnection = @new ExtendedPDO($connect, $user, $pass);
153                         self::$staticConnection->setAttribute(PDO::ATTR_AUTOCOMMIT,0);
154                 } catch (PDOException $e) {
155                         /// @TODO At least log exception, don't ignore it!
156                 }
157         }
158
159         /**
160          * @return ExtendedPDO The global, static connection
161          */
162         public static function getGlobConnection()
163         {
164                 return self::$staticConnection;
165         }
166
167         /**
168          * Perform a global commit for every nested transaction of the static connection
169          */
170         public static function statCommit()
171         {
172                 if (isset(self::$staticConnection)) {
173                         while (self::$staticConnection->getTransactionDepth() > 0) {
174                                 self::$staticConnection->commit();
175                         }
176                 }
177         }
178
179         /**
180          * Perform a global rollback for every nested transaction of the static connection
181          */
182         public static function statRollback()
183         {
184                 if (isset(self::$staticConnection)) {
185                         while (self::$staticConnection->getTransactionDepth() > 0) {
186                                 self::$staticConnection->rollBack();
187                         }
188                 }
189         }
190 }