mirror of
https://git.sekbaer.de/Friendica/friendica.git
synced 2025-06-07 07:44:27 +02:00
Merge branch 'develop' into eventdispatcher-part3
This commit is contained in:
commit
dfa1f52ad6
50 changed files with 1362 additions and 611 deletions
26
tests/CacheLockTestCase.php
Normal file
26
tests/CacheLockTestCase.php
Normal file
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
// Copyright (C) 2010-2024, the Friendica project
|
||||
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
|
||||
//
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
namespace Friendica\Test;
|
||||
|
||||
use Friendica\Core\Cache\Capability\ICanCacheInMemory;
|
||||
use Friendica\Core\Lock\Capability\ICanLock;
|
||||
|
||||
abstract class CacheLockTestCase extends LockTestCase
|
||||
{
|
||||
abstract protected function getCache(): ICanCacheInMemory;
|
||||
|
||||
abstract protected function getInstance(): ICanLock;
|
||||
|
||||
/**
|
||||
* Test if the getStats() result is identically to the getCacheStats()
|
||||
*/
|
||||
public function testGetStats()
|
||||
{
|
||||
self::assertSame(array_keys($this->getCache()->getStats()), array_keys($this->instance->getCacheStats()));
|
||||
}
|
||||
}
|
|
@ -8,21 +8,17 @@
|
|||
namespace Friendica\Test;
|
||||
|
||||
use Friendica\Core\Lock\Capability\ICanLock;
|
||||
use Friendica\Test\MockedTestCase;
|
||||
|
||||
abstract class LockTestCase extends MockedTestCase
|
||||
{
|
||||
/**
|
||||
* @var int Start time of the mock (used for time operations)
|
||||
* Start time of the mock (used for time operations)
|
||||
*/
|
||||
protected $startTime = 1417011228;
|
||||
protected int $startTime = 1417011228;
|
||||
protected ICanLock $instance;
|
||||
|
||||
/**
|
||||
* @var ICanLock
|
||||
*/
|
||||
protected $instance;
|
||||
abstract protected function getInstance(): ICanLock;
|
||||
|
||||
abstract protected function getInstance();
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
|
@ -205,4 +201,6 @@ abstract class LockTestCase extends MockedTestCase
|
|||
self::assertFalse($this->instance->isLocked('wrongLock'));
|
||||
self::assertFalse($this->instance->release('wrongLock'));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -17,7 +17,6 @@ return [
|
|||
'temppath' => '/tmp/friendica.local',
|
||||
'theme' => 'frio',
|
||||
'url' => 'https://friendica.local',
|
||||
'urlpath' => '',
|
||||
'build' => 1508,
|
||||
'maintenance' => false,
|
||||
'dbupdate' => 1,
|
||||
|
|
|
@ -129,7 +129,6 @@ class AutomaticInstallationConsoleTest extends ConsoleTestCase
|
|||
],
|
||||
'system' => [
|
||||
'basepath' => '',
|
||||
'urlpath' => '',
|
||||
'url' => 'http://friendica.local',
|
||||
'ssl_policy' => 0,
|
||||
'default_timezone' => '',
|
||||
|
@ -152,7 +151,6 @@ class AutomaticInstallationConsoleTest extends ConsoleTestCase
|
|||
'admin_email' => 'admin@philipp.info',
|
||||
],
|
||||
'system' => [
|
||||
'urlpath' => 'test/it',
|
||||
'url' => 'http://friendica.local/test/it',
|
||||
'basepath' => '',
|
||||
'ssl_policy' => '2',
|
||||
|
@ -176,7 +174,6 @@ class AutomaticInstallationConsoleTest extends ConsoleTestCase
|
|||
'admin_email' => 'admin@philipp.info',
|
||||
],
|
||||
'system' => [
|
||||
'urlpath' => 'test/it',
|
||||
'url' => 'https://friendica.local/test/it',
|
||||
'basepath' => '',
|
||||
'ssl_policy' => '1',
|
||||
|
@ -352,7 +349,6 @@ FIN;
|
|||
self::assertConfigEntry('system', 'default_timezone', $assertion, ($default) ? Installer::DEFAULT_TZ : null);
|
||||
self::assertConfigEntry('system', 'language', $assertion, ($default) ? Installer::DEFAULT_LANG : null);
|
||||
self::assertConfigEntry('system', 'url', $assertion);
|
||||
self::assertConfigEntry('system', 'urlpath', $assertion);
|
||||
self::assertConfigEntry('system', 'ssl_policy', $assertion, ($default) ? App\BaseURL::DEFAULT_SSL_SCHEME : null);
|
||||
self::assertConfigEntry('system', 'basepath', ($realBasepath) ? $this->root->url() : $assertion);
|
||||
}
|
||||
|
@ -446,7 +442,6 @@ return [
|
|||
],
|
||||
'system' => [
|
||||
'basepath' => '{$conf('system', 'basepath')}',
|
||||
'urlpath' => '{$conf('system', 'urlpath')}',
|
||||
'url' => '{$conf('system', 'url')}',
|
||||
'ssl_policy' => '{$conf('system', 'ssl_policy')}',
|
||||
'default_timezone' => '{$conf('system', 'default_timezone')}',
|
||||
|
@ -604,7 +599,7 @@ CONF;
|
|||
self::assertStuckDB($txt);
|
||||
self::assertTrue($this->root->hasChild('config' . DIRECTORY_SEPARATOR . 'local.config.php'));
|
||||
|
||||
self::assertConfig(['config' => ['hostname' => 'friendica.local'], 'system' => ['url' => 'http://friendica.local', 'ssl_policy' => 0, 'urlpath' => '']], false, true, false, true);
|
||||
self::assertConfig(['config' => ['hostname' => 'friendica.local'], 'system' => ['url' => 'http://friendica.local', 'ssl_policy' => 0]], false, true, false, true);
|
||||
}
|
||||
|
||||
public function testGetHelp()
|
||||
|
|
|
@ -35,4 +35,18 @@ class APCuCacheTest extends MemoryCacheTestCase
|
|||
$this->cache->clear(false);
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* @small
|
||||
*/
|
||||
public function testStats()
|
||||
{
|
||||
$stats = $this->instance->getStats();
|
||||
|
||||
self::assertNotNull($stats['entries']);
|
||||
self::assertNotNull($stats['used_memory']);
|
||||
self::assertNotNull($stats['hits']);
|
||||
self::assertNotNull($stats['misses']);
|
||||
self::assertNotNull($stats['avail_mem']);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,4 +33,12 @@ class ArrayCacheTest extends MemoryCacheTestCase
|
|||
self::markTestSkipped("Array Cache doesn't support TTL");
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @small
|
||||
*/
|
||||
public function testGetStats()
|
||||
{
|
||||
self::assertEmpty($this->cache->getStats());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -59,4 +59,21 @@ class MemcacheCacheTest extends MemoryCacheTestCase
|
|||
{
|
||||
static::markTestIncomplete('Race condition because of too fast getAllKeys() which uses a workaround');
|
||||
}
|
||||
|
||||
/**
|
||||
* @small
|
||||
*/
|
||||
public function testStats()
|
||||
{
|
||||
$stats = $this->instance->getStats();
|
||||
|
||||
self::assertNotNull($stats['version']);
|
||||
self::assertIsNumeric($stats['hits']);
|
||||
self::assertIsNumeric($stats['misses']);
|
||||
self::assertIsNumeric($stats['evictions']);
|
||||
self::assertIsNumeric($stats['entries']);
|
||||
self::assertIsNumeric($stats['used_memory']);
|
||||
self::assertGreaterThan(0, $stats['connected_clients']);
|
||||
self::assertGreaterThan(0, $stats['uptime']);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -58,4 +58,21 @@ class MemcachedCacheTest extends MemoryCacheTestCase
|
|||
{
|
||||
static::markTestIncomplete('Race condition because of too fast getAllKeys() which uses a workaround');
|
||||
}
|
||||
|
||||
/**
|
||||
* @small
|
||||
*/
|
||||
public function testStats()
|
||||
{
|
||||
$stats = $this->instance->getStats();
|
||||
|
||||
self::assertNotNull($stats['version']);
|
||||
self::assertIsNumeric($stats['hits']);
|
||||
self::assertIsNumeric($stats['misses']);
|
||||
self::assertIsNumeric($stats['evictions']);
|
||||
self::assertIsNumeric($stats['entries']);
|
||||
self::assertIsNumeric($stats['used_memory']);
|
||||
self::assertGreaterThan(0, $stats['connected_clients']);
|
||||
self::assertGreaterThan(0, $stats['uptime']);
|
||||
}
|
||||
}
|
||||
|
|
56
tests/src/Core/Cache/ProfilerCacheDecoratorTest.php
Normal file
56
tests/src/Core/Cache/ProfilerCacheDecoratorTest.php
Normal file
|
@ -0,0 +1,56 @@
|
|||
<?php
|
||||
|
||||
// Copyright (C) 2010-2024, the Friendica project
|
||||
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
|
||||
//
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
namespace Core\Cache;
|
||||
|
||||
use Friendica\Core\Cache\Type\ArrayCache;
|
||||
use Friendica\Core\Cache\Type\ProfilerCacheDecorator;
|
||||
use Friendica\Core\Config\Capability\IManageConfigValues;
|
||||
use Friendica\Test\MemoryCacheTestCase;
|
||||
use Friendica\Util\Profiler;
|
||||
|
||||
class ProfilerCacheDecoratorTest extends MemoryCacheTestCase
|
||||
{
|
||||
protected function getInstance()
|
||||
{
|
||||
$config = \Mockery::mock(IManageConfigValues::class);
|
||||
$config->shouldReceive('get')->with('system', 'profiler')->once()->andReturn(false);
|
||||
$config->shouldReceive('get')->with('rendertime', 'callstack')->once()->andReturn(false);
|
||||
|
||||
$this->cache = new ProfilerCacheDecorator(new ArrayCache('localhost'), new Profiler($config));
|
||||
return $this->cache;
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
$this->cache->clear(false);
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* @doesNotPerformAssertions
|
||||
*/
|
||||
public function testTTL()
|
||||
{
|
||||
// Array Cache doesn't support TTL
|
||||
self::markTestSkipped("Array Cache doesn't support TTL");
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @small
|
||||
*/
|
||||
public function testGetStats()
|
||||
{
|
||||
self::assertEmpty($this->cache->getStats());
|
||||
}
|
||||
|
||||
public function testGetName()
|
||||
{
|
||||
self::assertStringEndsWith(' (with profiler)', $this->instance->getName());
|
||||
}
|
||||
}
|
|
@ -57,4 +57,21 @@ class RedisCacheTest extends MemoryCacheTestCase
|
|||
$this->cache->clear(false);
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* @small
|
||||
*/
|
||||
public function testStats()
|
||||
{
|
||||
$stats = $this->instance->getStats();
|
||||
|
||||
self::assertNotNull($stats['version']);
|
||||
self::assertIsNumeric($stats['hits']);
|
||||
self::assertIsNumeric($stats['misses']);
|
||||
self::assertIsNumeric($stats['evictions']);
|
||||
self::assertIsNumeric($stats['entries']);
|
||||
self::assertIsNumeric($stats['used_memory']);
|
||||
self::assertGreaterThan(0, $stats['connected_clients']);
|
||||
self::assertGreaterThan(0, $stats['uptime']);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,26 +7,39 @@
|
|||
|
||||
namespace Friendica\Test\src\Core\Lock;
|
||||
|
||||
use Friendica\Core\Cache\Capability\ICanCacheInMemory;
|
||||
use Friendica\Core\Cache\Type\APCuCache;
|
||||
use Friendica\Core\Lock\Capability\ICanLock;
|
||||
use Friendica\Core\Lock\Type\CacheLock;
|
||||
use Friendica\Test\LockTestCase;
|
||||
use Friendica\Test\CacheLockTestCase;
|
||||
|
||||
/**
|
||||
* @group APCU
|
||||
*/
|
||||
class APCuCacheLockTest extends LockTestCase
|
||||
class APCuCacheLockTest extends CacheLockTestCase
|
||||
{
|
||||
private APCuCache $cache;
|
||||
private ICanLock $lock;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
if (!APCuCache::isAvailable()) {
|
||||
static::markTestSkipped('APCu is not available');
|
||||
}
|
||||
|
||||
$this->cache = new APCuCache('localhost');
|
||||
$this->lock = new CacheLock($this->cache);
|
||||
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
protected function getInstance()
|
||||
protected function getInstance(): CacheLock
|
||||
{
|
||||
return new CacheLock(new APCuCache('localhost'));
|
||||
return $this->lock;
|
||||
}
|
||||
|
||||
protected function getCache(): ICanCacheInMemory
|
||||
{
|
||||
return $this->cache;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,15 +7,32 @@
|
|||
|
||||
namespace Friendica\Test\src\Core\Lock;
|
||||
|
||||
use Friendica\Core\Cache\Capability\ICanCacheInMemory;
|
||||
use Friendica\Core\Cache\Type\ArrayCache;
|
||||
use Friendica\Core\Lock\Type\CacheLock;
|
||||
use Friendica\Test\LockTestCase;
|
||||
use Friendica\Test\CacheLockTestCase;
|
||||
|
||||
class ArrayCacheLockTest extends LockTestCase
|
||||
class ArrayCacheLockTest extends CacheLockTestCase
|
||||
{
|
||||
protected function getInstance()
|
||||
private CacheLock $lock;
|
||||
private ArrayCache $cache;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
return new CacheLock(new ArrayCache('localhost'));
|
||||
$this->cache = new ArrayCache('localhost');
|
||||
$this->lock = new CacheLock($this->cache);
|
||||
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
protected function getInstance(): CacheLock
|
||||
{
|
||||
return $this->lock;
|
||||
}
|
||||
|
||||
protected function getCache(): ICanCacheInMemory
|
||||
{
|
||||
return $this->cache;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
|
||||
namespace Friendica\Test\src\Core\Lock;
|
||||
|
||||
use Friendica\Core\Lock\Capability\ICanLock;
|
||||
use Friendica\Core\Lock\Type\DatabaseLock;
|
||||
use Friendica\Test\LockTestCase;
|
||||
use Friendica\Test\Util\CreateDatabaseTrait;
|
||||
|
@ -26,7 +27,7 @@ class DatabaseLockDriverTest extends LockTestCase
|
|||
parent::setUp();
|
||||
}
|
||||
|
||||
protected function getInstance()
|
||||
protected function getInstance(): ICanLock
|
||||
{
|
||||
return new DatabaseLock($this->getDbInstance(), $this->pid);
|
||||
}
|
||||
|
|
|
@ -8,19 +8,23 @@
|
|||
namespace Friendica\Test\src\Core\Lock;
|
||||
|
||||
use Exception;
|
||||
use Friendica\Core\Cache\Capability\ICanCacheInMemory;
|
||||
use Friendica\Core\Cache\Type\MemcacheCache;
|
||||
use Friendica\Core\Config\Capability\IManageConfigValues;
|
||||
use Friendica\Core\Lock\Type\CacheLock;
|
||||
use Friendica\Test\LockTestCase;
|
||||
use Friendica\Test\CacheLockTestCase;
|
||||
use Mockery;
|
||||
|
||||
/**
|
||||
* @requires extension Memcache
|
||||
* @group MEMCACHE
|
||||
*/
|
||||
class MemcacheCacheLockTest extends LockTestCase
|
||||
class MemcacheCacheLockTest extends CacheLockTestCase
|
||||
{
|
||||
protected function getInstance()
|
||||
private CacheLock $lock;
|
||||
private MemcacheCache $cache;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$configMock = Mockery::mock(IManageConfigValues::class);
|
||||
|
||||
|
@ -36,16 +40,24 @@ class MemcacheCacheLockTest extends LockTestCase
|
|||
->with('system', 'memcache_port')
|
||||
->andReturn($port);
|
||||
|
||||
$lock = null;
|
||||
|
||||
try {
|
||||
$cache = new MemcacheCache($host, $configMock);
|
||||
$lock = new CacheLock($cache);
|
||||
$this->cache = new MemcacheCache($host, $configMock);
|
||||
$this->lock = new CacheLock($this->cache);
|
||||
} catch (Exception $e) {
|
||||
static::markTestSkipped('Memcache is not available');
|
||||
}
|
||||
|
||||
return $lock;
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
protected function getInstance(): CacheLock
|
||||
{
|
||||
return $this->lock;
|
||||
}
|
||||
|
||||
protected function getCache(): ICanCacheInMemory
|
||||
{
|
||||
return $this->cache;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -8,10 +8,11 @@
|
|||
namespace Friendica\Test\src\Core\Lock;
|
||||
|
||||
use Exception;
|
||||
use Friendica\Core\Cache\Capability\ICanCacheInMemory;
|
||||
use Friendica\Core\Cache\Type\MemcachedCache;
|
||||
use Friendica\Core\Config\Capability\IManageConfigValues;
|
||||
use Friendica\Core\Lock\Type\CacheLock;
|
||||
use Friendica\Test\LockTestCase;
|
||||
use Friendica\Test\CacheLockTestCase;
|
||||
use Mockery;
|
||||
use Psr\Log\NullLogger;
|
||||
|
||||
|
@ -19,9 +20,12 @@ use Psr\Log\NullLogger;
|
|||
* @requires extension memcached
|
||||
* @group MEMCACHED
|
||||
*/
|
||||
class MemcachedCacheLockTest extends LockTestCase
|
||||
class MemcachedCacheLockTest extends CacheLockTestCase
|
||||
{
|
||||
protected function getInstance()
|
||||
private MemcachedCache $cache;
|
||||
private CacheLock $lock;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$configMock = Mockery::mock(IManageConfigValues::class);
|
||||
|
||||
|
@ -35,16 +39,24 @@ class MemcachedCacheLockTest extends LockTestCase
|
|||
|
||||
$logger = new NullLogger();
|
||||
|
||||
$lock = null;
|
||||
|
||||
try {
|
||||
$cache = new MemcachedCache($host, $configMock, $logger);
|
||||
$lock = new CacheLock($cache);
|
||||
$this->cache = new MemcachedCache($host, $configMock, $logger);
|
||||
$this->lock = new CacheLock($this->cache);
|
||||
} catch (Exception $e) {
|
||||
static::markTestSkipped('Memcached is not available');
|
||||
}
|
||||
|
||||
return $lock;
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
protected function getInstance(): CacheLock
|
||||
{
|
||||
return $this->lock;
|
||||
}
|
||||
|
||||
protected function getCache(): ICanCacheInMemory
|
||||
{
|
||||
return $this->cache;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -8,19 +8,20 @@
|
|||
namespace Friendica\Test\src\Core\Lock;
|
||||
|
||||
use Exception;
|
||||
use Friendica\Core\Cache\Capability\ICanCacheInMemory;
|
||||
use Friendica\Core\Cache\Type\RedisCache;
|
||||
use Friendica\Core\Config\Capability\IManageConfigValues;
|
||||
use Friendica\Core\Lock\Type\CacheLock;
|
||||
use Friendica\Test\LockTestCase;
|
||||
use Friendica\Test\CacheLockTestCase;
|
||||
use Mockery;
|
||||
|
||||
/**
|
||||
* @requires extension redis
|
||||
* @group REDIS
|
||||
*/
|
||||
class RedisCacheLockTest extends LockTestCase
|
||||
class RedisCacheLockTest extends CacheLockTestCase
|
||||
{
|
||||
protected function getInstance()
|
||||
protected function setUp(): void
|
||||
{
|
||||
$configMock = Mockery::mock(IManageConfigValues::class);
|
||||
|
||||
|
@ -45,15 +46,23 @@ class RedisCacheLockTest extends LockTestCase
|
|||
->with('system', 'redis_password')
|
||||
->andReturn(null);
|
||||
|
||||
$lock = null;
|
||||
|
||||
try {
|
||||
$cache = new RedisCache($host, $configMock);
|
||||
$lock = new CacheLock($cache);
|
||||
$this->cache = new RedisCache($host, $configMock);
|
||||
$this->lock = new CacheLock($this->cache);
|
||||
} catch (Exception $e) {
|
||||
static::markTestSkipped('Redis is not available. Error: ' . $e->getMessage());
|
||||
}
|
||||
|
||||
return $lock;
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
protected function getInstance(): CAcheLock
|
||||
{
|
||||
return $this->lock;
|
||||
}
|
||||
|
||||
protected function getCache(): ICanCacheInMemory
|
||||
{
|
||||
return $this->cache;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@ use Friendica\App;
|
|||
use Friendica\Core\Config\Capability\IManageConfigValues;
|
||||
use Friendica\Core\Config\Model\ReadOnlyFileConfig;
|
||||
use Friendica\Core\Config\ValueObject\Cache;
|
||||
use Friendica\Core\Lock\Capability\ICanLock;
|
||||
use Friendica\Core\Lock\Type\SemaphoreLock;
|
||||
use Friendica\Core\System;
|
||||
use Friendica\DI;
|
||||
|
@ -31,7 +32,7 @@ class SemaphoreLockTest extends LockTestCase
|
|||
$dice->shouldReceive('create')->with(App::class)->andReturn($app);
|
||||
|
||||
$configCache = new Cache(['system' => ['temppath' => '/tmp']]);
|
||||
$configMock = new ReadOnlyFileConfig($configCache);
|
||||
$configMock = new ReadOnlyFileConfig($configCache);
|
||||
$dice->shouldReceive('create')->with(IManageConfigValues::class)->andReturn($configMock);
|
||||
|
||||
// @todo Because "get_temppath()" is using static methods, we have to initialize the BaseObject
|
||||
|
@ -40,7 +41,7 @@ class SemaphoreLockTest extends LockTestCase
|
|||
parent::setUp();
|
||||
}
|
||||
|
||||
protected function getInstance()
|
||||
protected function getInstance(): ICanLock
|
||||
{
|
||||
return new SemaphoreLock();
|
||||
}
|
||||
|
|
203
tests/src/Module/StatsCachingTest.php
Normal file
203
tests/src/Module/StatsCachingTest.php
Normal file
|
@ -0,0 +1,203 @@
|
|||
<?php
|
||||
|
||||
// Copyright (C) 2010-2024, the Friendica project
|
||||
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
|
||||
//
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
namespace Friendica\Test\src\Module;
|
||||
|
||||
use Friendica\Capabilities\ICanCreateResponses;
|
||||
use Friendica\Core\Cache\Capability\ICanCache;
|
||||
use Friendica\Core\Cache\Type\ArrayCache;
|
||||
use Friendica\Core\Cache\Type\DatabaseCache;
|
||||
use Friendica\Core\Config\Capability\IManageConfigValues;
|
||||
use Friendica\Core\Lock\Capability\ICanLock;
|
||||
use Friendica\Core\Lock\Type\CacheLock;
|
||||
use Friendica\Core\Lock\Type\DatabaseLock;
|
||||
use Friendica\DI;
|
||||
use Friendica\Module\Special\HTTPException;
|
||||
use Friendica\Module\StatsCaching;
|
||||
use Friendica\Test\FixtureTestCase;
|
||||
use Mockery\MockInterface;
|
||||
use phpmock\mockery\PHPMockery;
|
||||
|
||||
class StatsCachingTest extends FixtureTestCase
|
||||
{
|
||||
/** @var MockInterface|HTTPException */
|
||||
protected $httpExceptionMock;
|
||||
|
||||
protected ICanCache $cache;
|
||||
protected ICanLock $lock;
|
||||
|
||||
/** @var MockInterface|IManageConfigValues */
|
||||
protected $config;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->httpExceptionMock = \Mockery::mock(HTTPException::class);
|
||||
$this->config = \Mockery::mock(IManageConfigValues::class);
|
||||
$this->cache = new ArrayCache('localhost');
|
||||
$this->lock = new CacheLock($this->cache);
|
||||
}
|
||||
|
||||
public function testStatsCachingNotAllowed()
|
||||
{
|
||||
$this->httpExceptionMock->shouldReceive('content')->andReturn('failed')->once();
|
||||
|
||||
$response = (new StatsCaching(DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], $this->config, $this->cache, $this->lock, []))
|
||||
->run($this->httpExceptionMock);
|
||||
|
||||
self::assertEquals('404', $response->getStatusCode());
|
||||
self::assertEquals('Page not found', $response->getReasonPhrase());
|
||||
self::assertEquals('failed', $response->getBody());
|
||||
}
|
||||
|
||||
public function testStatsCachingWitMinimumCache()
|
||||
{
|
||||
$request = [
|
||||
'key' => '12345',
|
||||
];
|
||||
$this->config->shouldReceive('get')->with('system', 'stats_key')->twice()->andReturn('12345');
|
||||
PHPMockery::mock("Friendica\\Module", "function_exists")->with('opcache_get_status')->once()->andReturn(false);
|
||||
|
||||
$response = (new StatsCaching(DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], $this->config, $this->cache, $this->lock, []))
|
||||
->run($this->httpExceptionMock, $request);
|
||||
|
||||
self::assertJson($response->getBody());
|
||||
self::assertEquals(['Content-type' => ['application/json; charset=utf-8'], ICanCreateResponses::X_HEADER => ['json']], $response->getHeaders());
|
||||
|
||||
$json = json_decode($response->getBody(), true);
|
||||
|
||||
self::assertEquals([
|
||||
'type' => 'array',
|
||||
'stats' => [],
|
||||
], $json['cache']);
|
||||
self::assertEquals([
|
||||
'type' => 'array',
|
||||
'stats' => [],
|
||||
], $json['lock']);
|
||||
}
|
||||
|
||||
public function testStatsCachingWithDatabase()
|
||||
{
|
||||
$request = [
|
||||
'key' => '12345',
|
||||
];
|
||||
$this->config->shouldReceive('get')->with('system', 'stats_key')->twice()->andReturn('12345');
|
||||
|
||||
$this->cache = new DatabaseCache('localhost', DI::dba());
|
||||
$this->lock = new DatabaseLock(DI::dba());
|
||||
PHPMockery::mock("Friendica\\Module", "function_exists")->with('opcache_get_status')->once()->andReturn(false);
|
||||
|
||||
$response = (new StatsCaching(DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], $this->config, $this->cache, $this->lock, []))
|
||||
->run($this->httpExceptionMock, $request);
|
||||
|
||||
self::assertJson($response->getBody());
|
||||
self::assertEquals(['Content-type' => ['application/json; charset=utf-8'], ICanCreateResponses::X_HEADER => ['json']], $response->getHeaders());
|
||||
|
||||
$json = json_decode($response->getBody(), true);
|
||||
|
||||
self::assertEquals(['enabled' => false], $json['opcache']);
|
||||
self::assertEquals(['type' => 'database'], $json['cache']);
|
||||
self::assertEquals(['type' => 'database'], $json['lock']);
|
||||
}
|
||||
|
||||
public function testStatsCachingWithCache()
|
||||
{
|
||||
$request = [
|
||||
'key' => '12345',
|
||||
];
|
||||
$this->config->shouldReceive('get')->with('system', 'stats_key')->twice()->andReturn('12345');
|
||||
|
||||
$this->cache = new DatabaseCache('localhost', DI::dba());
|
||||
$this->lock = new DatabaseLock(DI::dba());
|
||||
PHPMockery::mock("Friendica\\Module", "function_exists")->with('opcache_get_status')->once()->andReturn(false);
|
||||
|
||||
$response = (new StatsCaching(DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], $this->config, $this->cache, $this->lock, []))
|
||||
->run($this->httpExceptionMock, $request);
|
||||
|
||||
self::assertJson($response->getBody());
|
||||
self::assertEquals(['Content-type' => ['application/json; charset=utf-8'], ICanCreateResponses::X_HEADER => ['json']], $response->getHeaders());
|
||||
|
||||
$json = json_decode($response->getBody(), true);
|
||||
|
||||
self::assertEquals(['enabled' => false], $json['opcache']);
|
||||
self::assertEquals(['type' => 'database'], $json['cache']);
|
||||
self::assertEquals(['type' => 'database'], $json['lock']);
|
||||
}
|
||||
|
||||
public function testStatsCachingWithOpcacheAndNull()
|
||||
{
|
||||
$request = [
|
||||
'key' => '12345',
|
||||
];
|
||||
$this->config->shouldReceive('get')->with('system', 'stats_key')->twice()->andReturn('12345');
|
||||
|
||||
$this->cache = new DatabaseCache('localhost', DI::dba());
|
||||
$this->lock = new DatabaseLock(DI::dba());
|
||||
PHPMockery::mock("Friendica\\Module", "function_exists")->with('opcache_get_status')->once()->andReturn(true);
|
||||
PHPMockery::mock("Friendica\\Module", "opcache_get_status")->with(false)->once()->andReturn(false);
|
||||
|
||||
$response = (new StatsCaching(DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], $this->config, $this->cache, $this->lock, []))
|
||||
->run($this->httpExceptionMock, $request);
|
||||
|
||||
self::assertJson($response->getBody());
|
||||
self::assertEquals(['Content-type' => ['application/json; charset=utf-8'], ICanCreateResponses::X_HEADER => ['json']], $response->getHeaders());
|
||||
|
||||
$json = json_decode($response->getBody(), true);
|
||||
|
||||
self::assertEquals([
|
||||
'enabled' => false,
|
||||
'hit_rate' => null,
|
||||
'used_memory' => null,
|
||||
'free_memory' => null,
|
||||
'num_cached_scripts' => null,
|
||||
], $json['opcache']);
|
||||
self::assertEquals(['type' => 'database'], $json['cache']);
|
||||
self::assertEquals(['type' => 'database'], $json['lock']);
|
||||
}
|
||||
|
||||
public function testStatsCachingWithOpcacheAndValues()
|
||||
{
|
||||
$request = [
|
||||
'key' => '12345',
|
||||
];
|
||||
$this->config->shouldReceive('get')->with('system', 'stats_key')->twice()->andReturn('12345');
|
||||
|
||||
$this->cache = new DatabaseCache('localhost', DI::dba());
|
||||
$this->lock = new DatabaseLock(DI::dba());
|
||||
PHPMockery::mock("Friendica\\Module", "function_exists")->with('opcache_get_status')->once()->andReturn(true);
|
||||
PHPMockery::mock("Friendica\\Module", "opcache_get_status")->with(false)->once()->andReturn([
|
||||
'opcache_enabled' => true,
|
||||
'opcache_statistics' => [
|
||||
'opcache_hit_rate' => 1,
|
||||
'num_cached_scripts' => 2,
|
||||
],
|
||||
'memory_usage' => [
|
||||
'used_memory' => 3,
|
||||
'free_memory' => 4,
|
||||
]
|
||||
]);
|
||||
|
||||
$response = (new StatsCaching(DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], $this->config, $this->cache, $this->lock, []))
|
||||
->run($this->httpExceptionMock, $request);
|
||||
|
||||
self::assertJson($response->getBody());
|
||||
self::assertEquals(['Content-type' => ['application/json; charset=utf-8'], ICanCreateResponses::X_HEADER => ['json']], $response->getHeaders());
|
||||
|
||||
$json = json_decode($response->getBody(), true);
|
||||
|
||||
self::assertEquals([
|
||||
'enabled' => true,
|
||||
'hit_rate' => 1,
|
||||
'used_memory' => 3,
|
||||
'free_memory' => 4,
|
||||
'num_cached_scripts' => 2,
|
||||
], $json['opcache']);
|
||||
self::assertEquals(['type' => 'database'], $json['cache']);
|
||||
self::assertEquals(['type' => 'database'], $json['lock']);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue