Implement Hook::callAll('storage_instance') call for addons and add a description for it.

- Remove implicit Dice usage
- Add concrete instance creating
- Adding Hook call for addon instance creating
- Updating doc for Hook
- Updating tests
This commit is contained in:
nupplaPhil 2020-01-08 22:51:37 +01:00
parent 5d8e6c33ef
commit bfae6766bf
No known key found for this signature in database
GPG key ID: D8365C3D36B77D90
8 changed files with 155 additions and 62 deletions

View file

@ -5,9 +5,12 @@ namespace Friendica\Test\src\Core;
use Dice\Dice;
use Friendica\Core\Config\IConfiguration;
use Friendica\Core\Config\PreloadConfiguration;
use Friendica\Core\Hook;
use Friendica\Core\L10n\L10n;
use Friendica\Core\Session\ISession;
use Friendica\Core\StorageManager;
use Friendica\Database\Database;
use Friendica\DI;
use Friendica\Factory\ConfigFactory;
use Friendica\Model\Config\Config;
use Friendica\Model\Storage;
@ -21,6 +24,12 @@ use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Friendica\Test\Util\SampleStorageBackend;
/**
* @todo Rework Hook:: methods to dynamic to remove the separated process annotation
*
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*/
class StorageManagerTest extends DatabaseTest
{
/** @var Database */
@ -29,8 +38,8 @@ class StorageManagerTest extends DatabaseTest
private $config;
/** @var LoggerInterface */
private $logger;
/** @var Dice */
private $dice;
/** @var L10n */
private $l10n;
use VFSTrait;
@ -41,10 +50,6 @@ class StorageManagerTest extends DatabaseTest
$this->setUpVfsDir();
$this->logger = new NullLogger();
$this->dice = (new Dice())
->addRules(include __DIR__ . '/../../../static/dependencies.config.php')
->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true])
->addRule(ISession::class, ['instanceOf' => Session\Memory::class, 'shared' => true, 'call' => null]);
$profiler = \Mockery::mock(Profiler::class);
$profiler->shouldReceive('saveTimestamp')->withAnyArgs()->andReturn(true);
@ -58,6 +63,8 @@ class StorageManagerTest extends DatabaseTest
$configModel = new Config($this->dba);
$this->config = new PreloadConfiguration($configCache, $configModel);
$this->l10n = \Mockery::mock(L10n::class);
}
/**
@ -65,7 +72,7 @@ class StorageManagerTest extends DatabaseTest
*/
public function testInstance()
{
$storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->dice);
$storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
$this->assertInstanceOf(StorageManager::class, $storageManager);
}
@ -113,11 +120,11 @@ class StorageManagerTest extends DatabaseTest
*
* @dataProvider dataStorages
*/
public function testGetByName($name, $assert, $assertName)
public function testGetByName($name, $assert, $assertName, $userBackend)
{
$storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->dice);
$storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
$storage = $storageManager->getByName($name);
$storage = $storageManager->getByName($name, $userBackend);
if (!empty($assert)) {
$this->assertInstanceOf(Storage\IStorage::class, $storage);
@ -136,7 +143,7 @@ class StorageManagerTest extends DatabaseTest
*/
public function testIsValidBackend($name, $assert, $assertName, $userBackend)
{
$storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->dice);
$storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
$this->assertEquals($userBackend, $storageManager->isValidBackend($name));
}
@ -146,7 +153,7 @@ class StorageManagerTest extends DatabaseTest
*/
public function testListBackends()
{
$storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->dice);
$storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
$this->assertEquals(StorageManager::DEFAULT_BACKENDS, $storageManager->listBackends());
}
@ -158,7 +165,7 @@ class StorageManagerTest extends DatabaseTest
*/
public function testGetBackend($name, $assert, $assertName, $userBackend)
{
$storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->dice);
$storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
$this->assertNull($storageManager->getBackend());
@ -178,7 +185,7 @@ class StorageManagerTest extends DatabaseTest
{
$this->config->set('storage', 'name', $name);
$storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->dice);
$storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
if ($userBackend) {
$this->assertInstanceOf($assert, $storageManager->getBackend());
@ -196,17 +203,28 @@ class StorageManagerTest extends DatabaseTest
*/
public function testRegisterUnregisterBackends()
{
$storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->dice);
/// @todo Remove dice once "Hook" is dynamic and mockable
$dice = (new Dice())
->addRules(include __DIR__ . '/../../../static/dependencies.config.php')
->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true])
->addRule(ISession::class, ['instanceOf' => Session\Memory::class, 'shared' => true, 'call' => null]);
DI::init($dice);
$storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
$this->assertTrue($storageManager->register(SampleStorageBackend::class));
$this->assertEquals(array_merge(StorageManager::DEFAULT_BACKENDS, [
'Sample Storage' => SampleStorageBackend::class,
SampleStorageBackend::getName() => SampleStorageBackend::class,
]), $storageManager->listBackends());
$this->assertEquals(array_merge(StorageManager::DEFAULT_BACKENDS, [
'Sample Storage' => SampleStorageBackend::class,
SampleStorageBackend::getName() => SampleStorageBackend::class,
]), $this->config->get('storage', 'backends'));
// inline call to register own class as hook (testing purpose only)
SampleStorageBackend::registerHook();
Hook::loadHooks();
$this->assertTrue($storageManager->setBackend(SampleStorageBackend::NAME));
$this->assertEquals(SampleStorageBackend::NAME, $this->config->get('storage', 'name'));
@ -233,7 +251,7 @@ class StorageManagerTest extends DatabaseTest
$this->loadFixture(__DIR__ . '/../../datasets/storage/database.fixture.php', $this->dba);
$storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->dice);
$storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
$storage = $storageManager->getByName($name);
$storageManager->move($storage);