]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/Cache/CacheTest.php
Improved logging
[friendica.git] / tests / src / Core / Cache / CacheTest.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 Friendica\Core\Cache\Capability\ICanCache;
25 use Friendica\Core\Cache\Capability\ICanCacheInMemory;
26 use Friendica\Core\Cache\Type\AbstractCache;
27 use Friendica\Test\MockedTest;
28 use Friendica\Util\PidFile;
29
30 abstract class CacheTest extends MockedTest
31 {
32         /**
33          * @var int Start time of the mock (used for time operations)
34          */
35         protected $startTime = 1417011228;
36
37         /**
38          * @var ICanCache
39          */
40         protected $instance;
41
42         /**
43          * @var \Friendica\Core\Cache\Capability\ICanCacheInMemory
44          */
45         protected $cache;
46
47         /**
48          * Dataset for test setting different types in the cache
49          *
50          * @return array
51          */
52         public function dataTypesInCache()
53         {
54                 return [
55                         'string'    => ['data' => 'foobar'],
56                         'integer'   => ['data' => 1],
57                         'boolTrue'  => ['data' => true],
58                         'boolFalse' => ['data' => false],
59                         'float'     => ['data' => 4.6634234],
60                         'array'     => ['data' => ['1', '2', '3', '4', '5']],
61                         'object'    => ['data' => new PidFile()],
62                         'null'      => ['data' => null],
63                 ];
64         }
65
66         /**
67          * Dataset for simple value sets/gets
68          *
69          * @return array
70          */
71         public function dataSimple()
72         {
73                 return [
74                         'string' => [
75                                 'value1' => 'foobar',
76                                 'value2' => 'ipsum lorum',
77                                 'value3' => 'test',
78                                 'value4' => 'lasttest',
79                         ],
80                 ];
81         }
82
83         abstract protected function getInstance();
84
85         protected function setUp(): void
86         {
87                 parent::setUp();
88
89                 $this->instance = $this->getInstance();
90
91                 $this->instance->clear(false);
92         }
93
94         /**
95          * @small
96          * @dataProvider dataSimple
97          *
98          * @param mixed $value1 a first
99          * @param mixed $value2 a second
100          */
101         public function testSimple($value1, $value2)
102         {
103                 self::assertNull($this->instance->get('value1'));
104
105                 $this->instance->set('value1', $value1);
106                 $received = $this->instance->get('value1');
107                 self::assertEquals($value1, $received, 'Value received from cache not equal to the original');
108
109                 $this->instance->set('value1', $value2);
110                 $received = $this->instance->get('value1');
111                 self::assertEquals($value2, $received, 'Value not overwritten by second set');
112
113                 $this->instance->set('value2', $value1);
114                 $received2 = $this->instance->get('value2');
115                 self::assertEquals($value2, $received, 'Value changed while setting other variable');
116                 self::assertEquals($value1, $received2, 'Second value not equal to original');
117
118                 self::assertNull($this->instance->get('not_set'), 'Unset value not equal to null');
119
120                 self::assertTrue($this->instance->delete('value1'));
121                 self::assertNull($this->instance->get('value1'));
122         }
123
124         /**
125          * @small
126          * @dataProvider dataSimple
127          *
128          * @param mixed $value1 a first
129          * @param mixed $value2 a second
130          * @param mixed $value3 a third
131          * @param mixed $value4 a fourth
132          */
133         public function testClear($value1, $value2, $value3, $value4)
134         {
135                 $this->instance->set('1_value1', $value1);
136                 $this->instance->set('1_value2', $value2);
137                 $this->instance->set('2_value1', $value3);
138                 $this->instance->set('3_value1', $value4);
139
140                 self::assertEquals([
141                         '1_value1' => $value1,
142                         '1_value2' => $value2,
143                         '2_value1' => $value3,
144                         '3_value1' => $value4,
145                 ], [
146                         '1_value1' => $this->instance->get('1_value1'),
147                         '1_value2' => $this->instance->get('1_value2'),
148                         '2_value1' => $this->instance->get('2_value1'),
149                         '3_value1' => $this->instance->get('3_value1'),
150                 ]);
151
152                 self::assertTrue($this->instance->clear());
153
154                 self::assertEquals([
155                         '1_value1' => $value1,
156                         '1_value2' => $value2,
157                         '2_value1' => $value3,
158                         '3_value1' => $value4,
159                 ], [
160                         '1_value1' => $this->instance->get('1_value1'),
161                         '1_value2' => $this->instance->get('1_value2'),
162                         '2_value1' => $this->instance->get('2_value1'),
163                         '3_value1' => $this->instance->get('3_value1'),
164                 ]);
165
166                 self::assertTrue($this->instance->clear(false));
167
168                 self::assertEquals([
169                         '1_value1' => null,
170                         '1_value2' => null,
171                         '2_value3' => null,
172                         '3_value4' => null,
173                 ], [
174                         '1_value1' => $this->instance->get('1_value1'),
175                         '1_value2' => $this->instance->get('1_value2'),
176                         '2_value3' => $this->instance->get('2_value3'),
177                         '3_value4' => $this->instance->get('3_value4'),
178                 ]);
179         }
180
181         /**
182          * @medium
183          */
184         public function testTTL()
185         {
186                 static::markTestSkipped('taking too much time without mocking');
187
188                 self::assertNull($this->instance->get('value1'));
189
190                 $value = 'foobar';
191                 $this->instance->set('value1', $value, 1);
192                 $received = $this->instance->get('value1');
193                 self::assertEquals($value, $received, 'Value received from cache not equal to the original');
194
195                 sleep(2);
196
197                 self::assertNull($this->instance->get('value1'));
198         }
199
200         /**
201          * @small
202          *
203          * @param mixed $data the data to store in the cache
204          *
205          * @dataProvider dataTypesInCache
206          */
207         public function testDifferentTypesInCache($data)
208         {
209                 $this->instance->set('val', $data);
210                 $received = $this->instance->get('val');
211                 self::assertEquals($data, $received, 'Value type changed from ' . gettype($data) . ' to ' . gettype($received));
212         }
213
214         /**
215          * @small
216          *
217          * @param mixed $value1 a first
218          * @param mixed $value2 a second
219          * @param mixed $value3 a third
220          *
221          * @dataProvider dataSimple
222          */
223         public function testGetAllKeys($value1, $value2, $value3)
224         {
225                 self::assertTrue($this->instance->set('value1', $value1));
226                 self::assertTrue($this->instance->set('value2', $value2));
227                 self::assertTrue($this->instance->set('test_value3', $value3));
228
229                 $list = $this->instance->getAllKeys();
230
231                 self::assertContains('value1', $list);
232                 self::assertContains('value2', $list);
233                 self::assertContains('test_value3', $list);
234
235                 $list = $this->instance->getAllKeys('test');
236
237                 self::assertContains('test_value3', $list);
238                 self::assertNotContains('value1', $list);
239                 self::assertNotContains('value2', $list);
240         }
241
242         /**
243          * @small
244          */
245         public function testSpaceInKey()
246         {
247                 self::assertTrue($this->instance->set('key space', 'value'));
248                 self::assertEquals('value', $this->instance->get('key space'));
249         }
250
251         public function testGetName()
252         {
253                 if (defined(get_class($this->instance) . '::NAME')) {
254                         self::assertEquals($this->instance::NAME, $this->instance->getName());
255                 } else {
256                         self::expectNotToPerformAssertions();
257                 }
258         }
259 }