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