]> git.mxchange.org Git - friendica.git/commitdiff
Fix Storage Exceptions
authorPhilipp <admin@philipp.info>
Sun, 1 Aug 2021 12:31:57 +0000 (14:31 +0200)
committerPhilipp <admin@philipp.info>
Mon, 16 Aug 2021 21:27:42 +0000 (23:27 +0200)
src/Model/Storage/Database.php
tests/Util/SampleStorageBackend.php
tests/src/Core/StorageManagerTest.php
tests/src/Model/Storage/DatabaseStorageTest.php
tests/src/Model/Storage/FilesystemStorageTest.php
tests/src/Model/Storage/StorageTest.php

index 5e83ca85680aac43086ca9921ad3b04f0c13ea19..9edfe2170897f6d08b832eaa06c572e518380467 100644 (file)
@@ -57,7 +57,11 @@ class Database implements ISelectableStorage
 
                        return $result['data'];
                } catch (Exception $exception) {
-                       throw new StorageException(sprintf('Database storage failed to get %s', $reference), $exception->getCode(), $exception);
+                       if ($exception instanceof ReferenceStorageException) {
+                               throw $exception;
+                       } else {
+                               throw new StorageException(sprintf('Database storage failed to get %s', $reference), $exception->getCode(), $exception);
+                       }
                }
        }
 
@@ -101,7 +105,11 @@ class Database implements ISelectableStorage
                                throw new ReferenceStorageException(sprintf('Database storage failed to delete %s', $reference));
                        }
                } catch (Exception $exception) {
-                       throw new StorageException(sprintf('Database storage failed to delete %s', $reference), $exception->getCode(), $exception);
+                       if ($exception instanceof ReferenceStorageException) {
+                               throw $exception;
+                       } else {
+                               throw new StorageException(sprintf('Database storage failed to delete %s', $reference), $exception->getCode(), $exception);
+                       }
                }
        }
 
index 3d5789b0ded93797a3428ce63e62d7b96a9e75cf..1ebd64f6ee5136374d1936de95afe0d1317f975b 100644 (file)
 namespace Friendica\Test\Util;
 
 use Friendica\Core\Hook;
-use Friendica\Model\Storage\IStorage;
+use Friendica\Model\Storage\ISelectableStorage;
 
 use Friendica\Core\L10n;
 
 /**
  * A backend storage example class
  */
-class SampleStorageBackend implements IStorage
+class SampleStorageBackend implements ISelectableStorage
 {
        const NAME = 'Sample Storage';
 
@@ -62,14 +62,14 @@ class SampleStorageBackend implements IStorage
                $this->l10n = $l10n;
        }
 
-       public function get(string $reference)
+       public function get(string $reference): string
        {
                // we return always the same image data. Which file we load is defined by
                // a config key
-               return $this->data[$reference] ?? null;
+               return $this->data[$reference] ?? '';
        }
 
-       public function put(string $data, string $reference = '')
+       public function put(string $data, string $reference = ''): string
        {
                if ($reference === '') {
                        $reference = 'sample';
@@ -89,12 +89,12 @@ class SampleStorageBackend implements IStorage
                return true;
        }
 
-       public function getOptions()
+       public function getOptions(): array
        {
                return $this->options;
        }
 
-       public function saveOptions(array $data)
+       public function saveOptions(array $data): array
        {
                $this->options = $data;
 
@@ -107,7 +107,7 @@ class SampleStorageBackend implements IStorage
                return self::NAME;
        }
 
-       public static function getName()
+       public static function getName(): string
        {
                return self::NAME;
        }
@@ -120,4 +120,3 @@ class SampleStorageBackend implements IStorage
                Hook::register('storage_instance', __DIR__ . '/SampleStorageBackendInstance.php', 'create_instance');
        }
 }
-
index 537fd841e43a21e922c9d38d019a95e0ba43809c..f01640dce404f78787b6111fb3a999c584b1fc5c 100644 (file)
@@ -177,7 +177,11 @@ class StorageManagerTest extends DatabaseTest
        {
                $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n, $this->httpRequest);
 
-               $storage = $storageManager->getByName($name, $userBackend);
+               if ($userBackend) {
+                       $storage = $storageManager->getSelectableStorageByName($name);
+               } else {
+                       $storage = $storageManager->getByName($name);
+               }
 
                if (!empty($assert)) {
                        self::assertInstanceOf(Storage\IStorage::class, $storage);
@@ -195,7 +199,7 @@ class StorageManagerTest extends DatabaseTest
         */
        public function testIsValidBackend($name, $assert, $assertName, $userBackend)
        {
-               $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n, $this->httpRequest);
+               $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
 
                // true in every of the backends
                self::assertEquals(!empty($assertName), $storageManager->isValidBackend($name));
@@ -209,7 +213,7 @@ class StorageManagerTest extends DatabaseTest
         */
        public function testListBackends()
        {
-               $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n, $this->httpRequest);
+               $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
 
                self::assertEquals(StorageManager::DEFAULT_BACKENDS, $storageManager->listBackends());
        }
@@ -221,12 +225,13 @@ class StorageManagerTest extends DatabaseTest
         */
        public function testGetBackend($name, $assert, $assertName, $userBackend)
        {
-               $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n, $this->httpRequest);
+               $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
 
                self::assertNull($storageManager->getBackend());
 
                if ($userBackend) {
-                       $storageManager->setBackend($name);
+                       $selBackend = $storageManager->getSelectableStorageByName($name);
+                       $storageManager->setBackend($selBackend);
 
                        self::assertInstanceOf($assert, $storageManager->getBackend());
                }
@@ -242,7 +247,7 @@ class StorageManagerTest extends DatabaseTest
        {
                $this->config->set('storage', 'name', $name);
 
-               $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n, $this->httpRequest);
+               $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
 
                if ($userBackend) {
                        self::assertInstanceOf($assert, $storageManager->getBackend());
@@ -267,7 +272,7 @@ class StorageManagerTest extends DatabaseTest
                        ->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->httpRequest);
+               $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
 
                self::assertTrue($storageManager->register(SampleStorageBackend::class));
 
@@ -282,7 +287,7 @@ class StorageManagerTest extends DatabaseTest
                SampleStorageBackend::registerHook();
                Hook::loadHooks();
 
-               self::assertTrue($storageManager->setBackend(SampleStorageBackend::NAME));
+               self::assertTrue($storageManager->setBackend( $storageManager->getSelectableStorageByName(SampleStorageBackend::NAME)));
                self::assertEquals(SampleStorageBackend::NAME, $this->config->get('storage', 'name'));
 
                self::assertInstanceOf(SampleStorageBackend::class, $storageManager->getBackend());
@@ -308,7 +313,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->l10n, $this->httpRequest);
+               $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
                $storage = $storageManager->getSelectableStorageByName($name);
                $storageManager->move($storage);
 
@@ -328,12 +333,11 @@ class StorageManagerTest extends DatabaseTest
        /**
         * Test moving data to a WRONG storage
         */
-       public function testMoveStorageWrong()
+       public function testWrongSelectableStorage()
        {
-               $this->expectExceptionMessage("Can't move to storage backend 'SystemResource'");
-               $this->expectException(StorageException::class);
+               $this->expectException(\TypeError::class);
 
-               $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n, $this->httpRequest);
+               $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
                $storage = $storageManager->getSelectableStorageByName(Storage\SystemResource::getName());
                $storageManager->move($storage);
        }
index 21d6e18652c03086b741818fd593f5d5a55154e8..5e61ea13a420623d76afb855fe28be1705d83716 100644 (file)
@@ -62,10 +62,7 @@ class DatabaseStorageTest extends StorageTest
 
                $dba = new StaticDatabase($configCache, $profiler, $logger);
 
-               /** @var MockInterface|L10n $l10n */
-               $l10n = \Mockery::mock(L10n::class)->makePartial();
-
-               return new Database($dba, $logger, $l10n);
+               return new Database($dba);
        }
 
        protected function assertOption(IStorage $storage)
index ebe29f0c4c49cfcdd061227f01c14aef33327480..3319ea3fa4bd0779c7cf42d97a3422633e50f8b1 100644 (file)
@@ -50,7 +50,6 @@ class FilesystemStorageTest extends StorageTest
 
        protected function getInstance()
        {
-               $logger = new NullLogger();
                $profiler = \Mockery::mock(Profiler::class);
                $profiler->shouldReceive('startRecording');
                $profiler->shouldReceive('stopRecording');
@@ -63,7 +62,7 @@ class FilesystemStorageTest extends StorageTest
                             ->with('storage', 'filesystem_path', Filesystem::DEFAULT_BASE_FOLDER)
                             ->andReturn($this->root->getChild('storage')->url());
 
-               return new Filesystem($this->config, $logger, $l10n);
+               return new Filesystem($this->config, $l10n);
        }
 
        protected function assertOption(IStorage $storage)
index d978f013d902cba570a5d493eed1c35d033c0dc7..eadddcf02d879067bb05c71011328985c0a17a16 100644 (file)
@@ -22,6 +22,8 @@
 namespace Friendica\Test\src\Model\Storage;
 
 use Friendica\Model\Storage\IStorage;
+use Friendica\Model\Storage\ReferenceStorageException;
+use Friendica\Model\Storage\StorageException;
 use Friendica\Test\MockedTest;
 
 abstract class StorageTest extends MockedTest
@@ -62,7 +64,7 @@ abstract class StorageTest extends MockedTest
 
                self::assertEquals('data12345', $instance->get($ref));
 
-               self::assertTrue($instance->delete($ref));
+               $instance->delete($ref);
        }
 
        /**
@@ -70,10 +72,11 @@ abstract class StorageTest extends MockedTest
         */
        public function testInvalidDelete()
        {
+               self::expectException(ReferenceStorageException::class);
+
                $instance = $this->getInstance();
 
-               // Even deleting not existing references should return "true"
-               self::assertTrue($instance->delete(-1234456));
+               $instance->delete(-1234456);
        }
 
        /**
@@ -81,10 +84,11 @@ abstract class StorageTest extends MockedTest
         */
        public function testInvalidGet()
        {
+               self::expectException(ReferenceStorageException::class);
+
                $instance = $this->getInstance();
 
-               // Invalid references return an empty string
-               self::assertEmpty($instance->get(-123456));
+               $instance->get(-123456);
        }
 
        /**