]> git.mxchange.org Git - friendica.git/blobdiff - tests/Util/Database/StaticDatabase.php
Merge branch 'bug/phpinfo-accessible-hotfix' into develop
[friendica.git] / tests / Util / Database / StaticDatabase.php
index 6f9bdbe8b5db3c64b0d369e6b407ee8d90ffbce5..73a142d9a9c32931648f009a571c3f3c15674ed5 100644 (file)
@@ -1,4 +1,23 @@
 <?php
+/**
+ * @copyright Copyright (C) 2020, Friendica
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
 
 namespace Friendica\Test\Util\Database;
 
@@ -6,6 +25,12 @@ use Friendica\Database\Database;
 use PDO;
 use PDOException;
 
+/**
+ * Overrides the Friendica database class for re-using the connection
+ * for different tests
+ *
+ * Overrides functionality to enforce one transaction per call (for nested transactions)
+ */
 class StaticDatabase extends Database
 {
        /**
@@ -25,46 +50,14 @@ class StaticDatabase extends Database
                }
 
                if (!isset(self::$staticConnection)) {
-
-                       $port       = 0;
-                       $serveraddr = trim($this->configCache->get('database', 'hostname'));
-                       $serverdata = explode(':', $serveraddr);
-                       $server     = $serverdata[0];
-                       if (count($serverdata) > 1) {
-                               $port = trim($serverdata[1]);
-                       }
-                       $server  = trim($server);
-                       $user    = trim($this->configCache->get('database', 'username'));
-                       $pass    = trim($this->configCache->get('database', 'password'));
-                       $db      = trim($this->configCache->get('database', 'database'));
-                       $charset = trim($this->configCache->get('database', 'charset'));
-
-                       if (!(strlen($server) && strlen($user))) {
-                               return false;
-                       }
-
-                       $connect = "mysql:host=" . $server . ";dbname=" . $db;
-
-                       if ($port > 0) {
-                               $connect .= ";port=" . $port;
-                       }
-
-                       if ($charset) {
-                               $connect .= ";charset=" . $charset;
-                       }
-
-
-                       try {
-                               self::$staticConnection = @new ExtendedPDO($connect, $user, $pass);
-                               self::$staticConnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
-                       } catch (PDOException $e) {
-                               /// @TODO At least log exception, don't ignore it!
-                       }
+                       self::statConnect($_SERVER);
                }
 
                $this->driver = 'pdo';
                $this->connection = self::$staticConnection;
                $this->connected = true;
+               $this->emulate_prepares = false;
+               $this->pdo_emulate_prepares = false;
 
                return $this->connected;
        }
@@ -76,7 +69,7 @@ class StaticDatabase extends Database
         */
        public function transaction()
        {
-               if (!$this->connection->inTransaction() && !$this->connection->beginTransaction()) {
+               if (!$this->in_transaction && !$this->connection->beginTransaction()) {
                        return false;
                }
 
@@ -85,7 +78,7 @@ class StaticDatabase extends Database
        }
 
        /**
-        * @brief Does a commit
+        * Does a commit
         *
         * @return boolean Was the command executed successfully?
         */
@@ -99,13 +92,74 @@ class StaticDatabase extends Database
        }
 
        /**
-        * @return ExtendedPDO
+        * Setup of the global, static connection
+        * Either through explicit calling or through implicit using the Database
+        *
+        * @param array $server $_SERVER variables
+        */
+       public static function statConnect(array $server)
+       {
+               // Use environment variables for mysql if they are set beforehand
+               if (!empty($server['MYSQL_HOST'])
+                   && (!empty($server['MYSQL_USERNAME']) || !empty($server['MYSQL_USER']))
+                   && $server['MYSQL_PASSWORD'] !== false
+                   && !empty($server['MYSQL_DATABASE']))
+               {
+                       $db_host = $server['MYSQL_HOST'];
+                       if (!empty($server['MYSQL_PORT'])) {
+                               $db_host .= ':' . $server['MYSQL_PORT'];
+                       }
+
+                       if (!empty($server['MYSQL_USERNAME'])) {
+                               $db_user = $server['MYSQL_USERNAME'];
+                       } else {
+                               $db_user = $server['MYSQL_USER'];
+                       }
+                       $db_pw = (string) $server['MYSQL_PASSWORD'];
+                       $db_data = $server['MYSQL_DATABASE'];
+               }
+
+               $port       = 0;
+               $serveraddr = trim($db_host);
+               $serverdata = explode(':', $serveraddr);
+               $server     = $serverdata[0];
+               if (count($serverdata) > 1) {
+                       $port = trim($serverdata[1]);
+               }
+               $server  = trim($server);
+               $user    = trim($db_user);
+               $pass    = trim($db_pw);
+               $db      = trim($db_data);
+
+               if (!(strlen($server) && strlen($user))) {
+                       return;
+               }
+
+               $connect = "mysql:host=" . $server . ";dbname=" . $db;
+
+               if ($port > 0) {
+                       $connect .= ";port=" . $port;
+               }
+
+               try {
+                       self::$staticConnection = @new ExtendedPDO($connect, $user, $pass);
+                       self::$staticConnection->setAttribute(PDO::ATTR_AUTOCOMMIT,0);
+               } catch (PDOException $e) {
+                       /// @TODO At least log exception, don't ignore it!
+               }
+       }
+
+       /**
+        * @return ExtendedPDO The global, static connection
         */
        public static function getGlobConnection()
        {
                return self::$staticConnection;
        }
 
+       /**
+        * Perform a global commit for every nested transaction of the static connection
+        */
        public static function statCommit()
        {
                if (isset(self::$staticConnection)) {
@@ -115,6 +169,9 @@ class StaticDatabase extends Database
                }
        }
 
+       /**
+        * Perform a global rollback for every nested transaction of the static connection
+        */
        public static function statRollback()
        {
                if (isset(self::$staticConnection)) {