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