Refactor Cache/Lock to DICE
- Refactor Cache classes - Refactor Lock classes - Improved test speed (removed some seperate class annotations)
This commit is contained in:
parent
b95d4f41b9
commit
d56bd28a07
40 changed files with 766 additions and 621 deletions
|
@ -17,7 +17,7 @@ class APCuCacheDriverTest extends MemoryCacheTest
|
|||
|
||||
protected function getInstance()
|
||||
{
|
||||
$this->cache = new APCuCache();
|
||||
$this->cache = new APCuCache('localhost');
|
||||
return $this->cache;
|
||||
}
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@ class ArrayCacheDriverTest extends MemoryCacheTest
|
|||
{
|
||||
protected function getInstance()
|
||||
{
|
||||
$this->cache = new ArrayCache();
|
||||
$this->cache = new ArrayCache('localhost');
|
||||
return $this->cache;
|
||||
}
|
||||
|
||||
|
|
|
@ -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');
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue