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