]> git.mxchange.org Git - friendica.git/blobdiff - tests/Util/Database/StaticDatabase.php
Merge branch '2021.06-rc' of https://github.com/friendica/friendica into 2021.06...
[friendica.git] / tests / Util / Database / StaticDatabase.php
index c95b690c635801bd04039ac9d19f93eb920cdb83..3bf6396a94bbf3d4dc9be99796a6df0f294e2213 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2021, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -22,6 +22,7 @@
 namespace Friendica\Test\Util\Database;
 
 use Friendica\Database\Database;
+use Friendica\Database\DatabaseException;
 use PDO;
 use PDOException;
 
@@ -38,6 +39,9 @@ class StaticDatabase extends Database
         */
        private static $staticConnection;
 
+       /** @var bool  */
+       private $_locked = false;
+
        /**
         * Override the behaviour of connect, due there is just one, static connection at all
         *
@@ -57,7 +61,6 @@ class StaticDatabase extends Database
                $this->connection = self::$staticConnection;
                $this->connected = true;
                $this->emulate_prepares = false;
-               $this->pdo_emulate_prepares = false;
 
                return $this->connected;
        }
@@ -77,10 +80,35 @@ class StaticDatabase extends Database
                return true;
        }
 
+       /** Mock for locking tables */
+       public function lock($table)
+       {
+               if ($this->_locked) {
+                       return false;
+               }
+
+               $this->in_transaction = true;
+               $this->_locked = true;
+
+               return true;
+       }
+
+       /** Mock for unlocking tables */
+       public function unlock()
+       {
+               // See here: https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html
+               $this->performCommit();
+
+               $this->in_transaction = false;
+               $this->_locked = false;
+
+               return true;
+       }
+
        /**
         * Does a commit
         *
-        * @return boolean Was the command executed successfully?
+        * @return bool Was the command executed successfully?
         */
        public function commit()
        {
@@ -96,12 +124,14 @@ class StaticDatabase extends Database
         * Either through explicit calling or through implicit using the Database
         *
         * @param array $server $_SERVER variables
+        *
+        * @throws \Exception
         */
        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'])))
+                   && (!empty($server['MYSQL_USERNAME']) || !empty($server['MYSQL_USER']))
                    && $server['MYSQL_PASSWORD'] !== false
                    && !empty($server['MYSQL_DATABASE']))
                {
@@ -119,6 +149,10 @@ class StaticDatabase extends Database
                        $db_data = $server['MYSQL_DATABASE'];
                }
 
+               if (empty($db_host) || empty($db_user) || empty($db_data)) {
+                       throw new DatabaseException('Either one of the following settings are missing: Host, User or Database', 999, 'CONNECT');
+               }
+
                $port       = 0;
                $serveraddr = trim($db_host);
                $serverdata = explode(':', $serveraddr);
@@ -128,7 +162,7 @@ class StaticDatabase extends Database
                }
                $server  = trim($server);
                $user    = trim($db_user);
-               $pass    = trim($db_pw);
+               $pass    = trim($db_pw ?? '');
                $db      = trim($db_data);
 
                if (!(strlen($server) && strlen($user))) {
@@ -157,18 +191,6 @@ class StaticDatabase extends Database
                return self::$staticConnection;
        }
 
-       /**
-        * Perform a global commit for every nested transaction of the static connection
-        */
-       public static function statCommit()
-       {
-               if (isset(self::$staticConnection)) {
-                       while (self::$staticConnection->getTransactionDepth() > 0) {
-                               self::$staticConnection->commit();
-                       }
-               }
-       }
-
        /**
         * Perform a global rollback for every nested transaction of the static connection
         */