]> git.mxchange.org Git - friendica.git/commitdiff
Refactor Cache/Lock to DICE
authorPhilipp Holzer <admin+github@philipp.info>
Sat, 3 Aug 2019 18:48:56 +0000 (20:48 +0200)
committerPhilipp Holzer <admin+github@philipp.info>
Sat, 3 Aug 2019 18:51:58 +0000 (20:51 +0200)
- Refactor Cache classes
- Refactor Lock classes
- Improved test speed (removed some seperate class annotations)

40 files changed:
src/Console/Cache.php
src/Core/Cache.php
src/Core/Cache/APCuCache.php
src/Core/Cache/AbstractCacheDriver.php
src/Core/Cache/ArrayCache.php
src/Core/Cache/DatabaseCacheDriver.php
src/Core/Cache/ICacheDriver.php
src/Core/Cache/IMemoryCacheDriver.php
src/Core/Cache/MemcacheCacheDriver.php
src/Core/Cache/MemcachedCacheDriver.php
src/Core/Cache/ProfilerCache.php [new file with mode: 0644]
src/Core/Cache/RedisCacheDriver.php
src/Core/Lock.php
src/Core/Lock/AbstractLockDriver.php
src/Core/Lock/CacheLockDriver.php
src/Core/Lock/DatabaseLockDriver.php
src/Core/Lock/ILockDriver.php
src/Factory/CacheDriverFactory.php
src/Factory/LockDriverFactory.php [new file with mode: 0644]
static/dependencies.config.php
tests/DatabaseTest.php
tests/DatabaseTestTrait.php [new file with mode: 0644]
tests/Util/DbaCacheMockTrait.php
tests/Util/VFSTrait.php
tests/src/Core/Cache/APCuCacheDriverTest.php
tests/src/Core/Cache/ArrayCacheDriverTest.php
tests/src/Core/Cache/CacheTest.php
tests/src/Core/Cache/DatabaseCacheDriverTest.php
tests/src/Core/Cache/MemcacheCacheDriverTest.php
tests/src/Core/Cache/MemcachedCacheDriverTest.php
tests/src/Core/Cache/MemoryCacheTest.php
tests/src/Core/Cache/RedisCacheDriverTest.php
tests/src/Core/Lock/APCuCacheLockDriverTest.php
tests/src/Core/Lock/ArrayCacheLockDriverTest.php
tests/src/Core/Lock/DatabaseLockDriverTest.php
tests/src/Core/Lock/LockTest.php
tests/src/Core/Lock/MemcacheCacheLockDriverTest.php
tests/src/Core/Lock/MemcachedCacheLockDriverTest.php
tests/src/Core/Lock/RedisCacheLockDriverTest.php
tests/src/Core/Lock/SemaphoreLockDriverTest.php

index e2e1dba6c01f8932279a275c8c48fd5cd0f1f8dd..7596ae6d51acf63ea48f1869f14484917d960f9e 100644 (file)
@@ -4,7 +4,9 @@ namespace Friendica\Console;
 
 use Asika\SimpleConsole\CommandArgsException;
 use Friendica\App;
-use Friendica\Core;
+use Friendica\Core\Cache\ICacheDriver;
+use Friendica\Core\Config\Configuration;
+use Friendica\Factory\CacheDriverFactory;
 use RuntimeException;
 
 /**
@@ -25,6 +27,16 @@ class Cache extends \Asika\SimpleConsole\Console
         */
        private $appMode;
 
+       /**
+        * @var string The cache driver name
+        */
+       private $cacheDriverName;
+
+       /**
+        * @var ICacheDriver
+        */
+       private $cache;
+
        protected function getHelp()
        {
                $help = <<<HELP
@@ -59,11 +71,13 @@ HELP;
                return $help;
        }
 
-       public function __construct(App\Mode $appMode, array $argv = null)
+       public function __construct(App\Mode $appMode, Configuration $config, ICacheDriver $cache, array $argv = null)
        {
                parent::__construct($argv);
 
                $this->appMode = $appMode;
+               $this->cache = $cache;
+               $this->cacheDriverName = $config->get('system', 'cache_driver', CacheDriverFactory::DEFAULT_DRIVER);
        }
 
        protected function doExecute()
@@ -79,11 +93,9 @@ HELP;
                        $this->out('Database isn\'t ready or populated yet, database cache won\'t be available');
                }
 
-               Core\Cache::init();
-
                if ($this->getOption('v')) {
-                       $this->out('Cache Driver Name: ' . Core\Cache::$driver_name);
-                       $this->out('Cache Driver Class: ' . Core\Cache::$driver_class);
+                       $this->out('Cache Driver Name: ' . $this->cacheDriverName);
+                       $this->out('Cache Driver Class: ' . get_class($this->cache));
                }
 
                switch ($this->getArgument(0)) {
@@ -115,7 +127,7 @@ HELP;
        private function executeList()
        {
                $prefix = $this->getArgument(1);
-               $keys = Core\Cache::getAllKeys($prefix);
+               $keys = $this->cache->getAllKeys($prefix);
 
                if (empty($prefix)) {
                        $this->out('Listing all cache keys:');
@@ -136,7 +148,7 @@ HELP;
        {
                if (count($this->args) >= 2) {
                        $key = $this->getArgument(1);
-                       $value = Core\Cache::get($key);
+                       $value = $this->cache->get($key);
 
                        $this->out("{$key} => " . var_export($value, true));
                } else {
@@ -149,15 +161,15 @@ HELP;
                if (count($this->args) >= 3) {
                        $key = $this->getArgument(1);
                        $value = $this->getArgument(2);
-                       $duration = intval($this->getArgument(3, Core\Cache::FIVE_MINUTES));
+                       $duration = intval($this->getArgument(3, ICacheDriver::FIVE_MINUTES));
 
-                       if (is_array(Core\Cache::get($key))) {
+                       if (is_array($this->cache->get($key))) {
                                throw new RuntimeException("$key is an array and can't be set using this command.");
                        }
 
-                       $result = Core\Cache::set($key, $value, $duration);
+                       $result = $this->cache->set($key, $value, $duration);
                        if ($result) {
-                               $this->out("{$key} <= " . Core\Cache::get($key));
+                               $this->out("{$key} <= " . $this->cache->get($key));
                        } else {
                                $this->out("Unable to set {$key}");
                        }
@@ -168,7 +180,7 @@ HELP;
 
        private function executeFlush()
        {
-               $result = Core\Cache::clear();
+               $result = $this->cache->clear();
                if ($result) {
                        $this->out('Cache successfully flushed');
                } else {
@@ -178,7 +190,7 @@ HELP;
 
        private function executeClear()
        {
-               $result = Core\Cache::clear(false);
+               $result = $this->cache->clear(false);
                if ($result) {
                        $this->out('Cache successfully cleared');
                } else {
index 7a8f7367ec10597613b7ed94385bfdf85c2464f0..0c5597aa5e66372e2322f7e683cb1e6c831c092e 100644 (file)
@@ -4,51 +4,33 @@
  */
 namespace Friendica\Core;
 
-use Friendica\Factory\CacheDriverFactory;
+use Friendica\BaseObject;
+use Friendica\Core\Cache\ICacheDriver;
 
 /**
  * @brief Class for storing data for a short time
  */
-class Cache extends \Friendica\BaseObject
+class Cache extends BaseObject
 {
-       const MONTH        = 2592000;
+       /** @deprecated Use ICacheDriver::MONTH */
+       const MONTH        = ICacheDriver::MONTH;
+       /** @deprecated Use ICacheDriver::WEEK */
        const WEEK         = 604800;
+       /** @deprecated Use ICacheDriver::DAY */
        const DAY          = 86400;
+       /** @deprecated Use ICacheDriver::HOUR */
        const HOUR         = 3600;
+       /** @deprecated Use ICacheDriver::HALF_HOUR */
        const HALF_HOUR    = 1800;
+       /** @deprecated Use ICacheDriver::QUARTER_HOUR */
        const QUARTER_HOUR = 900;
+       /** @deprecated Use ICacheDriver::FIVE_MINUTES */
        const FIVE_MINUTES = 300;
+       /** @deprecated Use ICacheDriver::MINUTE */
        const MINUTE       = 60;
+       /** @deprecated Use ICacheDriver::INFINITE */
        const INFINITE     = 0;
 
-       /**
-        * @var Cache\ICacheDriver
-        */
-       private static $driver       = null;
-       public  static $driver_class = null;
-       public  static $driver_name  = null;
-
-       public static function init()
-       {
-               self::$driver_name  = Config::get('system', 'cache_driver', 'database');
-               self::$driver       = CacheDriverFactory::create(self::$driver_name);
-               self::$driver_class = get_class(self::$driver);
-       }
-
-       /**
-        * Returns the current cache driver
-        *
-        * @return Cache\ICacheDriver
-        */
-       private static function getDriver()
-       {
-               if (self::$driver === null) {
-                       self::init();
-               }
-
-               return self::$driver;
-       }
-
        /**
         * @brief Returns all the cache keys sorted alphabetically
         *
@@ -59,13 +41,7 @@ class Cache extends \Friendica\BaseObject
         */
        public static function getAllKeys($prefix = null)
        {
-               $time = microtime(true);
-
-               $return = self::getDriver()->getAllKeys($prefix);
-
-               self::getApp()->getProfiler()->saveTimestamp($time, 'cache', System::callstack());
-
-               return $return;
+               return self::getClass(ICacheDriver::class)->getAllKeys($prefix);
        }
 
        /**
@@ -78,13 +54,7 @@ class Cache extends \Friendica\BaseObject
         */
        public static function get($key)
        {
-               $time = microtime(true);
-
-               $return = self::getDriver()->get($key);
-
-               self::getApp()->getProfiler()->saveTimestamp($time, 'cache', System::callstack());
-
-               return $return;
+               return self::getClass(ICacheDriver::class)->get($key);
        }
 
        /**
@@ -99,15 +69,9 @@ class Cache extends \Friendica\BaseObject
         * @return bool
         * @throws \Exception
         */
-       public static function set($key, $value, $duration = self::MONTH)
+       public static function set($key, $value, $duration = ICacheDriver::MONTH)
        {
-               $time = microtime(true);
-
-               $return = self::getDriver()->set($key, $value, $duration);
-
-               self::getApp()->getProfiler()->saveTimestamp($time, 'cache_write', System::callstack());
-
-               return $return;
+               return self::getClass(ICacheDriver::class)->set($key, $value, $duration);
        }
 
        /**
@@ -120,13 +84,7 @@ class Cache extends \Friendica\BaseObject
         */
        public static function delete($key)
        {
-               $time = microtime(true);
-
-               $return = self::getDriver()->delete($key);
-
-               self::getApp()->getProfiler()->saveTimestamp($time, 'cache_write', System::callstack());
-
-               return $return;
+               return self::getClass(ICacheDriver::class)->delete($key);
        }
 
        /**
@@ -135,9 +93,10 @@ class Cache extends \Friendica\BaseObject
         * @param boolean $outdated just remove outdated values
         *
         * @return bool
+        * @throws \Exception
         */
        public static function clear($outdated = true)
        {
-               return self::getDriver()->clear($outdated);
+               return self::getClass(ICacheDriver::class)->clear($outdated);
        }
 }
index f658424cdcb7a907b365b8e5f8f143593f5e0633..b89d7fe4b3f95181cd75090fdd56332538ddee92 100644 (file)
@@ -18,11 +18,13 @@ class APCuCache extends AbstractCacheDriver implements IMemoryCacheDriver
        /**
         * @throws Exception
         */
-       public function __construct()
+       public function __construct(string $hostname)
        {
                if (!self::isAvailable()) {
                        throw new Exception('APCu is not available.');
                }
+
+               parent::__construct($hostname);
        }
 
        /**
index f238a7819cfc2a1d63181e9c78c11838f87ed4f6..e12f63bfa44daecd305062e705011d60c3335b2d 100644 (file)
@@ -1,8 +1,6 @@
 <?php
 
 namespace Friendica\Core\Cache;
-use Friendica\BaseObject;
-
 
 /**
  * Abstract class for common used functions
@@ -11,8 +9,18 @@ use Friendica\BaseObject;
  *
  * @package Friendica\Core\Cache
  */
-abstract class AbstractCacheDriver extends BaseObject
+abstract class AbstractCacheDriver implements ICacheDriver
 {
+       /**
+        * @var string The hostname
+        */
+       private $hostName;
+
+       public function __construct(string $hostName)
+       {
+               $this->hostName = $hostName;
+       }
+
        /**
         * Returns the prefix (to avoid namespace conflicts)
         *
@@ -22,7 +30,7 @@ abstract class AbstractCacheDriver extends BaseObject
        protected function getPrefix()
        {
                // We fetch with the hostname as key to avoid problems with other applications
-               return self::getApp()->getHostName();
+               return $this->hostName;
        }
 
        /**
@@ -46,7 +54,7 @@ abstract class AbstractCacheDriver extends BaseObject
                } else {
                        // Keys are prefixed with the node hostname, let's remove it
                        array_walk($keys, function (&$value) {
-                               $value = preg_replace('/^' . self::getApp()->getHostName() . ':/', '', $value);
+                               $value = preg_replace('/^' . $this->hostName . ':/', '', $value);
                        });
 
                        sort($keys);
@@ -79,6 +87,5 @@ abstract class AbstractCacheDriver extends BaseObject
 
                        return $result;
                }
-
        }
 }
index a99b05788f64e75e46d5d8e4df8c230bdcf6c414..f2f37708de82d58a35c30a7fab01b435f9e132b1 100644 (file)
@@ -2,7 +2,6 @@
 
 namespace Friendica\Core\Cache;
 
-
 use Friendica\Core\Cache;
 
 /**
index f6f5b6486cb6b7c3d02d4af991a84cc955758da5..f1d6150701c00e2f320f8cd45e3b7b95b5cdf890 100644 (file)
@@ -3,7 +3,7 @@
 namespace Friendica\Core\Cache;
 
 use Friendica\Core\Cache;
-use Friendica\Database\DBA;
+use Friendica\Database\Database;
 use Friendica\Util\DateTimeFormat;
 
 /**
@@ -13,6 +13,18 @@ use Friendica\Util\DateTimeFormat;
  */
 class DatabaseCacheDriver extends AbstractCacheDriver implements ICacheDriver
 {
+       /**
+        * @var Database
+        */
+       private $dba;
+
+       public function __construct(string $hostname, Database $dba)
+       {
+               parent::__construct($hostname);
+
+               $this->dba = $dba;
+       }
+
        /**
         * (@inheritdoc)
         */
@@ -24,13 +36,13 @@ class DatabaseCacheDriver extends AbstractCacheDriver implements ICacheDriver
                        $where = ['`expires` >= ? AND `k` LIKE CONCAT(?, \'%\')', DateTimeFormat::utcNow(), $prefix];
                }
 
-               $stmt = DBA::select('cache', ['k'], $where);
+               $stmt = $this->dba->select('cache', ['k'], $where);
 
                $keys = [];
-               while ($key = DBA::fetch($stmt)) {
+               while ($key = $this->dba->fetch($stmt)) {
                        array_push($keys, $key['k']);
                }
-               DBA::close($stmt);
+               $this->dba->close($stmt);
 
                return $keys;
        }
@@ -40,9 +52,9 @@ class DatabaseCacheDriver extends AbstractCacheDriver implements ICacheDriver
         */
        public function get($key)
        {
-               $cache = DBA::selectFirst('cache', ['v'], ['`k` = ? AND (`expires` >= ? OR `expires` = -1)', $key, DateTimeFormat::utcNow()]);
+               $cache = $this->dba->selectFirst('cache', ['v'], ['`k` = ? AND (`expires` >= ? OR `expires` = -1)', $key, DateTimeFormat::utcNow()]);
 
-               if (DBA::isResult($cache)) {
+               if ($this->dba->isResult($cache)) {
                        $cached = $cache['v'];
                        $value = @unserialize($cached);
 
@@ -76,7 +88,7 @@ class DatabaseCacheDriver extends AbstractCacheDriver implements ICacheDriver
                        ];
                }
 
-               return DBA::update('cache', $fields, ['k' => $key], true);
+               return $this->dba->update('cache', $fields, ['k' => $key], true);
        }
 
        /**
@@ -84,7 +96,7 @@ class DatabaseCacheDriver extends AbstractCacheDriver implements ICacheDriver
         */
        public function delete($key)
        {
-               return DBA::delete('cache', ['k' => $key]);
+               return $this->dba->delete('cache', ['k' => $key]);
        }
 
        /**
@@ -93,9 +105,9 @@ class DatabaseCacheDriver extends AbstractCacheDriver implements ICacheDriver
        public function clear($outdated = true)
        {
                if ($outdated) {
-                       return DBA::delete('cache', ['`expires` < NOW()']);
+                       return $this->dba->delete('cache', ['`expires` < NOW()']);
                } else {
-                       return DBA::delete('cache', ['`k` IS NOT NULL ']);
+                       return $this->dba->delete('cache', ['`k` IS NOT NULL ']);
                }
        }
 }
index 1188e51877f65e2fecc40ad36a68cf67b44d9209..34b741968afb41996af4e4a4d2c624775adef232 100644 (file)
@@ -2,8 +2,6 @@
 
 namespace Friendica\Core\Cache;
 
-use Friendica\Core\Cache;
-
 /**
  * Cache Driver Interface
  *
@@ -11,6 +9,16 @@ use Friendica\Core\Cache;
  */
 interface ICacheDriver
 {
+       const MONTH        = 2592000;
+       const WEEK         = 604800;
+       const DAY          = 86400;
+       const HOUR         = 3600;
+       const HALF_HOUR    = 1800;
+       const QUARTER_HOUR = 900;
+       const FIVE_MINUTES = 300;
+       const MINUTE       = 60;
+       const INFINITE     = 0;
+
        /**
         * Lists all cache keys
         *
@@ -38,7 +46,7 @@ interface ICacheDriver
         *
         * @return bool
         */
-       public function set($key, $value, $ttl = Cache::FIVE_MINUTES);
+       public function set($key, $value, $ttl = self::FIVE_MINUTES);
 
        /**
         * Delete a key from the cache
index 0c5146f4399a01b186406eb24653bfef8d8de924..e35159a3a601272b877455dc1db16fb4914931a7 100644 (file)
@@ -1,7 +1,6 @@
 <?php
 
 namespace Friendica\Core\Cache;
-use Friendica\Core\Cache;
 
 /**
  * This interface defines methods for Memory-Caches only
@@ -20,7 +19,7 @@ interface IMemoryCacheDriver extends ICacheDriver
         * @param int    $ttl      The cache lifespan, must be one of the Cache constants
         * @return bool
         */
-       public function add($key, $value, $ttl = Cache::FIVE_MINUTES);
+       public function add($key, $value, $ttl = ICacheDriver::FIVE_MINUTES);
 
        /**
         * Compares if the old value is set and sets the new value
@@ -32,7 +31,7 @@ interface IMemoryCacheDriver extends ICacheDriver
         *
         * @return bool
         */
-       public function compareSet($key, $oldValue, $newValue, $ttl = Cache::FIVE_MINUTES);
+       public function compareSet($key, $oldValue, $newValue, $ttl = ICacheDriver::FIVE_MINUTES);
 
        /**
         * Compares if the old value is set and removes it
index fd928c6fcc15ed2a5245f2bca27c664a6f696531..bf8c34a41693d8d6227eaf11d12cb3f367eed813 100644 (file)
@@ -2,9 +2,9 @@
 
 namespace Friendica\Core\Cache;
 
-use Friendica\Core\Cache;
-
 use Exception;
+use Friendica\Core\Cache;
+use Friendica\Core\Config\Configuration;
 use Memcache;
 
 /**
@@ -23,18 +23,21 @@ class MemcacheCacheDriver extends AbstractCacheDriver implements IMemoryCacheDri
        private $memcache;
 
        /**
-        * @param string $memcache_host
-        * @param int    $memcache_port
         * @throws Exception
         */
-       public function __construct($memcache_host, $memcache_port)
+       public function __construct(string $hostname, Configuration $config)
        {
                if (!class_exists('Memcache', false)) {
                        throw new Exception('Memcache class isn\'t available');
                }
 
+               parent::__construct($hostname);
+
                $this->memcache = new Memcache();
 
+               $memcache_host = $config->get('system', 'memcache_host');
+               $memcache_port = $config->get('system', 'memcache_port');
+
                if (!$this->memcache->connect($memcache_host, $memcache_port)) {
                        throw new Exception('Expected Memcache server at ' . $memcache_host . ':' . $memcache_port . ' isn\'t available');
                }
index 687e67416acf3314c9d1655e5c63d2382e84cd86..7b3a2d5dfa4f9f42d2b01c805d33df828b4fdb8d 100644 (file)
@@ -2,11 +2,11 @@
 
 namespace Friendica\Core\Cache;
 
-use Friendica\Core\Cache;
-use Friendica\Core\Logger;
-
 use Exception;
+use Friendica\Core\Cache;
+use Friendica\Core\Config\Configuration;
 use Memcached;
+use Psr\Log\LoggerInterface;
 
 /**
  * Memcached Cache Driver
@@ -23,6 +23,11 @@ class MemcachedCacheDriver extends AbstractCacheDriver implements IMemoryCacheDr
         */
        private $memcached;
 
+       /**
+        * @var LoggerInterface
+        */
+       private $logger;
+
        /**
         * Due to limitations of the INI format, the expected configuration for Memcached servers is the following:
         * array {
@@ -33,14 +38,20 @@ class MemcachedCacheDriver extends AbstractCacheDriver implements IMemoryCacheDr
         * @param array $memcached_hosts
         * @throws \Exception
         */
-       public function __construct(array $memcached_hosts)
+       public function __construct(string $hostname, Configuration $config, LoggerInterface $logger)
        {
                if (!class_exists('Memcached', false)) {
                        throw new Exception('Memcached class isn\'t available');
                }
 
+               parent::__construct($hostname);
+
+               $this->logger = $logger;
+
                $this->memcached = new Memcached();
 
+               $memcached_hosts = $config->get('system', 'memcached_hosts');
+
                array_walk($memcached_hosts, function (&$value) {
                        if (is_string($value)) {
                                $value = array_map('trim', explode(',', $value));
@@ -64,7 +75,7 @@ class MemcachedCacheDriver extends AbstractCacheDriver implements IMemoryCacheDr
                if ($this->memcached->getResultCode() == Memcached::RES_SUCCESS) {
                        return $this->filterArrayKeysByPrefix($keys, $prefix);
                } else {
-                       Logger::log('Memcached \'getAllKeys\' failed with ' . $this->memcached->getResultMessage(), Logger::ALL);
+                       $this->logger->debug('Memcached \'getAllKeys\' failed', ['result' =>  $this->memcached->getResultMessage()]);
                        return [];
                }
        }
@@ -83,7 +94,7 @@ class MemcachedCacheDriver extends AbstractCacheDriver implements IMemoryCacheDr
                if ($this->memcached->getResultCode() === Memcached::RES_SUCCESS) {
                        $return = $value;
                } else {
-                       Logger::log('Memcached \'get\' failed with ' . $this->memcached->getResultMessage(), Logger::ALL);
+                       $this->logger->debug('Memcached \'get\' failed', ['result' =>  $this->memcached->getResultMessage()]);
                }
 
                return $return;
diff --git a/src/Core/Cache/ProfilerCache.php b/src/Core/Cache/ProfilerCache.php
new file mode 100644 (file)
index 0000000..04271e7
--- /dev/null
@@ -0,0 +1,155 @@
+<?php
+
+namespace Friendica\Core\Cache;
+
+use Friendica\Core\Cache;
+use Friendica\Core\System;
+use Friendica\Util\Profiler;
+
+/**
+ * This class wraps cache driver so they can get profiled - in case the profiler is enabled
+ *
+ * It is using the decorator pattern (@see
+ */
+class ProfilerCache implements ICacheDriver, IMemoryCacheDriver
+{
+       /**
+        * @var ICacheDriver The original cache driver
+        */
+       private $cache;
+
+       /**
+        * @var Profiler The profiler of Friendica
+        */
+       private $profiler;
+
+       public function __construct(ICacheDriver $cache, Profiler $profiler)
+       {
+               $this->cache    = $cache;
+               $this->profiler = $profiler;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public function getAllKeys($prefix = null)
+       {
+               $time = microtime(true);
+
+               $return = $this->cache->getAllKeys($prefix);
+
+               $this->profiler->saveTimestamp($time, 'cache', System::callstack());
+
+               return $return;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public function get($key)
+       {
+               $time = microtime(true);
+
+               $return = $this->cache->get($key);
+
+               $this->profiler->saveTimestamp($time, 'cache', System::callstack());
+
+               return $return;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
+       {
+               $time = microtime(true);
+
+               $return = $this->cache->set($key, $value, $ttl);
+
+               $this->profiler->saveTimestamp($time, 'cache', System::callstack());
+
+               return $return;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public function delete($key)
+       {
+               $time = microtime(true);
+
+               $return = $this->cache->delete($key);
+
+               $this->profiler->saveTimestamp($time, 'cache', System::callstack());
+
+               return $return;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public function clear($outdated = true)
+       {
+               $time = microtime(true);
+
+               $return = $this->cache->clear($outdated);
+
+               $this->profiler->saveTimestamp($time, 'cache', System::callstack());
+
+               return $return;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
+       {
+               if ($this->cache instanceof IMemoryCacheDriver) {
+                       $time = microtime(true);
+
+                       $return = $this->cache->add($key, $value, $ttl);
+
+                       $this->profiler->saveTimestamp($time, 'cache', System::callstack());
+
+                       return $return;
+               } else {
+                       return false;
+               }
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public function compareSet($key, $oldValue, $newValue, $ttl = Cache::FIVE_MINUTES)
+       {
+               if ($this->cache instanceof IMemoryCacheDriver) {
+                       $time = microtime(true);
+
+                       $return = $this->cache->compareSet($key, $oldValue, $newValue, $ttl);
+
+                       $this->profiler->saveTimestamp($time, 'cache', System::callstack());
+
+                       return $return;
+               } else {
+                       return false;
+               }
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public function compareDelete($key, $value)
+       {
+               if ($this->cache instanceof IMemoryCacheDriver) {
+                       $time = microtime(true);
+
+                       $return = $this->cache->compareDelete($key, $value);
+
+                       $this->profiler->saveTimestamp($time, 'cache', System::callstack());
+
+                       return $return;
+               } else {
+                       return false;
+               }
+       }
+}
index ea4eb4390a629c9c2a9b42fef9baeddcc659df2b..951f51dc1337de034093c45b827fe7c172b90d69 100644 (file)
@@ -4,6 +4,7 @@ namespace Friendica\Core\Cache;
 
 use Exception;
 use Friendica\Core\Cache;
+use Friendica\Core\Config\Configuration;
 use Redis;
 
 /**
@@ -20,20 +21,23 @@ class RedisCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver
        private $redis;
 
        /**
-        * @param string  $redis_host
-        * @param int     $redis_port
-        * @param int     $redis_db (Default = 0, maximum is 15)
-        * @param string? $redis_pw
         * @throws Exception
         */
-       public function __construct($redis_host, $redis_port, $redis_db = 0, $redis_pw = null)
+       public function __construct(string $hostname, Configuration $config)
        {
                if (!class_exists('Redis', false)) {
                        throw new Exception('Redis class isn\'t available');
                }
 
+               parent::__construct($hostname);
+
                $this->redis = new Redis();
 
+               $redis_host = $config->get('system', 'redis_host');
+               $redis_port = $config->get('system', 'redis_port');
+               $redis_pw   = $config->get('system', 'redis_password');
+               $redis_db   = $config->get('system', 'redis_db', 0);
+
                if (!$this->redis->connect($redis_host, $redis_port)) {
                        throw new Exception('Expected Redis server at ' . $redis_host . ':' . $redis_port . ' isn\'t available');
                }
index a45490bf3966065990476741e64340910bb78582..04dfaa1c5edfbaf8b8f773b391f4899425a07de3 100644 (file)
 
 namespace Friendica\Core;
 
-use Friendica\Factory\CacheDriverFactory;
-use Friendica\Core\Cache\IMemoryCacheDriver;
+use Friendica\BaseObject;
+use Friendica\Core\Cache\ICacheDriver;
+use Friendica\Core\Lock\ILockDriver;
 
 /**
- * @brief This class contain Functions for preventing parallel execution of functions
+ * This class contain Functions for preventing parallel execution of functions
  */
-class Lock
+class Lock extends BaseObject
 {
-       /**
-        * @var Lock\ILockDriver;
-        */
-       static $driver = null;
-
-       public static function init()
-       {
-               $lock_driver = Config::get('system', 'lock_driver', 'default');
-
-               try {
-                       switch ($lock_driver) {
-                               case 'memcache':
-                               case 'memcached':
-                               case 'redis':
-                                       $cache_driver = CacheDriverFactory::create($lock_driver);
-                                       if ($cache_driver instanceof IMemoryCacheDriver) {
-                                               self::$driver = new Lock\CacheLockDriver($cache_driver);
-                                       }
-                                       break;
-
-                               case 'database':
-                                       self::$driver = new Lock\DatabaseLockDriver();
-                                       break;
-
-                               case 'semaphore':
-                                       self::$driver = new Lock\SemaphoreLockDriver();
-                                       break;
-
-                               default:
-                                       self::useAutoDriver();
-                       }
-               } catch (\Exception $exception) {
-                       Logger::log('Driver \'' . $lock_driver . '\' failed - Fallback to \'useAutoDriver()\'');
-                       self::useAutoDriver();
-               }
-       }
-
-       /**
-        * @brief This method tries to find the best - local - locking method for Friendica
-        *
-        * The following sequence will be tried:
-        * 1. Semaphore Locking
-        * 2. Cache Locking
-        * 3. Database Locking
-        *
-        */
-       private static function useAutoDriver() {
-
-               // 1. Try to use Semaphores for - local - locking
-               if (function_exists('sem_get')) {
-                       try {
-                               self::$driver = new Lock\SemaphoreLockDriver();
-                               return;
-                       } catch (\Exception $exception) {
-                               Logger::log('Using Semaphore driver for locking failed: ' . $exception->getMessage());
-                       }
-               }
-
-               // 2. Try to use Cache Locking (don't use the DB-Cache Locking because it works different!)
-               $cache_driver = Config::get('system', 'cache_driver', 'database');
-               if ($cache_driver != 'database') {
-                       try {
-                               $lock_driver = CacheDriverFactory::create($cache_driver);
-                               if ($lock_driver instanceof IMemoryCacheDriver) {
-                                       self::$driver = new Lock\CacheLockDriver($lock_driver);
-                               }
-                               return;
-                       } catch (\Exception $exception) {
-                               Logger::log('Using Cache driver for locking failed: ' . $exception->getMessage());
-                       }
-               }
-
-               // 3. Use Database Locking as a Fallback
-               self::$driver = new Lock\DatabaseLockDriver();
-       }
-
-       /**
-        * Returns the current cache driver
-        *
-        * @return Lock\ILockDriver;
-        */
-       private static function getDriver()
-       {
-               if (self::$driver === null) {
-                       self::init();
-               }
-
-               return self::$driver;
-       }
-
        /**
         * @brief Acquires a lock for a given name
         *
-        * @param string  $key Name of the lock
+        * @param string  $key     Name of the lock
         * @param integer $timeout Seconds until we give up
-        * @param integer $ttl The Lock lifespan, must be one of the Cache constants
+        * @param integer $ttl     The Lock lifespan, must be one of the Cache constants
         *
         * @return boolean Was the lock successful?
+        * @throws \Exception
         */
-       public static function acquire($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES)
+       public static function acquire($key, $timeout = 120, $ttl = ICacheDriver::FIVE_MINUTES)
        {
-               return self::getDriver()->acquireLock($key, $timeout, $ttl);
+               return self::getClass(ILockDriver::class)->acquireLock($key, $timeout, $ttl);
        }
 
        /**
@@ -124,19 +36,22 @@ class Lock
         *
         * @param string $key      Name of the lock
         * @param bool   $override Overrides the lock to get releases
+        *
         * @return void
+        * @throws \Exception
         */
        public static function release($key, $override = false)
        {
-               self::getDriver()->releaseLock($key, $override);
+               return self::getClass(ILockDriver::class)->releaseLock($key, $override);
        }
 
        /**
         * @brief Releases all lock that were set by us
         * @return void
+        * @throws \Exception
         */
        public static function releaseAll()
        {
-               self::getDriver()->releaseAll();
+               self::getClass(ILockDriver::class)->releaseAll();
        }
 }
index 0aedeeb1b09ab61cdf71c144c7df3b6cd02e6263..71ad08d0888568574e8ae3da5c0943d2ff8b5bdf 100644 (file)
@@ -1,7 +1,6 @@
 <?php
 
 namespace Friendica\Core\Lock;
-use Friendica\BaseObject;
 
 /**
  * Class AbstractLockDriver
@@ -10,7 +9,7 @@ use Friendica\BaseObject;
  *
  * Basic class for Locking with common functions (local acquired locks, releaseAll, ..)
  */
-abstract class AbstractLockDriver extends BaseObject implements ILockDriver
+abstract class AbstractLockDriver implements ILockDriver
 {
        /**
         * @var array The local acquired locks
@@ -21,6 +20,7 @@ abstract class AbstractLockDriver extends BaseObject implements ILockDriver
         * Check if we've locally acquired a lock
         *
         * @param string key The Name of the lock
+        *
         * @return bool      Returns true if the lock is set
         */
        protected function hasAcquiredLock($key)
index 6f34a0d6a948b9bdd3c5fafd6302675490d6591c..6169933ddc86c4aaed6a9041cd64198af18f13cd 100644 (file)
@@ -28,7 +28,7 @@ class CacheLockDriver extends AbstractLockDriver
        public function acquireLock($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES)
        {
                $got_lock = false;
-               $start = time();
+               $start    = time();
 
                $cachekey = self::getLockKey($key);
 
@@ -65,8 +65,6 @@ class CacheLockDriver extends AbstractLockDriver
        {
                $cachekey = self::getLockKey($key);
 
-               $return = false;
-
                if ($override) {
                        $return = $this->cache->delete($cachekey);
                } else {
@@ -83,15 +81,17 @@ class CacheLockDriver extends AbstractLockDriver
        public function isLocked($key)
        {
                $cachekey = self::getLockKey($key);
-               $lock = $this->cache->get($cachekey);
+               $lock     = $this->cache->get($cachekey);
                return isset($lock) && ($lock !== false);
        }
 
        /**
-        * @param string $key   The original key
-        * @return string               The cache key used for the cache
+        * @param string $key The original key
+        *
+        * @return string        The cache key used for the cache
         */
-       private static function getLockKey($key) {
+       private static function getLockKey($key)
+       {
                return "lock:" . $key;
        }
 }
index a8269bc92ea6ad16004c9dcdad2447e569d0f8a5..e9e0500bb47735a33a06f93bc9c03091f94b624f 100644 (file)
@@ -3,7 +3,7 @@
 namespace Friendica\Core\Lock;
 
 use Friendica\Core\Cache;
-use Friendica\Database\DBA;
+use Friendica\Database\Database;
 use Friendica\Util\DateTimeFormat;
 
 /**
@@ -18,11 +18,17 @@ class DatabaseLockDriver extends AbstractLockDriver
         */
        private $pid;
 
+       /**
+        * @var Database The database connection of Friendica
+        */
+       private $dba;
+
        /**
         * @param null|int $pid The Id of the current process (null means determine automatically)
         */
-       public function __construct($pid = null)
+       public function __construct(Database $dba, $pid = null)
        {
+               $this->dba = $dba;
                $this->pid = isset($pid) ? $pid : getmypid();
        }
 
@@ -32,13 +38,13 @@ class DatabaseLockDriver extends AbstractLockDriver
        public function acquireLock($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES)
        {
                $got_lock = false;
-               $start = time();
+               $start    = time();
 
                do {
-                       DBA::lock('locks');
-                       $lock = DBA::selectFirst('locks', ['locked', 'pid'], ['`name` = ? AND `expires` >= ?', $key, DateTimeFormat::utcNow()]);
+                       $this->dba->lock('locks');
+                       $lock = $this->dba->selectFirst('locks', ['locked', 'pid'], ['`name` = ? AND `expires` >= ?', $key, DateTimeFormat::utcNow()]);
 
-                       if (DBA::isResult($lock)) {
+                       if ($this->dba->isResult($lock)) {
                                if ($lock['locked']) {
                                        // We want to lock something that was already locked by us? So we got the lock.
                                        if ($lock['pid'] == $this->pid) {
@@ -46,16 +52,16 @@ class DatabaseLockDriver extends AbstractLockDriver
                                        }
                                }
                                if (!$lock['locked']) {
-                                       DBA::update('locks', ['locked' => true, 'pid' => $this->pid, 'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds')], ['name' => $key]);
+                                       $this->dba->update('locks', ['locked' => true, 'pid' => $this->pid, 'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds')], ['name' => $key]);
                                        $got_lock = true;
                                }
                        } else {
-                               DBA::insert('locks', ['name' => $key, 'locked' => true, 'pid' => $this->pid, 'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds')]);
+                               $this->dba->insert('locks', ['name' => $key, 'locked' => true, 'pid' => $this->pid, 'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds')]);
                                $got_lock = true;
                                $this->markAcquire($key);
                        }
 
-                       DBA::unlock();
+                       $this->dba->unlock();
 
                        if (!$got_lock && ($timeout > 0)) {
                                usleep(rand(100000, 2000000));
@@ -76,7 +82,7 @@ class DatabaseLockDriver extends AbstractLockDriver
                        $where = ['name' => $key, 'pid' => $this->pid];
                }
 
-               $return = DBA::delete('locks', $where);
+               $return = $this->dba->delete('locks', $where);
 
                $this->markRelease($key);
 
@@ -88,7 +94,7 @@ class DatabaseLockDriver extends AbstractLockDriver
         */
        public function releaseAll()
        {
-               $return = DBA::delete('locks', ['pid' => $this->pid]);
+               $return = $this->dba->delete('locks', ['pid' => $this->pid]);
 
                $this->acquiredLocks = [];
 
@@ -100,9 +106,9 @@ class DatabaseLockDriver extends AbstractLockDriver
         */
        public function isLocked($key)
        {
-               $lock = DBA::selectFirst('locks', ['locked'], ['`name` = ? AND `expires` >= ?', $key, DateTimeFormat::utcNow()]);
+               $lock = $this->dba->selectFirst('locks', ['locked'], ['`name` = ? AND `expires` >= ?', $key, DateTimeFormat::utcNow()]);
 
-               if (DBA::isResult($lock)) {
+               if ($this->dba->isResult($lock)) {
                        return $lock['locked'] !== false;
                } else {
                        return false;
index 7df5b3f87aaad43e7a1064fcc0113100c4485779..43793d62872caf55527504ba33097f578c427506 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 
 namespace Friendica\Core\Lock;
+
 use Friendica\Core\Cache;
 
 /**
@@ -13,7 +14,8 @@ interface ILockDriver
        /**
         * Checks, if a key is currently locked to a or my process
         *
-        * @param string $key           The name of the lock
+        * @param string $key The name of the lock
+        *
         * @return bool
         */
        public function isLocked($key);
@@ -22,13 +24,13 @@ interface ILockDriver
         *
         * Acquires a lock for a given name
         *
-        * @param string  $key      The Name of the lock
-        * @param integer $timeout  Seconds until we give up
-        * @param integer $ttl      Seconds The lock lifespan, must be one of the Cache constants
+        * @param string  $key     The Name of the lock
+        * @param integer $timeout Seconds until we give up
+        * @param integer $ttl     Seconds The lock lifespan, must be one of the Cache constants
         *
         * @return boolean Was the lock successful?
         */
-       public function acquireLock($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES);
+       public function acquireLock($key, $timeout = 120, $ttl = Cache\ICacheDriver::FIVE_MINUTES);
 
        /**
         * Releases a lock if it was set by us
index 390534a70a0a1ed233951fea72e9ee8073d013ff..6b4c878f12740385f596a990c4c031306601cb2a 100644 (file)
@@ -4,7 +4,11 @@ namespace Friendica\Factory;
 
 use Friendica\Core\Cache;
 use Friendica\Core\Cache\ICacheDriver;
-use Friendica\Core\Config;
+use Friendica\Core\Config\Configuration;
+use Friendica\Database\Database;
+use Friendica\Util\BaseURL;
+use Friendica\Util\Profiler;
+use Psr\Log\LoggerInterface;
 
 /**
  * Class CacheDriverFactory
@@ -15,43 +19,79 @@ use Friendica\Core\Config;
  */
 class CacheDriverFactory
 {
+       /**
+        * @var string The default driver for caching
+        */
+       const DEFAULT_DRIVER = 'database';
+
+       /**
+        * @var Configuration The configuration to read parameters out of the config
+        */
+       private $config;
+
+       /**
+        * @var Database The database connection in case that the cache is used the dba connection
+        */
+       private $dba;
+
+       /**
+        * @var string The hostname, used as Prefix for Caching
+        */
+       private $hostname;
+
+       /**
+        * @var Profiler The optional profiler if the cached should be profiled
+        */
+       private $profiler;
+
+       /**
+        * @var LoggerInterface The Friendica Logger
+        */
+       private $logger;
+
+       public function __construct(BaseURL $baseURL, Configuration $config, Database $dba, Profiler $profiler, LoggerInterface $logger)
+       {
+               $this->hostname = $baseURL->getHostname();
+               $this->config = $config;
+               $this->dba = $dba;
+               $this->profiler = $profiler;
+               $this->logger = $logger;
+       }
+
        /**
         * This method creates a CacheDriver for the given cache driver name
         *
-        * @param string $driver The name of the cache driver
         * @return ICacheDriver  The instance of the CacheDriver
         * @throws \Exception    The exception if something went wrong during the CacheDriver creation
         */
-       public static function create($driver) {
+       public function create()
+       {
+               $driver = $this->config->get('system', 'cache_driver', self::DEFAULT_DRIVER);
 
                switch ($driver) {
                        case 'memcache':
-                               $memcache_host = Config::get('system', 'memcache_host');
-                               $memcache_port = Config::get('system', 'memcache_port');
-
-                               return new Cache\MemcacheCacheDriver($memcache_host, $memcache_port);
+                               $cache = new Cache\MemcacheCacheDriver($this->hostname, $this->config);
                                break;
-
                        case 'memcached':
-                               $memcached_hosts = Config::get('system', 'memcached_hosts');
-
-                               return new Cache\MemcachedCacheDriver($memcached_hosts);
+                               $cache = new Cache\MemcachedCacheDriver($this->hostname, $this->config, $this->logger);
                                break;
                        case 'redis':
-                               $redis_host = Config::get('system', 'redis_host');
-                               $redis_port = Config::get('system', 'redis_port');
-                               $redis_pw   = Config::get('system', 'redis_password');
-                               $redis_db   = Config::get('system', 'redis_db', 0);
-
-                               return new Cache\RedisCacheDriver($redis_host, $redis_port, $redis_db, $redis_pw);
+                               $cache = new Cache\RedisCacheDriver($this->hostname, $this->config);
                                break;
-
                        case 'apcu':
-                               return new Cache\APCuCache();
+                               $cache = new Cache\APCuCache($this->hostname);
                                break;
-
                        default:
-                               return new Cache\DatabaseCacheDriver();
+                               $cache = new Cache\DatabaseCacheDriver($this->hostname, $this->dba);
+               }
+
+               $profiling = $this->config->get('system', 'profiling', false);
+
+               // In case profiling is enabled, wrap the ProfilerCache around the current cache
+               if (isset($profiling) && $profiling !== false) {
+                        return new Cache\ProfilerCache($cache, $this->profiler);
+               } else {
+                       return $cache;
                }
        }
 }
diff --git a/src/Factory/LockDriverFactory.php b/src/Factory/LockDriverFactory.php
new file mode 100644 (file)
index 0000000..d31728b
--- /dev/null
@@ -0,0 +1,128 @@
+<?php
+
+namespace Friendica\Factory;
+
+use Friendica\Core\Cache\ICacheDriver;
+use Friendica\Core\Cache\IMemoryCacheDriver;
+use Friendica\Core\Config\Configuration;
+use Friendica\Core\Lock;
+use Friendica\Database\Database;
+use Friendica\Util\Profiler;
+use Psr\Log\LoggerInterface;
+
+/**
+ * Class LockDriverFactory
+ *
+ * @package Friendica\Core\Cache
+ *
+ * A basic class to generate a LockDriver
+ */
+class LockDriverFactory
+{
+       /**
+        * @var string The default driver for caching
+        */
+       const DEFAULT_DRIVER = 'default';
+
+       /**
+        * @var Configuration The configuration to read parameters out of the config
+        */
+       private $config;
+
+       /**
+        * @var Database The database connection in case that the cache is used the dba connection
+        */
+       private $dba;
+
+       /**
+        * @var ICacheDriver The memory cache driver in case we use it
+        */
+       private $cacheDriver;
+
+       /**
+        * @var Profiler The optional profiler if the cached should be profiled
+        */
+       private $profiler;
+
+       /**
+        * @var LoggerInterface The Friendica Logger
+        */
+       private $logger;
+
+       public function __construct(ICacheDriver $cacheDriver, Configuration $config, Database $dba, Profiler $profiler, LoggerInterface $logger)
+       {
+               $this->cacheDriver = $cacheDriver;
+               $this->config      = $config;
+               $this->dba         = $dba;
+               $this->logger      = $logger;
+       }
+
+       public function create()
+       {
+               $lock_driver = $this->config->get('system', 'lock_driver', self::DEFAULT_DRIVER);
+
+               try {
+                       switch ($lock_driver) {
+                               case 'memcache':
+                               case 'memcached':
+                               case 'redis':
+                                       if ($this->cacheDriver instanceof IMemoryCacheDriver) {
+                                               return new Lock\CacheLockDriver($this->cacheDriver);
+                                       }
+                                       break;
+
+                               case 'database':
+                                       return new Lock\DatabaseLockDriver($this->dba);
+                                       break;
+
+                               case 'semaphore':
+                                       return new Lock\SemaphoreLockDriver();
+                                       break;
+
+                               default:
+                                       return self::useAutoDriver();
+                       }
+               } catch (\Exception $exception) {
+                       $this->logger->alert('Driver \'' . $lock_driver . '\' failed - Fallback to \'useAutoDriver()\'', ['exception' => $exception]);
+                       return self::useAutoDriver();
+               }
+       }
+
+       /**
+        * @brief This method tries to find the best - local - locking method for Friendica
+        *
+        * The following sequence will be tried:
+        * 1. Semaphore Locking
+        * 2. Cache Locking
+        * 3. Database Locking
+        *
+        * @return Lock\ILockDriver
+        */
+       private function useAutoDriver()
+       {
+
+               // 1. Try to use Semaphores for - local - locking
+               if (function_exists('sem_get')) {
+                       try {
+                               return new Lock\SemaphoreLockDriver();
+                       } catch (\Exception $exception) {
+                               $this->logger->debug('Using Semaphore driver for locking failed.', ['exception' => $exception]);
+                       }
+               }
+
+               // 2. Try to use Cache Locking (don't use the DB-Cache Locking because it works different!)
+               $cache_driver = $this->config->get('system', 'cache_driver', 'database');
+               if ($cache_driver != 'database') {
+                       try {
+                               if ($this->cacheDriver instanceof IMemoryCacheDriver) {
+                                       return new Lock\CacheLockDriver($this->cacheDriver);
+                               }
+                       } catch (\Exception $exception) {
+                               $this->logger->debug('Using Cache driver for locking failed.', ['exception' => $exception]);
+                       }
+               }
+
+               // 3. Use Database Locking as a Fallback
+               return new Lock\DatabaseLockDriver($this->dba);
+       }
+}
index 475f5cceed785c0bf6d71f645eb8f8be7e8cb4f0..55a2c6845b4bfba72db4a05cfa68bf6d5f7eb1ac 100644 (file)
@@ -2,7 +2,9 @@
 
 use Dice\Dice;
 use Friendica\App;
+use Friendica\Core\Cache;
 use Friendica\Core\Config;
+use Friendica\Core\Lock\ILockDriver;
 use Friendica\Database\Database;
 use Friendica\Factory;
 use Friendica\Util;
@@ -116,4 +118,19 @@ return [
                        ['createDev', [], Dice::CHAIN_CALL],
                ]
        ],
+       Cache\ICacheDriver::class => [
+               'instanceOf' => Factory\CacheDriverFactory::class,
+               'call' => [
+                       ['create', [], Dice::CHAIN_CALL],
+               ],
+       ],
+       Cache\IMemoryCacheDriver::class => [
+               'instanceOf' => Cache\ICacheDriver::class,
+       ],
+       ILockDriver::class => [
+               'instanceOf' => Factory\LockDriverFactory::class,
+               'call' => [
+                       ['create', [], Dice::CHAIN_CALL],
+               ],
+       ],
 ];
index 4f0275493c8f659d28734c99052cd3145080ff2a..7421f16a09c5342e313c12aa70d161577974e2ba 100644 (file)
@@ -5,56 +5,10 @@
 
 namespace Friendica\Test;
 
-use Friendica\Database\Database;
-use Friendica\Test\Util\Database\StaticDatabase;
-
 /**
  * Abstract class used by tests that need a database.
  */
 abstract class DatabaseTest extends MockedTest
 {
-       protected function setUp()
-       {
-               parent::setUp();
-
-               StaticDatabase::statConnect($_SERVER);
-               // Rollbacks every DB usage (in case the test couldn't call tearDown)
-               StaticDatabase::statRollback();
-               // Start the first, outer transaction
-               StaticDatabase::getGlobConnection()->beginTransaction();
-       }
-
-       protected function tearDown()
-       {
-               // Rollbacks every DB usage so we don't commit anything into the DB
-               StaticDatabase::statRollback();
-
-               parent::tearDown();
-       }
-
-       /**
-        * Loads a given DB fixture for this DB test
-        *
-        * @param string   $fixture The path to the fixture
-        * @param Database $dba     The DB connection
-        *
-        * @throws \Exception
-        */
-       protected function loadFixture(string $fixture, Database $dba)
-       {
-               $this->assertFileExists($fixture);
-
-               $data = include $fixture;
-
-               foreach ($data as $tableName => $rows) {
-                       if (!is_array($rows)) {
-                               $dba->p('TRUNCATE TABLE `' . $tableName . '``');
-                               continue;
-                       }
-
-                       foreach ($rows as $row) {
-                               $dba->insert($tableName, $row);
-                       }
-               }
-       }
+       use DatabaseTestTrait;
 }
diff --git a/tests/DatabaseTestTrait.php b/tests/DatabaseTestTrait.php
new file mode 100644 (file)
index 0000000..49dc999
--- /dev/null
@@ -0,0 +1,58 @@
+<?php
+/**
+ * DatabaseTest class.
+ */
+
+namespace Friendica\Test;
+
+use Friendica\Database\Database;
+use Friendica\Test\Util\Database\StaticDatabase;
+
+/**
+ * Abstract class used by tests that need a database.
+ */
+trait DatabaseTestTrait
+{
+       protected function setUp()
+       {
+               StaticDatabase::statConnect($_SERVER);
+               // Rollbacks every DB usage (in case the test couldn't call tearDown)
+               StaticDatabase::statRollback();
+               // Start the first, outer transaction
+               StaticDatabase::getGlobConnection()->beginTransaction();
+
+               parent::setUp();
+       }
+
+       protected function tearDown()
+       {
+               // Rollbacks every DB usage so we don't commit anything into the DB
+               StaticDatabase::statRollback();
+
+               parent::tearDown();
+       }
+
+       /**
+        * Loads a given DB fixture for this DB test
+        *
+        * @param string   $fixture The path to the fixture
+        * @param Database $dba     The DB connection
+        *
+        * @throws \Exception
+        */
+       protected function loadFixture(string $fixture, Database $dba)
+       {
+               $data = include $fixture;
+
+               foreach ($data as $tableName => $rows) {
+                       if (!is_array($rows)) {
+                               $dba->p('TRUNCATE TABLE `' . $tableName . '``');
+                               continue;
+                       }
+
+                       foreach ($rows as $row) {
+                               $dba->insert($tableName, $row);
+                       }
+               }
+       }
+}
index 87ab450c9e8311246964540e8645a5d801b5d26f..95e7cbcb1def2caf2bf3a9d4b85a4a1dcc435342 100644 (file)
@@ -4,8 +4,14 @@ namespace Friendica\Test\Util;
 
 trait DbaCacheMockTrait
 {
-       use DBAMockTrait;
-       use DateTimeFormatMockTrait;
+       /**
+        * @var
+        */
+       protected $dba;
+
+       public function __construct()
+       {
+       }
 
        protected function mockDelete($key, $return = true, $times = null)
        {
index 110f24a61ae38e4a54b8c03dcfcb0ac1b3b0ce2f..565e693c95e322d5502d794acba7341637097de2 100644 (file)
@@ -28,6 +28,7 @@ trait VFSTrait
                // create a virtual directory and copy all needed files and folders to it
                $this->root = vfsStream::setup('friendica', 0777, $structure);
 
+               $this->setConfigFile('dbstructure.config.php', true);
                $this->setConfigFile('defaults.config.php', true);
                $this->setConfigFile('settings.config.php', true);
                $this->setConfigFile('local.config.php');
index eac00f559a3070c447de761282a5e086f27df859..9925b02352de9f21b825281d027ece4b7d6e1230 100644 (file)
@@ -17,7 +17,7 @@ class APCuCacheDriverTest extends MemoryCacheTest
 
        protected function getInstance()
        {
-               $this->cache = new APCuCache();
+               $this->cache = new APCuCache('localhost');
                return $this->cache;
        }
 
index c92fb98dacc2c370dd24ed4554fcb6132fee60df..68b8d1dba12d8174495095095d731a6b9bbabefe 100644 (file)
@@ -8,7 +8,7 @@ class ArrayCacheDriverTest extends MemoryCacheTest
 {
        protected function getInstance()
        {
-               $this->cache = new ArrayCache();
+               $this->cache = new ArrayCache('localhost');
                return $this->cache;
        }
 
index ef97f5a172214ea16ed325ca79b7ff8cb5f2d027..088715cd7983c0c88bc6228074ae30b9016fe6f3 100644 (file)
@@ -4,15 +4,10 @@ namespace Friendica\Test\src\Core\Cache;
 
 use Friendica\Core\Cache\MemcachedCacheDriver;
 use Friendica\Test\MockedTest;
-use Friendica\Test\Util\AppMockTrait;
-use Friendica\Test\Util\VFSTrait;
 use Friendica\Util\PidFile;
 
 abstract class CacheTest extends MockedTest
 {
-       use VFSTrait;
-       use AppMockTrait;
-
        /**
         * @var int Start time of the mock (used for time operations)
         */
@@ -30,6 +25,7 @@ abstract class CacheTest extends MockedTest
 
        /**
         * Dataset for test setting different types in the cache
+        *
         * @return array
         */
        public function dataTypesInCache()
@@ -48,6 +44,7 @@ abstract class CacheTest extends MockedTest
 
        /**
         * Dataset for simple value sets/gets
+        *
         * @return array
         */
        public function dataSimple()
@@ -66,12 +63,6 @@ abstract class CacheTest extends MockedTest
 
        protected function setUp()
        {
-               $this->setUpVfsDir();
-               $this->mockApp($this->root);
-               $this->app
-                       ->shouldReceive('getHostname')
-                       ->andReturn('friendica.local');
-
                parent::setUp();
 
                $this->instance = $this->getInstance();
@@ -82,10 +73,12 @@ abstract class CacheTest extends MockedTest
        /**
         * @small
         * @dataProvider dataSimple
+        *
         * @param mixed $value1 a first
         * @param mixed $value2 a second
         */
-       function testSimple($value1, $value2) {
+       function testSimple($value1, $value2)
+       {
                $this->assertNull($this->instance->get('value1'));
 
                $this->instance->set('value1', $value1);
@@ -110,12 +103,14 @@ abstract class CacheTest extends MockedTest
        /**
         * @small
         * @dataProvider dataSimple
+        *
         * @param mixed $value1 a first
         * @param mixed $value2 a second
         * @param mixed $value3 a third
         * @param mixed $value4 a fourth
         */
-       function testClear($value1, $value2, $value3, $value4) {
+       function testClear($value1, $value2, $value3, $value4)
+       {
                $value = 'ipsum lorum';
                $this->instance->set('1_value1', $value1);
                $this->instance->set('1_value2', $value2);
@@ -166,7 +161,8 @@ abstract class CacheTest extends MockedTest
        /**
         * @medium
         */
-       function testTTL() {
+       function testTTL()
+       {
                $this->markTestSkipped('taking too much time without mocking');
 
                $this->assertNull($this->instance->get('value1'));
@@ -183,10 +179,13 @@ abstract class CacheTest extends MockedTest
 
        /**
         * @small
+        *
         * @param $data mixed the data to store in the cache
+        *
         * @dataProvider dataTypesInCache
         */
-       function testDifferentTypesInCache($data) {
+       function testDifferentTypesInCache($data)
+       {
                $this->instance->set('val', $data);
                $received = $this->instance->get('val');
                $this->assertEquals($data, $received, 'Value type changed from ' . gettype($data) . ' to ' . gettype($received));
@@ -194,12 +193,15 @@ abstract class CacheTest extends MockedTest
 
        /**
         * @small
+        *
         * @param mixed $value1 a first
         * @param mixed $value2 a second
         * @param mixed $value3 a third
+        *
         * @dataProvider dataSimple
         */
-       public function testGetAllKeys($value1, $value2, $value3) {
+       public function testGetAllKeys($value1, $value2, $value3)
+       {
                if ($this->cache instanceof MemcachedCacheDriver) {
                        $this->markTestSkipped('Memcached doesn\'t support getAllKeys anymore');
                }
index 2d29c2ad96cac40dc8db03ed90665dd202b20e12..1aa9bf02b84d176e120bb0565465b3e1cb2ff6d6 100644 (file)
@@ -3,33 +3,40 @@
 namespace Friendica\Test\src\Core\Cache;
 
 use Friendica\Core\Cache;
-use Friendica\Factory\CacheDriverFactory;
-use Friendica\Test\Util\DbaCacheMockTrait;
+use Friendica\Factory\ConfigFactory;
+use Friendica\Test\DatabaseTestTrait;
+use Friendica\Test\Util\Database\StaticDatabase;
+use Friendica\Test\Util\VFSTrait;
+use Friendica\Util\ConfigFileLoader;
+use Friendica\Util\Profiler;
+use Psr\Log\NullLogger;
 
-/**
- * @runTestsInSeparateProcesses
- * @preserveGlobalState disabled
- */
 class DatabaseCacheDriverTest extends CacheTest
 {
-       use DbaCacheMockTrait;
+       use DatabaseTestTrait;
+       use VFSTrait;
 
-       public function setUp()
+       protected function setUp()
        {
-               $this->mockUtcNow($this->startTime);
-
-               $this->mockConnected();
-               $this->mockConnect();
-
-               // The first "clear" at setup
-               $this->mockClear(false, true, 2);
+               $this->setUpVfsDir();
 
                parent::setUp();
        }
 
        protected function getInstance()
        {
-               $this->cache = CacheDriverFactory::create('database');
+               $logger = new NullLogger();
+               $profiler = \Mockery::mock(Profiler::class);
+               $profiler->shouldReceive('saveTimestamp')->withAnyArgs()->andReturn(true);
+
+               // load real config to avoid mocking every config-entry which is related to the Database class
+               $configFactory = new ConfigFactory();
+               $loader = new ConfigFileLoader($this->root->url());
+               $configCache = $configFactory->createCache($loader);
+
+               $dba = new StaticDatabase($configCache, $profiler, $logger);
+
+               $this->cache = new Cache\DatabaseCacheDriver('database', $dba);
                return $this->cache;
        }
 
@@ -38,104 +45,4 @@ class DatabaseCacheDriverTest extends CacheTest
                $this->cache->clear(false);
                parent::tearDown();
        }
-
-       /**
-        * {@inheritdoc}
-        * @dataProvider dataSimple
-        */
-       public function testSimple($value1, $value2)
-       {
-               // assertNull
-               $this->mockGet('value1', null, $this->startTime, 1);
-
-               // assertEquals
-               $this->mockSet('value1', $value1, Cache::FIVE_MINUTES, $this->startTime, true, 1);
-               $this->mockGet('value1', $value1, $this->startTime, 1);
-
-               // assertEquals
-               $this->mockSet('value1', $value2, Cache::FIVE_MINUTES, $this->startTime, true, 1);
-               $this->mockGet('value1', $value2, $this->startTime, 1);
-
-               // assertEquals
-               $this->mockSet('value2', $value1, Cache::FIVE_MINUTES, $this->startTime, true, 1);
-               $this->mockGet('value2', $value1, $this->startTime, 1);
-
-               // assertNull
-               $this->mockGet('not_set', null, $this->startTime, 1);
-
-               // assertNull
-               $this->mockDelete('value1', true, 1);
-               $this->mockGet('value1', null, $this->startTime, 1);
-
-               parent::testSimple($value1, $value2);
-       }
-
-       /**
-        * {@inheritdoc}
-        * @dataProvider dataSimple
-        */
-       public function testClear($value1, $value2, $value3, $value4)
-       {
-               // assert Equals
-               $this->mockSet('1_value1', $value1, Cache::FIVE_MINUTES, $this->startTime, true, 1);
-               $this->mockSet('1_value2', $value2, Cache::FIVE_MINUTES, $this->startTime, true, 1);
-               $this->mockSet('2_value1', $value3, Cache::FIVE_MINUTES, $this->startTime, true, 1);
-               $this->mockSet('3_value1', $value4, Cache::FIVE_MINUTES, $this->startTime, true, 1);
-
-               $this->mockGet('1_value1', $value1, $this->startTime, 2);
-               $this->mockGet('1_value2', $value2, $this->startTime, 2);
-               $this->mockGet('2_value1', $value3, $this->startTime, 2);
-               $this->mockGet('3_value1', $value4, $this->startTime, 2);
-
-               // assertTrue
-               $this->mockClear(true, true, 1);
-               $this->mockClear(false, true, 1);
-
-               // assertEquals
-               $this->mockGet('1_value1', null, $this->startTime, 1);
-               $this->mockGet('1_value2', null, $this->startTime, 1);
-               $this->mockGet('2_value3', null, $this->startTime, 1);
-               $this->mockGet('3_value4', null, $this->startTime, 1);
-
-               parent::testClear($value1, $value2, $value3, $value4);
-       }
-
-       /**
-        * {@inheritdoc}
-        * @dataProvider dataTypesInCache
-        */
-       public function testDifferentTypesInCache($data)
-       {
-               $this->mockSet('val', $data, Cache::FIVE_MINUTES, $this->startTime, true, 1);
-               $this->mockGet('val', $data, $this->startTime, 1);
-
-               parent::testDifferentTypesInCache($data);
-       }
-
-       /**
-        * {@inheritdoc}
-        * @dataProvider dataSimple
-        */
-       public function testGetAllKeys($value1, $value2, $value3)
-       {
-               $this->mockSet('value1', $value1, Cache::FIVE_MINUTES, $this->startTime, true, 1);
-               $this->mockSet('value2', $value2,Cache::FIVE_MINUTES, $this->startTime, true, 1);
-               $this->mockSet('test_value3', $value3, Cache::FIVE_MINUTES, $this->startTime, true, 1);
-
-               $result = [
-                       ['k' => 'value1'],
-                       ['k' => 'value2'],
-                       ['k' => 'test_value3'],
-               ];
-
-               $this->mockGetAllKeys(null, $result, $this->startTime, 1);
-
-               $result = [
-                       ['k' => 'test_value3'],
-               ];
-
-               $this->mockGetAllKeys('test', $result, $this->startTime, 1);
-
-               parent::testGetAllKeys($value1, $value2, $value3);
-       }
 }
index f8de88ac95a52e3d2b263a5e76e44ed72eb04d6e..31aa0b48522647f736e9f2e9998defc34a366630 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 
-
 namespace Friendica\Test\src\Core\Cache;
 
-use Friendica\Factory\CacheDriverFactory;
+use Friendica\Core\Cache\MemcacheCacheDriver;
+use Friendica\Core\Config\Configuration;
 
 /**
  * @requires extension memcache
@@ -12,19 +12,19 @@ class MemcacheCacheDriverTest extends MemoryCacheTest
 {
        protected function getInstance()
        {
-               $this->configMock
+               $configMock = \Mockery::mock(Configuration::class);
+
+               $configMock
                        ->shouldReceive('get')
                        ->with('system', 'memcache_host')
                        ->andReturn('localhost');
-
-               $this->configMock
+               $configMock
                        ->shouldReceive('get')
                        ->with('system', 'memcache_port')
                        ->andReturn(11211);
 
-               $this->cache = CacheDriverFactory::create('memcache');
+               $this->cache = new MemcacheCacheDriver('localhost', $configMock);
                return $this->cache;
-
        }
 
        public function tearDown()
index 9f0ed8d4fee2a0b1ce17f39eceb8b8a613706142..ade3886841627e57bbd2162a122ada5962de85ed 100644 (file)
@@ -3,7 +3,9 @@
 
 namespace Friendica\Test\src\Core\Cache;
 
-use Friendica\Factory\CacheDriverFactory;
+use Friendica\Core\Cache\MemcachedCacheDriver;
+use Friendica\Core\Config\Configuration;
+use Psr\Log\NullLogger;
 
 /**
  * @requires extension memcached
@@ -12,12 +14,16 @@ class MemcachedCacheDriverTest extends MemoryCacheTest
 {
        protected function getInstance()
        {
-               $this->configMock
+               $configMock = \Mockery::mock(Configuration::class);
+
+               $configMock
                        ->shouldReceive('get')
                        ->with('system', 'memcached_hosts')
                        ->andReturn([0 => 'localhost, 11211']);
 
-               $this->cache = CacheDriverFactory::create('memcached');
+               $logger = new NullLogger();
+
+               $this->cache = new MemcachedCacheDriver('localhost', $configMock, $logger);
                return $this->cache;
        }
 
index 6688153b05ad8082c03cde44da82d1972f88fc76..0bd7617dd09b5d75a6a4b2b42eb6abfa4872813f 100644 (file)
@@ -3,8 +3,6 @@
 namespace Friendica\Test\src\Core\Cache;
 
 use Friendica\Core\Cache\IMemoryCacheDriver;
-use Psr\Log\LoggerInterface;
-use Psr\Log\NullLogger;
 
 abstract class MemoryCacheTest extends CacheTest
 {
@@ -17,11 +15,6 @@ abstract class MemoryCacheTest extends CacheTest
        {
                parent::setUp();
 
-               $logger = new NullLogger();
-               $this->dice->shouldReceive('create')
-                          ->with(LoggerInterface::class)
-                          ->andReturn($logger);
-
                if (!($this->instance instanceof IMemoryCacheDriver)) {
                        throw new \Exception('MemoryCacheTest unsupported');
                }
@@ -31,7 +24,8 @@ abstract class MemoryCacheTest extends CacheTest
         * @small
         * @dataProvider dataSimple
         */
-       function testCompareSet($value1, $value2) {
+       function testCompareSet($value1, $value2)
+       {
                $this->assertNull($this->instance->get('value1'));
 
                $this->instance->add('value1', $value1);
@@ -47,7 +41,8 @@ abstract class MemoryCacheTest extends CacheTest
         * @small
         * @dataProvider dataSimple
         */
-       function testNegativeCompareSet($value1, $value2) {
+       function testNegativeCompareSet($value1, $value2)
+       {
                $this->assertNull($this->instance->get('value1'));
 
                $this->instance->add('value1', $value1);
@@ -64,7 +59,8 @@ abstract class MemoryCacheTest extends CacheTest
         * @small
         * @dataProvider dataSimple
         */
-       function testCompareDelete($data) {
+       function testCompareDelete($data)
+       {
                $this->assertNull($this->instance->get('value1'));
 
                $this->instance->add('value1', $data);
@@ -78,7 +74,8 @@ abstract class MemoryCacheTest extends CacheTest
         * @small
         * @dataProvider dataSimple
         */
-       function testNegativeCompareDelete($data) {
+       function testNegativeCompareDelete($data)
+       {
                $this->assertNull($this->instance->get('value1'));
 
                $this->instance->add('value1', $data);
@@ -95,7 +92,8 @@ abstract class MemoryCacheTest extends CacheTest
         * @small
         * @dataProvider dataSimple
         */
-       function testAdd($value1, $value2) {
+       function testAdd($value1, $value2)
+       {
                $this->assertNull($this->instance->get('value1'));
 
                $this->instance->add('value1', $value1);
@@ -111,4 +109,4 @@ abstract class MemoryCacheTest extends CacheTest
                $this->assertEquals($value2, $received, 'Value was not overwritten by add');
                $this->assertNotEquals($value1, $received, 'Value was not overwritten by any other value');
        }
-}
\ No newline at end of file
+}
index 6e11c3b8a42f72fbf43156df82863b13ab6881d6..999261834f5594ebfbd367341a126340090df6b0 100644 (file)
@@ -3,7 +3,8 @@
 
 namespace Friendica\Test\src\Core\Cache;
 
-use Friendica\Factory\CacheDriverFactory;
+use Friendica\Core\Cache\RedisCacheDriver;
+use Friendica\Core\Config\Configuration;
 
 /**
  * @requires extension redis
@@ -12,27 +13,27 @@ class RedisCacheDriverTest extends MemoryCacheTest
 {
        protected function getInstance()
        {
-               $this->configMock
+               $configMock = \Mockery::mock(Configuration::class);
+
+               $configMock
                        ->shouldReceive('get')
                        ->with('system', 'redis_host')
                        ->andReturn('localhost');
-
-               $this->configMock
+               $configMock
                        ->shouldReceive('get')
                        ->with('system', 'redis_port')
                        ->andReturn(null);
 
-               $this->configMock
+               $configMock
                        ->shouldReceive('get')
-                       ->with('system', 'redis_db')
+                       ->with('system', 'redis_db', 0)
                        ->andReturn(3);
-
-               $this->configMock
+               $configMock
                        ->shouldReceive('get')
                        ->with('system', 'redis_password')
                        ->andReturn(null);
 
-               $this->cache = CacheDriverFactory::create('redis');
+               $this->cache = new RedisCacheDriver('localhost', $configMock);
                return $this->cache;
        }
 
index 6e185d6b9ad68995024499b9f003045f0c1d3952..70796b2698ebf08e4d1d2ba31445a72876a335bc 100644 (file)
@@ -2,7 +2,6 @@
 
 namespace Friendica\Test\src\Core\Lock;
 
-
 use Friendica\Core\Cache\APCuCache;
 use Friendica\Core\Lock\CacheLockDriver;
 
@@ -19,6 +18,6 @@ class APCuCacheLockDriverTest extends LockTest
 
        protected function getInstance()
        {
-               return new CacheLockDriver(new APCuCache());
+               return new CacheLockDriver(new APCuCache('localhost'));
        }
 }
index 671341718b78168e5d15ffaadcd05d50747ed34d..9b74cd5670f6f6ac7bbd31426a5b4abbd0adc818 100644 (file)
@@ -2,7 +2,6 @@
 
 namespace Friendica\Test\src\Core\Lock;
 
-
 use Friendica\Core\Cache\ArrayCache;
 use Friendica\Core\Lock\CacheLockDriver;
 
@@ -10,7 +9,7 @@ class ArrayCacheLockDriverTest extends LockTest
 {
        protected function getInstance()
        {
-               return new CacheLockDriver(new ArrayCache());
+               return new CacheLockDriver(new ArrayCache('localhost'));
        }
 
        public function testLockTTL()
index 297e76d50b0fd25c70613fbe10aaefac1fef5a90..f1c8376af155056705d98c5a8b3fc74d364d02c6 100644 (file)
 
 namespace Friendica\Test\src\Core\Lock;
 
-use Friendica\Core\Cache;
 use Friendica\Core\Lock\DatabaseLockDriver;
-use Friendica\Test\Util\DbaLockMockTrait;
+use Friendica\Factory\ConfigFactory;
+use Friendica\Test\DatabaseTestTrait;
+use Friendica\Test\Util\Database\StaticDatabase;
+use Friendica\Test\Util\VFSTrait;
+use Friendica\Util\ConfigFileLoader;
+use Friendica\Util\Profiler;
+use Psr\Log\NullLogger;
 
-/**
- * @runTestsInSeparateProcesses
- * @preserveGlobalState disabled
- */
 class DatabaseLockDriverTest extends LockTest
 {
-       use DbaLockMockTrait;
+       use VFSTrait;
+       use DatabaseTestTrait;
 
        protected $pid = 123;
 
        protected function setUp()
        {
-               $this->mockConnected();
-               $this->mockConnect();
-
-               $this->mockReleaseAll($this->pid, 2);
+               $this->setUpVfsDir();
 
                parent::setUp();
        }
 
        protected function getInstance()
        {
-               return new DatabaseLockDriver($this->pid);
-       }
-
-       public function testLock()
-       {
-               $this->mockIsLocked('foo', false, $this->startTime, 1);
-               $this->mockAcquireLock('foo', Cache::FIVE_MINUTES, false, $this->pid, false, $this->startTime, 1);
-               $this->mockIsLocked('foo', true, $this->startTime, 1);
-               $this->mockIsLocked('bar', false, $this->startTime, 1);
-
-               parent::testLock();
-       }
-
-       public function testDoubleLock()
-       {
-               $this->mockIsLocked('foo', false, $this->startTime, 1);
-               $this->mockAcquireLock('foo', Cache::FIVE_MINUTES, false, $this->pid, false, $this->startTime, 1);
-               $this->mockIsLocked('foo', true, $this->startTime, 1);
-               $this->mockAcquireLock('foo', Cache::FIVE_MINUTES, true, $this->pid, true, $this->startTime, 1);
-
-               parent::testDoubleLock();
-       }
-
-       public function testReleaseLock()
-       {
-               $this->mockIsLocked('foo', false, $this->startTime, 1);
-               $this->mockAcquireLock('foo', Cache::FIVE_MINUTES, false, $this->pid, false, $this->startTime, 1);
-               $this->mockIsLocked('foo', true, $this->startTime, 1);
-               $this->mockReleaseLock('foo', $this->pid, 1);
-               $this->mockIsLocked('foo', false, $this->startTime, 1);
-
-               parent::testReleaseLock();
-       }
-
-       public function testReleaseAll()
-       {
-               $this->mockAcquireLock('foo', Cache::FIVE_MINUTES, false, $this->pid, false, $this->startTime, 1);
-               $this->mockAcquireLock('bar', Cache::FIVE_MINUTES, false, $this->pid, false, $this->startTime, 1);
-               $this->mockAcquireLock('nice', Cache::FIVE_MINUTES, false, $this->pid, false, $this->startTime, 1);
-
-               $this->mockIsLocked('foo', true, $this->startTime, 1);
-               $this->mockIsLocked('bar', true, $this->startTime, 1);
-               $this->mockIsLocked('nice', true, $this->startTime, 1);
-
-               $this->mockReleaseAll($this->pid, 1);
-
-               $this->mockIsLocked('foo', false, $this->startTime, 1);
-               $this->mockIsLocked('bar', false, $this->startTime, 1);
-               $this->mockIsLocked('nice', false, $this->startTime, 1);
-
-               parent::testReleaseAll();
-       }
-
-       public function testReleaseAfterUnlock()
-       {
-               $this->mockIsLocked('foo', false, $this->startTime, 1);
-               $this->mockIsLocked('bar', false, $this->startTime, 1);
-               $this->mockIsLocked('nice', false, $this->startTime, 1);
+               $logger   = new NullLogger();
+               $profiler = \Mockery::mock(Profiler::class);
+               $profiler->shouldReceive('saveTimestamp')->withAnyArgs()->andReturn(true);
 
-               $this->mockAcquireLock('foo', Cache::FIVE_MINUTES, false, $this->pid, false, $this->startTime, 1);
-               $this->mockAcquireLock('bar', Cache::FIVE_MINUTES, false, $this->pid, false, $this->startTime, 1);
-               $this->mockAcquireLock('nice', Cache::FIVE_MINUTES, false, $this->pid, false, $this->startTime, 1);
+               // load real config to avoid mocking every config-entry which is related to the Database class
+               $configFactory = new ConfigFactory();
+               $loader        = new ConfigFileLoader($this->root->url());
+               $configCache   = $configFactory->createCache($loader);
 
-               $this->mockReleaseLock('foo', $this->pid, 1);
-
-               $this->mockIsLocked('foo', false, $this->startTime, 1);
-               $this->mockIsLocked('bar', true, $this->startTime, 1);
-               $this->mockIsLocked('nice', true, $this->startTime, 1);
-
-               $this->mockReleaseAll($this->pid, 1);
-
-               $this->mockIsLocked('bar', false, $this->startTime, 1);
-               $this->mockIsLocked('nice', false, $this->startTime, 1);
-
-               parent::testReleaseAfterUnlock();
-       }
-
-       public function testReleaseWitTTL()
-       {
-               $this->mockIsLocked('test', false, $this->startTime, 1);
-               $this->mockAcquireLock('test', 10, false, $this->pid, false, $this->startTime, 1);
-               $this->mockIsLocked('test', true, $this->startTime, 1);
-               $this->mockReleaseLock('test', $this->pid, 1);
-               $this->mockIsLocked('test', false, $this->startTime, 1);
+               $dba = new StaticDatabase($configCache, $profiler, $logger);
 
-               parent::testReleaseWitTTL();
+               return new DatabaseLockDriver($dba, $this->pid);
        }
 }
index 59d6ee2d5d824f7b45cba698efde6db55a4b7654..5eb409539b0f679dc9c005db58e65d3b59dc0648 100644 (file)
@@ -3,16 +3,9 @@
 namespace Friendica\Test\src\Core\Lock;
 
 use Friendica\Test\MockedTest;
-use Friendica\Test\Util\AppMockTrait;
-use Friendica\Test\Util\VFSTrait;
-use Psr\Log\LoggerInterface;
-use Psr\Log\NullLogger;
 
 abstract class LockTest extends MockedTest
 {
-       use VFSTrait;
-       use AppMockTrait;
-
        /**
         * @var int Start time of the mock (used for time operations)
         */
@@ -27,19 +20,8 @@ abstract class LockTest extends MockedTest
 
        protected function setUp()
        {
-               // Reusable App object
-               $this->setUpVfsDir();
-               $this->mockApp($this->root);
-               $this->app
-                       ->shouldReceive('getHostname')
-                       ->andReturn('friendica.local');
-
-               $logger = new NullLogger();
-               $this->dice->shouldReceive('create')
-                          ->with(LoggerInterface::class)
-                          ->andReturn($logger);
-
                parent::setUp();
+
                $this->instance = $this->getInstance();
                $this->instance->releaseAll();
        }
@@ -53,7 +35,8 @@ abstract class LockTest extends MockedTest
        /**
         * @small
         */
-       public function testLock() {
+       public function testLock()
+       {
                $this->assertFalse($this->instance->isLocked('foo'));
                $this->assertTrue($this->instance->acquireLock('foo', 1));
                $this->assertTrue($this->instance->isLocked('foo'));
@@ -63,7 +46,8 @@ abstract class LockTest extends MockedTest
        /**
         * @small
         */
-       public function testDoubleLock() {
+       public function testDoubleLock()
+       {
                $this->assertFalse($this->instance->isLocked('foo'));
                $this->assertTrue($this->instance->acquireLock('foo', 1));
                $this->assertTrue($this->instance->isLocked('foo'));
@@ -74,7 +58,8 @@ abstract class LockTest extends MockedTest
        /**
         * @small
         */
-       public function testReleaseLock() {
+       public function testReleaseLock()
+       {
                $this->assertFalse($this->instance->isLocked('foo'));
                $this->assertTrue($this->instance->acquireLock('foo', 1));
                $this->assertTrue($this->instance->isLocked('foo'));
@@ -85,7 +70,8 @@ abstract class LockTest extends MockedTest
        /**
         * @small
         */
-       public function testReleaseAll() {
+       public function testReleaseAll()
+       {
                $this->assertTrue($this->instance->acquireLock('foo', 1));
                $this->assertTrue($this->instance->acquireLock('bar', 1));
                $this->assertTrue($this->instance->acquireLock('nice', 1));
@@ -104,7 +90,8 @@ abstract class LockTest extends MockedTest
        /**
         * @small
         */
-       public function testReleaseAfterUnlock() {
+       public function testReleaseAfterUnlock()
+       {
                $this->assertFalse($this->instance->isLocked('foo'));
                $this->assertFalse($this->instance->isLocked('bar'));
                $this->assertFalse($this->instance->isLocked('nice'));
@@ -139,7 +126,8 @@ abstract class LockTest extends MockedTest
        /**
         * @medium
         */
-       function testLockTTL() {
+       function testLockTTL()
+       {
                $this->markTestSkipped('taking too much time without mocking');
 
                $this->assertFalse($this->instance->isLocked('foo'));
index 8d32ad527d3415cf3635d87a5355168fc6f0d42c..8c723f34edf41db062bce28b0b28714ece662b86 100644 (file)
@@ -3,7 +3,8 @@
 
 namespace Friendica\Test\src\Core\Lock;
 
-use Friendica\Factory\CacheDriverFactory;
+use Friendica\Core\Cache\MemcacheCacheDriver;
+use Friendica\Core\Config\Configuration;
 use Friendica\Core\Lock\CacheLockDriver;
 
 /**
@@ -13,16 +14,17 @@ class MemcacheCacheLockDriverTest extends LockTest
 {
        protected function getInstance()
        {
-               $this->configMock
+               $configMock = \Mockery::mock(Configuration::class);
+
+               $configMock
                        ->shouldReceive('get')
                        ->with('system', 'memcache_host')
                        ->andReturn('localhost');
-
-               $this->configMock
+               $configMock
                        ->shouldReceive('get')
                        ->with('system', 'memcache_port')
                        ->andReturn(11211);
 
-               return new CacheLockDriver(CacheDriverFactory::create('memcache'));
+               return new CacheLockDriver(new MemcacheCacheDriver('localhost', $configMock));
        }
 }
index f08ffa38172447ef6e2f6be0696b8337d616d568..7685d816de12898a0b9289a50e8f9432b6792332 100644 (file)
@@ -3,8 +3,10 @@
 
 namespace Friendica\Test\src\Core\Lock;
 
-use Friendica\Factory\CacheDriverFactory;
+use Friendica\Core\Cache\MemcachedCacheDriver;
+use Friendica\Core\Config\Configuration;
 use Friendica\Core\Lock\CacheLockDriver;
+use Psr\Log\NullLogger;
 
 /**
  * @requires extension memcached
@@ -13,11 +15,15 @@ class MemcachedCacheLockDriverTest extends LockTest
 {
        protected function getInstance()
        {
-               $this->configMock
+               $configMock = \Mockery::mock(Configuration::class);
+
+               $configMock
                        ->shouldReceive('get')
                        ->with('system', 'memcached_hosts')
                        ->andReturn([0 => 'localhost, 11211']);
 
-               return new CacheLockDriver(CacheDriverFactory::create('memcached'));
+               $logger = new NullLogger();
+
+               return new CacheLockDriver(new MemcachedCacheDriver('localhost', $configMock, $logger));
        }
 }
index 21bace501822e631010beb0c83f6878a958be6ec..fdc961ccc8fd47f1f765724d1958bd6a2a1d60bb 100644 (file)
@@ -3,8 +3,9 @@
 
 namespace Friendica\Test\src\Core\Lock;
 
+use Friendica\Core\Cache\RedisCacheDriver;
+use Friendica\Core\Config\Configuration;
 use Friendica\Core\Lock\CacheLockDriver;
-use Friendica\Factory\CacheDriverFactory;
 
 /**
  * @requires extension redis
@@ -13,26 +14,26 @@ class RedisCacheLockDriverTest extends LockTest
 {
        protected function getInstance()
        {
-               $this->configMock
+               $configMock = \Mockery::mock(Configuration::class);
+
+               $configMock
                        ->shouldReceive('get')
                        ->with('system', 'redis_host')
                        ->andReturn('localhost');
-
-               $this->configMock
+               $configMock
                        ->shouldReceive('get')
                        ->with('system', 'redis_port')
                        ->andReturn(null);
 
-               $this->configMock
+               $configMock
                        ->shouldReceive('get')
-                       ->with('system', 'redis_db')
+                       ->with('system', 'redis_db', 0)
                        ->andReturn(3);
-
-               $this->configMock
+               $configMock
                        ->shouldReceive('get')
                        ->with('system', 'redis_password')
                        ->andReturn(null);
 
-               return new CacheLockDriver(CacheDriverFactory::create('redis'));
+               return new CacheLockDriver(new RedisCacheDriver('localhost', $configMock));
        }
 }
index a37fbffbed24c86a7076fd060216e50b751a84cd..d5aaa36ba94565d410998a9a05744a25daa1362d 100644 (file)
@@ -2,6 +2,10 @@
 
 namespace Friendica\Test\src\Core\Lock;
 
+use Dice\Dice;
+use Friendica\App;
+use Friendica\BaseObject;
+use Friendica\Core\Config\Configuration;
 use Friendica\Core\Lock\SemaphoreLockDriver;
 
 class SemaphoreLockDriverTest extends LockTest
@@ -10,12 +14,21 @@ class SemaphoreLockDriverTest extends LockTest
        {
                parent::setUp();
 
-               $this->app->shouldReceive('getHostname')->andReturn('friendica.local');
+               $dice = \Mockery::mock(Dice::class)->makePartial();
 
-               $this->configMock
+               $app = \Mockery::mock(App::class);
+               $app->shouldReceive('getHostname')->andReturn('friendica.local');
+               $dice->shouldReceive('create')->with(App::class)->andReturn($app);
+
+               $configMock = \Mockery::mock(Configuration::class);
+               $configMock
                        ->shouldReceive('get')
-                       ->with('system', 'temppath')
+                       ->with('system', 'temppath', NULL, false)
                        ->andReturn('/tmp/');
+               $dice->shouldReceive('create')->with(Configuration::class)->andReturn($configMock);
+
+               // @todo Because "get_temppath()" is using static methods, we have to initialize the BaseObject
+               BaseObject::setDependencyInjection($dice);
        }
 
        protected function getInstance()