X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=tests%2Fsrc%2FCore%2FStorageManagerTest.php;h=30342099c55bdfb445e2614e64d3ec59c5e20a0a;hb=53237a0259477b13eebcd98fd872d3255e41e860;hp=82af38b4aae0e41fe155874e81aeed3abaeac7ed;hpb=dbd5b5bb6e329ceb216fc2c13540e9f482129ccd;p=friendica.git diff --git a/tests/src/Core/StorageManagerTest.php b/tests/src/Core/StorageManagerTest.php index 82af38b4aa..30342099c5 100644 --- a/tests/src/Core/StorageManagerTest.php +++ b/tests/src/Core/StorageManagerTest.php @@ -1,17 +1,40 @@ . + * + */ namespace Friendica\Test\src\Core; use Dice\Dice; -use Friendica\Core\Config\IConfiguration; -use Friendica\Core\Config\PreloadConfiguration; +use Friendica\Core\Config\IConfig; +use Friendica\Core\Config\PreloadConfig; +use Friendica\Core\Hook; +use Friendica\Core\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; use Friendica\Core\Session; +use Friendica\Database\DBStructure; use Friendica\Test\DatabaseTest; use Friendica\Test\Util\Database\StaticDatabase; use Friendica\Test\Util\VFSTrait; @@ -25,12 +48,12 @@ class StorageManagerTest extends DatabaseTest { /** @var Database */ private $dba; - /** @var IConfiguration */ + /** @var IConfig */ private $config; /** @var LoggerInterface */ private $logger; - /** @var Dice */ - private $dice; + /** @var L10n */ + private $l10n; use VFSTrait; @@ -41,10 +64,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); @@ -56,8 +75,12 @@ class StorageManagerTest extends DatabaseTest $this->dba = new StaticDatabase($configCache, $profiler, $this->logger); + $this->dba->setTestmode(true); + $configModel = new Config($this->dba); - $this->config = new PreloadConfiguration($configCache, $configModel); + $this->config = new PreloadConfig($configCache, $configModel); + + $this->l10n = \Mockery::mock(L10n::class); } /** @@ -65,7 +88,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); } @@ -108,21 +131,52 @@ class StorageManagerTest extends DatabaseTest ]; } + /** + * Data array for legacy backends + * + * @todo 2020.09 After 2 releases, remove the legacy functionality and these data array with it + * + * @return array + */ + public function dataLegacyBackends() + { + return [ + 'legacyDatabase' => [ + 'name' => 'Friendica\Model\Storage\Database', + 'assert' => Storage\Database::class, + 'assertName' => Storage\Database::NAME, + 'userBackend' => true, + ], + 'legacyFilesystem' => [ + 'name' => 'Friendica\Model\Storage\Filesystem', + 'assert' => Storage\Filesystem::class, + 'assertName' => Storage\Filesystem::NAME, + 'userBackend' => true, + ], + 'legacySystemResource' => [ + 'name' => 'Friendica\Model\Storage\SystemResource', + 'assert' => Storage\SystemResource::class, + 'assertName' => Storage\SystemResource::NAME, + 'userBackend' => false, + ], + ]; + } + /** * Test the getByName() method * * @dataProvider dataStorages + * @dataProvider dataLegacyBackends */ - 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); $this->assertInstanceOf($assert, $storage); - $this->assertEquals($name, $storage::getName()); } else { $this->assertNull($storage); } @@ -136,9 +190,13 @@ 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); + + // true in every of the backends + $this->assertEquals(!empty($assertName), $storageManager->isValidBackend($name)); - $this->assertEquals($userBackend, $storageManager->isValidBackend($name)); + // if userBackend is set to true, filter out e.g. SystemRessource + $this->assertEquals($userBackend, $storageManager->isValidBackend($name, true)); } /** @@ -146,7 +204,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 +216,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()); @@ -173,12 +231,13 @@ class StorageManagerTest extends DatabaseTest * Test the method getBackend() with a pre-configured backend * * @dataProvider dataStorages + * @dataProvider dataLegacyBackends */ public function testPresetBackend($name, $assert, $assertName, $userBackend) { $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 +255,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 +303,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); @@ -249,4 +319,17 @@ class StorageManagerTest extends DatabaseTest $this->assertNotEmpty($data); } } + + /** + * Test moving data to a WRONG storage + * + * @expectedException \Friendica\Model\Storage\StorageException + * @expectedExceptionMessage Can't move to storage backend 'SystemResource' + */ + public function testMoveStorageWrong() + { + $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n); + $storage = $storageManager->getByName(Storage\SystemResource::getName()); + $storageManager->move($storage); + } }