]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/Cache/CacheTest.php
Merge pull request #6582 from MrPetovan/bug/6570-fix-diaspora-hashtags
[friendica.git] / tests / src / Core / Cache / CacheTest.php
1 <?php
2
3 namespace Friendica\Test\src\Core\Cache;
4
5 use Friendica\Core\Cache\MemcachedCacheDriver;
6 use Friendica\Test\MockedTest;
7 use Friendica\Test\Util\AppMockTrait;
8 use Friendica\Test\Util\DateTimeFormatMockTrait;
9 use Friendica\Test\Util\VFSTrait;
10 use Friendica\Util\PidFile;
11
12 abstract class CacheTest extends MockedTest
13 {
14         use VFSTrait;
15         use AppMockTrait;
16         use DateTimeFormatMockTrait;
17
18         /**
19          * @var int Start time of the mock (used for time operations)
20          */
21         protected $startTime = 1417011228;
22
23         /**
24          * @var \Friendica\Core\Cache\ICacheDriver
25          */
26         protected $instance;
27
28         /**
29          * @var \Friendica\Core\Cache\IMemoryCacheDriver
30          */
31         protected $cache;
32
33         /**
34          * Dataset for test setting different types in the cache
35          * @return array
36          */
37         public function dataTypesInCache()
38         {
39                 return [
40                         'string'    => ['data' => 'foobar'],
41                         'integer'   => ['data' => 1],
42                         'boolTrue'  => ['data' => true],
43                         'boolFalse' => ['data' => false],
44                         'float'     => ['data' => 4.6634234],
45                         'array'     => ['data' => ['1', '2', '3', '4', '5']],
46                         'object'    => ['data' => new PidFile()],
47                         'null'      => ['data' => null],
48                 ];
49         }
50
51         /**
52          * Dataset for simple value sets/gets
53          * @return array
54          */
55         public function dataSimple()
56         {
57                 return [
58                         'string' => [
59                                 'value1' => 'foobar',
60                                 'value2' => 'ipsum lorum',
61                                 'value3' => 'test',
62                                 'value4' => 'lasttest',
63                         ],
64                 ];
65         }
66
67         abstract protected function getInstance();
68
69         protected function setUp()
70         {
71                 $this->setUpVfsDir();
72                 $this->mockApp($this->root);
73                 $this->app
74                         ->shouldReceive('getHostname')
75                         ->andReturn('friendica.local');
76
77                 $this->mockUtcNow($this->startTime);
78
79                 parent::setUp();
80
81                 $this->instance = $this->getInstance();
82
83                 // Default config
84                 $this->mockConfigGet('config', 'hostname', 'localhost');
85                 $this->mockConfigGet('system', 'throttle_limit_day', 100);
86                 $this->mockConfigGet('system', 'throttle_limit_week', 100);
87                 $this->mockConfigGet('system', 'throttle_limit_month', 100);
88                 $this->mockConfigGet('system', 'theme', 'system_theme');
89
90                 $this->instance->clear(false);
91         }
92
93         /**
94          * @small
95          * @dataProvider dataSimple
96          * @param mixed $value1 a first
97          * @param mixed $value2 a second
98          */
99         function testSimple($value1, $value2) {
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          * @param mixed $value1 a first
125          * @param mixed $value2 a second
126          * @param mixed $value3 a third
127          * @param mixed $value4 a fourth
128          */
129         function testClear($value1, $value2, $value3, $value4) {
130                 $value = 'ipsum lorum';
131                 $this->instance->set('1_value1', $value1);
132                 $this->instance->set('1_value2', $value2);
133                 $this->instance->set('2_value1', $value3);
134                 $this->instance->set('3_value1', $value4);
135
136                 $this->assertEquals([
137                         '1_value1' => $value1,
138                         '1_value2' => $value2,
139                         '2_value1' => $value3,
140                         '3_value1' => $value4,
141                 ], [
142                         '1_value1' => $this->instance->get('1_value1'),
143                         '1_value2' => $this->instance->get('1_value2'),
144                         '2_value1' => $this->instance->get('2_value1'),
145                         '3_value1' => $this->instance->get('3_value1'),
146                 ]);
147
148                 $this->assertTrue($this->instance->clear());
149
150                 $this->assertEquals([
151                         '1_value1' => $value1,
152                         '1_value2' => $value2,
153                         '2_value1' => $value3,
154                         '3_value1' => $value4,
155                 ], [
156                         '1_value1' => $this->instance->get('1_value1'),
157                         '1_value2' => $this->instance->get('1_value2'),
158                         '2_value1' => $this->instance->get('2_value1'),
159                         '3_value1' => $this->instance->get('3_value1'),
160                 ]);
161
162                 $this->assertTrue($this->instance->clear(false));
163
164                 $this->assertEquals([
165                         '1_value1' => null,
166                         '1_value2' => null,
167                         '2_value3' => null,
168                         '3_value4' => null,
169                 ], [
170                         '1_value1' => $this->instance->get('1_value1'),
171                         '1_value2' => $this->instance->get('1_value2'),
172                         '2_value3' => $this->instance->get('2_value3'),
173                         '3_value4' => $this->instance->get('3_value4'),
174                 ]);
175         }
176
177         /**
178          * @medium
179          */
180         function testTTL() {
181                 $this->markTestSkipped('taking too much time without mocking');
182
183                 $this->assertNull($this->instance->get('value1'));
184
185                 $value = 'foobar';
186                 $this->instance->set('value1', $value, 1);
187                 $received = $this->instance->get('value1');
188                 $this->assertEquals($value, $received, 'Value received from cache not equal to the original');
189
190                 sleep(2);
191
192                 $this->assertNull($this->instance->get('value1'));
193         }
194
195         /**
196          * @small
197          * @param $data mixed the data to store in the cache
198          * @dataProvider dataTypesInCache
199          */
200         function testDifferentTypesInCache($data) {
201                 $this->instance->set('val', $data);
202                 $received = $this->instance->get('val');
203                 $this->assertEquals($data, $received, 'Value type changed from ' . gettype($data) . ' to ' . gettype($received));
204         }
205
206         /**
207          * @small
208          * @param mixed $value1 a first
209          * @param mixed $value2 a second
210          * @param mixed $value3 a third
211          * @dataProvider dataSimple
212          */
213         public function testGetAllKeys($value1, $value2, $value3) {
214                 if ($this->cache instanceof MemcachedCacheDriver) {
215                         $this->markTestSkipped('Memcached doesn\'t support getAllKeys anymore');
216                 }
217
218                 $this->assertTrue($this->instance->set('value1', $value1));
219                 $this->assertTrue($this->instance->set('value2', $value2));
220                 $this->assertTrue($this->instance->set('test_value3', $value3));
221
222                 $list = $this->instance->getAllKeys();
223
224                 $this->assertContains('value1', $list);
225                 $this->assertContains('value2', $list);
226                 $this->assertContains('test_value3', $list);
227
228                 $list = $this->instance->getAllKeys('test');
229
230                 $this->assertContains('test_value3', $list);
231         }
232 }