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