]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/Cache/MemoryCacheTest.php
Happy New Year 2023!
[friendica.git] / tests / src / Core / Cache / MemoryCacheTest.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\Cache;
23
24 use Exception;
25 use Friendica\Core\Cache\Capability\ICanCacheInMemory;
26
27 abstract class MemoryCacheTest extends CacheTest
28 {
29         /**
30          * @var \Friendica\Core\Cache\Capability\ICanCacheInMemory
31          */
32         protected $instance;
33
34         protected function setUp(): void
35         {
36                 parent::setUp();
37
38                 if (!($this->instance instanceof ICanCacheInMemory)) {
39                         throw new Exception('MemoryCacheTest unsupported');
40                 }
41         }
42
43         /**
44          * @small
45          * @dataProvider dataSimple
46          */
47         public function testCompareSet($value1, $value2)
48         {
49                 self::assertNull($this->instance->get('value1'));
50
51                 $this->instance->add('value1', $value1);
52                 $received = $this->instance->get('value1');
53                 self::assertEquals($value1, $received, 'Value received from cache not equal to the original');
54
55                 $this->instance->compareSet('value1', $value1, $value2);
56                 $received = $this->instance->get('value1');
57                 self::assertEquals($value2, $received, 'Value not overwritten by compareSet');
58         }
59
60         /**
61          * @small
62          * @dataProvider dataSimple
63          */
64         public function testNegativeCompareSet($value1, $value2)
65         {
66                 self::assertNull($this->instance->get('value1'));
67
68                 $this->instance->add('value1', $value1);
69                 $received = $this->instance->get('value1');
70                 self::assertEquals($value1, $received, 'Value received from cache not equal to the original');
71
72                 $this->instance->compareSet('value1', 'wrong', $value2);
73                 $received = $this->instance->get('value1');
74                 self::assertNotEquals($value2, $received, 'Value was wrongly overwritten by compareSet');
75                 self::assertEquals($value1, $received, 'Value was wrongly overwritten by any other value');
76         }
77
78         /**
79          * @small
80          * @dataProvider dataSimple
81          */
82         public function testCompareDelete($data)
83         {
84                 self::assertNull($this->instance->get('value1'));
85
86                 $this->instance->add('value1', $data);
87                 $received = $this->instance->get('value1');
88                 self::assertEquals($data, $received, 'Value received from cache not equal to the original');
89                 $this->instance->compareDelete('value1', $data);
90                 self::assertNull($this->instance->get('value1'), 'Value was not deleted by compareDelete');
91         }
92
93         /**
94          * @small
95          * @dataProvider dataSimple
96          */
97         public function testNegativeCompareDelete($data)
98         {
99                 self::assertNull($this->instance->get('value1'));
100
101                 $this->instance->add('value1', $data);
102                 $received = $this->instance->get('value1');
103                 self::assertEquals($data, $received, 'Value received from cache not equal to the original');
104                 $this->instance->compareDelete('value1', 'wrong');
105                 self::assertNotNull($this->instance->get('value1'), 'Value was wrongly compareDeleted');
106
107                 $this->instance->compareDelete('value1', $data);
108                 self::assertNull($this->instance->get('value1'), 'Value was wrongly NOT deleted by compareDelete');
109         }
110
111         /**
112          * @small
113          * @dataProvider dataSimple
114          */
115         public function testAdd($value1, $value2)
116         {
117                 self::assertNull($this->instance->get('value1'));
118
119                 $this->instance->add('value1', $value1);
120
121                 $this->instance->add('value1', $value2);
122                 $received = $this->instance->get('value1');
123                 self::assertNotEquals($value2, $received, 'Value was wrongly overwritten by add');
124                 self::assertEquals($value1, $received, 'Value was wrongly overwritten by any other value');
125
126                 $this->instance->delete('value1');
127                 $this->instance->add('value1', $value2);
128                 $received = $this->instance->get('value1');
129                 self::assertEquals($value2, $received, 'Value was not overwritten by add');
130                 self::assertNotEquals($value1, $received, 'Value was not overwritten by any other value');
131         }
132 }