]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/Storage/StorageTest.php
Merge pull request #11129 from urbalazs/copyright-2022
[friendica.git] / tests / src / Core / Storage / StorageTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Test\src\Core\Storage;
23
24 use Friendica\Core\Storage\Capability\ICanReadFromStorage;
25 use Friendica\Core\Storage\Capability\ICanWriteToStorage;
26 use Friendica\Core\Storage\Exception\ReferenceStorageException;
27 use Friendica\Test\MockedTest;
28
29 abstract class StorageTest extends MockedTest
30 {
31         /** @return ICanWriteToStorage */
32         abstract protected function getInstance();
33
34         /**
35          * Test if the instance is "really" implementing the interface
36          */
37         public function testInstance()
38         {
39                 $instance = $this->getInstance();
40                 self::assertInstanceOf(ICanReadFromStorage::class, $instance);
41         }
42
43         /**
44          * Test basic put, get and delete operations
45          */
46         public function testPutGetDelete()
47         {
48                 $instance = $this->getInstance();
49
50                 $ref = $instance->put('data12345');
51                 self::assertNotEmpty($ref);
52
53                 self::assertEquals('data12345', $instance->get($ref));
54
55                 $instance->delete($ref);
56         }
57
58         /**
59          * Test a delete with an invalid reference
60          */
61         public function testInvalidDelete()
62         {
63                 self::expectException(ReferenceStorageException::class);
64
65                 $instance = $this->getInstance();
66
67                 $instance->delete(-1234456);
68         }
69
70         /**
71          * Test a get with an invalid reference
72          */
73         public function testInvalidGet()
74         {
75                 self::expectException(ReferenceStorageException::class);
76
77                 $instance = $this->getInstance();
78
79                 $instance->get(-123456);
80         }
81
82         /**
83          * Test an update with a given reference
84          */
85         public function testUpdateReference()
86         {
87                 $instance = $this->getInstance();
88
89                 $ref = $instance->put('data12345');
90                 self::assertNotEmpty($ref);
91
92                 self::assertEquals('data12345', $instance->get($ref));
93
94                 self::assertEquals($ref, $instance->put('data5432', $ref));
95                 self::assertEquals('data5432', $instance->get($ref));
96         }
97
98         /**
99          * Test that an invalid update results in an insert
100          */
101         public function testInvalidUpdate()
102         {
103                 $instance = $this->getInstance();
104
105                 self::assertEquals(-123, $instance->put('data12345', -123));
106         }
107 }