]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/KeyValueStorage/KeyValueStorageTest.php
Merge pull request #12679 from nupplaphil/feat/reduce_config_dependency
[friendica.git] / tests / src / Core / KeyValueStorage / KeyValueStorageTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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\KeyValueStorage;
23
24 use Friendica\Core\KeyValueStorage\Capabilities\IManageKeyValuePairs;
25 use Friendica\Test\MockedTest;
26
27 abstract class KeyValueStorageTest extends MockedTest
28 {
29         abstract public function getInstance(): IManageKeyValuePairs;
30
31         public function testInstance()
32         {
33                 $instance = $this->getInstance();
34
35                 self::assertInstanceOf(IManageKeyValuePairs::class, $instance);
36         }
37
38         public function dataTests(): array
39         {
40                 return [
41                         'string'       => ['k' => 'data', 'v' => 'it'],
42                         'boolTrue'     => ['k' => 'data', 'v' => true],
43                         'boolFalse'    => ['k' => 'data', 'v' => false],
44                         'integer'      => ['k' => 'data', 'v' => 235],
45                         'decimal'      => ['k' => 'data', 'v' => 2.456],
46                         'array'        => ['k' => 'data', 'v' => ['1', 2, '3', true, false]],
47                         'boolIntTrue'  => ['k' => 'data', 'v' => 1],
48                         'boolIntFalse' => ['k' => 'data', 'v' => 0],
49                 ];
50         }
51
52         /**
53          * @dataProvider dataTests
54          */
55         public function testGetSetDelete($k, $v)
56         {
57                 $instance = $this->getInstance();
58
59                 $instance->set($k, $v);
60
61                 self::assertEquals($v, $instance->get($k));
62                 self::assertEquals($v, $instance[$k]);
63
64                 $instance->delete($k);
65
66                 self::assertNull($instance->get($k));
67                 self::assertNull($instance[$k]);
68         }
69
70         /**
71          * @dataProvider dataTests
72          */
73         public function testSetOverride($k, $v)
74         {
75                 $instance = $this->getInstance();
76
77                 $instance->set($k, $v);
78
79                 self::assertEquals($v, $instance->get($k));
80                 self::assertEquals($v, $instance[$k]);
81
82                 $instance->set($k, 'another_value');
83
84                 self::assertEquals('another_value', $instance->get($k));
85                 self::assertEquals('another_value', $instance[$k]);
86         }
87
88         /**
89          * @dataProvider dataTests
90          */
91         public function testOffsetSetDelete($k, $v)
92         {
93                 $instance = $this->getInstance();
94
95                 $instance[$k] = $v;
96
97                 self::assertEquals($v, $instance->get($k));
98                 self::assertEquals($v, $instance[$k]);
99
100                 unset($instance[$k]);
101
102                 self::assertNull($instance->get($k));
103                 self::assertNull($instance[$k]);
104         }
105 }