mirror of
https://github.com/friendica/friendica
synced 2025-04-22 07:10:24 +00:00
adapt tests
This commit is contained in:
parent
d272e8c3c7
commit
326566638f
16 changed files with 254 additions and 712 deletions
|
@ -23,22 +23,25 @@ namespace Friendica\Test\src\Core\Config;
|
|||
|
||||
use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
|
||||
use Friendica\Core\Config\Capability\IManageConfigValues;
|
||||
use Friendica\Core\Config\Repository\Config as ConfigModel;
|
||||
use Friendica\Core\Config\Model\Config;
|
||||
use Friendica\Core\Config\Util\ConfigFileManager;
|
||||
use Friendica\Core\Config\Util\ConfigFileTransformer;
|
||||
use Friendica\Core\Config\ValueObject\Cache;
|
||||
use Friendica\Test\MockedTest;
|
||||
use Mockery\MockInterface;
|
||||
use Mockery;
|
||||
use Friendica\Test\Util\VFSTrait;
|
||||
use org\bovigo\vfs\vfsStream;
|
||||
|
||||
abstract class ConfigTest extends MockedTest
|
||||
class ConfigTest extends MockedTest
|
||||
{
|
||||
use ArraySubsetAsserts;
|
||||
|
||||
/** @var ConfigModel|MockInterface */
|
||||
protected $configModel;
|
||||
use VFSTrait;
|
||||
|
||||
/** @var Cache */
|
||||
protected $configCache;
|
||||
|
||||
/** @var ConfigFileManager */
|
||||
protected $configFileManager;
|
||||
|
||||
/** @var IManageConfigValues */
|
||||
protected $testedConfig;
|
||||
|
||||
|
@ -60,17 +63,22 @@ abstract class ConfigTest extends MockedTest
|
|||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->setUpVfsDir();
|
||||
|
||||
parent::setUp();
|
||||
|
||||
// Create the config model
|
||||
$this->configModel = Mockery::mock(ConfigModel::class);
|
||||
$this->configCache = new Cache();
|
||||
$this->configFileManager = new ConfigFileManager($this->root->url(), $this->root->url() . '/config/', $this->root->url() . '/static/');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return IManageConfigValues
|
||||
*/
|
||||
abstract public function getInstance();
|
||||
public function getInstance()
|
||||
{
|
||||
$this->configFileManager->setupCache($this->configCache, []);
|
||||
return new Config($this->configFileManager, $this->configCache);
|
||||
}
|
||||
|
||||
public function dataTests()
|
||||
{
|
||||
|
@ -156,12 +164,13 @@ abstract class ConfigTest extends MockedTest
|
|||
|
||||
/**
|
||||
* Test the configuration initialization
|
||||
* @dataProvider dataConfigLoad
|
||||
*/
|
||||
public function testSetUp(array $data)
|
||||
{
|
||||
$this->configModel->shouldReceive('isConnected')
|
||||
->andReturn(true)
|
||||
->once();
|
||||
vfsStream::newFile(ConfigFileManager::CONFIG_DATA_FILE)
|
||||
->at($this->root->getChild('config'))
|
||||
->setContent(ConfigFileTransformer::encode($data));
|
||||
|
||||
$this->testedConfig = $this->getInstance();
|
||||
self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
|
||||
|
@ -171,19 +180,23 @@ abstract class ConfigTest extends MockedTest
|
|||
}
|
||||
|
||||
/**
|
||||
* Test the configuration load() method
|
||||
* Test the configuration reload() method
|
||||
*
|
||||
* @param array $data
|
||||
* @param array $load
|
||||
*
|
||||
* @dataProvider dataConfigLoad
|
||||
*/
|
||||
public function testLoad(array $data, array $load)
|
||||
public function testReload(array $data, array $load)
|
||||
{
|
||||
vfsStream::newFile(ConfigFileManager::CONFIG_DATA_FILE)
|
||||
->at($this->root->getChild('config'))
|
||||
->setContent(ConfigFileTransformer::encode($data));
|
||||
|
||||
$this->testedConfig = $this->getInstance();
|
||||
self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
|
||||
|
||||
foreach ($load as $loadedCats) {
|
||||
$this->testedConfig->load($loadedCats);
|
||||
}
|
||||
$this->testedConfig->reload();
|
||||
|
||||
// Assert at least loaded cats are loaded
|
||||
foreach ($load as $loadedCats) {
|
||||
|
@ -256,23 +269,31 @@ abstract class ConfigTest extends MockedTest
|
|||
|
||||
/**
|
||||
* Test the configuration load() method with overwrite
|
||||
*
|
||||
* @dataProvider dataDoubleLoad
|
||||
*/
|
||||
public function testCacheLoadDouble(array $data1, array $data2, array $expect = [])
|
||||
{
|
||||
vfsStream::newFile(ConfigFileManager::CONFIG_DATA_FILE)
|
||||
->at($this->root->getChild('config'))
|
||||
->setContent(ConfigFileTransformer::encode($data1));
|
||||
|
||||
$this->testedConfig = $this->getInstance();
|
||||
self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
|
||||
|
||||
foreach ($data1 as $cat => $data) {
|
||||
$this->testedConfig->load($cat);
|
||||
}
|
||||
|
||||
// Assert at least loaded cats are loaded
|
||||
foreach ($data1 as $cat => $data) {
|
||||
self::assertConfig($cat, $data);
|
||||
}
|
||||
|
||||
vfsStream::newFile(ConfigFileManager::CONFIG_DATA_FILE)
|
||||
->at($this->root->getChild('config'))
|
||||
->setContent(ConfigFileTransformer::encode($data2));
|
||||
|
||||
$this->testedConfig->reload();
|
||||
|
||||
foreach ($data2 as $cat => $data) {
|
||||
$this->testedConfig->load($cat);
|
||||
self::assertConfig($cat, $data);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -281,44 +302,19 @@ abstract class ConfigTest extends MockedTest
|
|||
*/
|
||||
public function testLoadWrong()
|
||||
{
|
||||
$this->configModel->shouldReceive('isConnected')->andReturn(true)->once();
|
||||
$this->configModel->shouldReceive('load')->withAnyArgs()->andReturn([])->once();
|
||||
|
||||
$this->testedConfig = $this->getInstance();
|
||||
$this->testedConfig = new Config($this->configFileManager, new Cache());
|
||||
self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
|
||||
|
||||
self::assertEmpty($this->testedConfig->getCache()->getAll());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the configuration get() and set() methods without adapter
|
||||
* Test the configuration get() and set() methods
|
||||
*
|
||||
* @dataProvider dataTests
|
||||
*/
|
||||
public function testSetGetWithoutDB($data)
|
||||
public function testSetGet($data)
|
||||
{
|
||||
$this->configModel->shouldReceive('isConnected')
|
||||
->andReturn(false)
|
||||
->times(3);
|
||||
|
||||
$this->testedConfig = $this->getInstance();
|
||||
self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
|
||||
|
||||
self::assertTrue($this->testedConfig->set('test', 'it', $data));
|
||||
|
||||
self::assertEquals($data, $this->testedConfig->get('test', 'it'));
|
||||
self::assertEquals($data, $this->testedConfig->getCache()->get('test', 'it'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the configuration get() and set() methods with a model/db
|
||||
*
|
||||
* @dataProvider dataTests
|
||||
*/
|
||||
public function testSetGetWithDB($data)
|
||||
{
|
||||
$this->configModel->shouldReceive('set')->with('test', 'it', $data)->andReturn(true)->once();
|
||||
|
||||
$this->testedConfig = $this->getInstance();
|
||||
self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
|
||||
|
||||
|
@ -349,41 +345,16 @@ abstract class ConfigTest extends MockedTest
|
|||
self::assertEquals('default', $this->testedConfig->get('test', 'it', 'default', true));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the configuration get() method with refresh
|
||||
*
|
||||
* @dataProvider dataTests
|
||||
*/
|
||||
public function testGetWithRefresh($data)
|
||||
{
|
||||
$this->configCache->load(['test' => ['it' => 'now']], Cache::SOURCE_FILE);
|
||||
|
||||
$this->testedConfig = $this->getInstance();
|
||||
self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
|
||||
|
||||
// without refresh
|
||||
self::assertEquals('now', $this->testedConfig->get('test', 'it'));
|
||||
self::assertEquals('now', $this->testedConfig->getCache()->get('test', 'it'));
|
||||
|
||||
// with refresh
|
||||
self::assertEquals($data, $this->testedConfig->get('test', 'it', null, true));
|
||||
self::assertEquals($data, $this->testedConfig->getCache()->get('test', 'it'));
|
||||
|
||||
// without refresh and wrong value and default
|
||||
self::assertEquals('default', $this->testedConfig->get('test', 'not', 'default'));
|
||||
self::assertNull($this->testedConfig->getCache()->get('test', 'not'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the configuration delete() method without a model/db
|
||||
*
|
||||
* @dataProvider dataTests
|
||||
*/
|
||||
public function testDeleteWithoutDB($data)
|
||||
public function testDelete($data)
|
||||
{
|
||||
$this->configCache->load(['test' => ['it' => $data]], Cache::SOURCE_FILE);
|
||||
|
||||
$this->testedConfig = $this->getInstance();
|
||||
$this->testedConfig = new Config($this->configFileManager, $this->configCache);
|
||||
self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
|
||||
|
||||
self::assertEquals($data, $this->testedConfig->get('test', 'it'));
|
||||
|
@ -396,51 +367,6 @@ abstract class ConfigTest extends MockedTest
|
|||
self::assertEmpty($this->testedConfig->getCache()->getAll());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the configuration delete() method with a model/db
|
||||
*/
|
||||
public function testDeleteWithDB()
|
||||
{
|
||||
$this->configCache->load(['test' => ['it' => 'now', 'quarter' => 'true']], Cache::SOURCE_FILE);
|
||||
|
||||
$this->configModel->shouldReceive('delete')
|
||||
->with('test', 'it')
|
||||
->andReturn(false)
|
||||
->once();
|
||||
$this->configModel->shouldReceive('delete')
|
||||
->with('test', 'second')
|
||||
->andReturn(true)
|
||||
->once();
|
||||
$this->configModel->shouldReceive('delete')
|
||||
->with('test', 'third')
|
||||
->andReturn(false)
|
||||
->once();
|
||||
$this->configModel->shouldReceive('delete')
|
||||
->with('test', 'quarter')
|
||||
->andReturn(true)
|
||||
->once();
|
||||
|
||||
$this->testedConfig = $this->getInstance();
|
||||
self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
|
||||
|
||||
// directly set the value to the cache
|
||||
$this->testedConfig->getCache()->set('test', 'it', 'now');
|
||||
|
||||
self::assertEquals('now', $this->testedConfig->get('test', 'it'));
|
||||
self::assertEquals('now', $this->testedConfig->getCache()->get('test', 'it'));
|
||||
|
||||
// delete from cache only
|
||||
self::assertTrue($this->testedConfig->delete('test', 'it'));
|
||||
// delete from db only
|
||||
self::assertTrue($this->testedConfig->delete('test', 'second'));
|
||||
// no delete
|
||||
self::assertFalse($this->testedConfig->delete('test', 'third'));
|
||||
// delete both
|
||||
self::assertTrue($this->testedConfig->delete('test', 'quarter'));
|
||||
|
||||
self::assertEmpty($this->testedConfig->getCache()->getAll());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the configuration get() and set() method where the db value has a higher prio than the config file
|
||||
*/
|
||||
|
@ -462,6 +388,12 @@ abstract class ConfigTest extends MockedTest
|
|||
*/
|
||||
public function testSetGetLowPrio()
|
||||
{
|
||||
vfsStream::newFile(ConfigFileManager::CONFIG_DATA_FILE)
|
||||
->at($this->root->getChild('config'))
|
||||
->setContent(ConfigFileTransformer::encode([
|
||||
'config' => ['test' => 'it'],
|
||||
]));
|
||||
|
||||
$this->testedConfig = $this->getInstance();
|
||||
self::assertInstanceOf(Cache::class, $this->testedConfig->getCache());
|
||||
self::assertEquals('it', $this->testedConfig->get('config', 'test'));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue