]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/Cache/CacheTest.php
Merge pull request #6598 from annando/worker-index
[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                 $configMock = \Mockery::mock('Friendica\Core\Config\ConfigCache');
73                 $this->mockApp($this->root, $configMock);
74                 $this->app
75                         ->shouldReceive('getHostname')
76                         ->andReturn('friendica.local');
77
78                 $this->mockUtcNow($this->startTime);
79
80                 parent::setUp();
81
82                 $this->instance = $this->getInstance();
83
84                 // Default config
85                 $this->mockConfigGet('config', 'hostname', 'localhost');
86                 $this->mockConfigGet('system', 'throttle_limit_day', 100);
87                 $this->mockConfigGet('system', 'throttle_limit_week', 100);
88                 $this->mockConfigGet('system', 'throttle_limit_month', 100);
89                 $this->mockConfigGet('system', 'theme', 'system_theme');
90
91                 $this->instance->clear(false);
92         }
93
94         /**
95          * @small
96          * @dataProvider dataSimple
97          * @param mixed $value1 a first
98          * @param mixed $value2 a second
99          */
100         function testSimple($value1, $value2) {
101                 $this->assertNull($this->instance->get('value1'));
102
103                 $this->instance->set('value1', $value1);
104                 $received = $this->instance->get('value1');
105                 $this->assertEquals($value1, $received, 'Value received from cache not equal to the original');
106
107                 $this->instance->set('value1', $value2);
108                 $received = $this->instance->get('value1');
109                 $this->assertEquals($value2, $received, 'Value not overwritten by second set');
110
111                 $this->instance->set('value2', $value1);
112                 $received2 = $this->instance->get('value2');
113                 $this->assertEquals($value2, $received, 'Value changed while setting other variable');
114                 $this->assertEquals($value1, $received2, 'Second value not equal to original');
115
116                 $this->assertNull($this->instance->get('not_set'), 'Unset value not equal to null');
117
118                 $this->assertTrue($this->instance->delete('value1'));
119                 $this->assertNull($this->instance->get('value1'));
120         }
121
122         /**
123          * @small
124          * @dataProvider dataSimple
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                 $value = 'ipsum lorum';
132                 $this->instance->set('1_value1', $value1);
133                 $this->instance->set('1_value2', $value2);
134                 $this->instance->set('2_value1', $value3);
135                 $this->instance->set('3_value1', $value4);
136
137                 $this->assertEquals([
138                         '1_value1' => $value1,
139                         '1_value2' => $value2,
140                         '2_value1' => $value3,
141                         '3_value1' => $value4,
142                 ], [
143                         '1_value1' => $this->instance->get('1_value1'),
144                         '1_value2' => $this->instance->get('1_value2'),
145                         '2_value1' => $this->instance->get('2_value1'),
146                         '3_value1' => $this->instance->get('3_value1'),
147                 ]);
148
149                 $this->assertTrue($this->instance->clear());
150
151                 $this->assertEquals([
152                         '1_value1' => $value1,
153                         '1_value2' => $value2,
154                         '2_value1' => $value3,
155                         '3_value1' => $value4,
156                 ], [
157                         '1_value1' => $this->instance->get('1_value1'),
158                         '1_value2' => $this->instance->get('1_value2'),
159                         '2_value1' => $this->instance->get('2_value1'),
160                         '3_value1' => $this->instance->get('3_value1'),
161                 ]);
162
163                 $this->assertTrue($this->instance->clear(false));
164
165                 $this->assertEquals([
166                         '1_value1' => null,
167                         '1_value2' => null,
168                         '2_value3' => null,
169                         '3_value4' => null,
170                 ], [
171                         '1_value1' => $this->instance->get('1_value1'),
172                         '1_value2' => $this->instance->get('1_value2'),
173                         '2_value3' => $this->instance->get('2_value3'),
174                         '3_value4' => $this->instance->get('3_value4'),
175                 ]);
176         }
177
178         /**
179          * @medium
180          */
181         function testTTL() {
182                 $this->markTestSkipped('taking too much time without mocking');
183
184                 $this->assertNull($this->instance->get('value1'));
185
186                 $value = 'foobar';
187                 $this->instance->set('value1', $value, 1);
188                 $received = $this->instance->get('value1');
189                 $this->assertEquals($value, $received, 'Value received from cache not equal to the original');
190
191                 sleep(2);
192
193                 $this->assertNull($this->instance->get('value1'));
194         }
195
196         /**
197          * @small
198          * @param $data mixed the data to store in the cache
199          * @dataProvider dataTypesInCache
200          */
201         function testDifferentTypesInCache($data) {
202                 $this->instance->set('val', $data);
203                 $received = $this->instance->get('val');
204                 $this->assertEquals($data, $received, 'Value type changed from ' . gettype($data) . ' to ' . gettype($received));
205         }
206
207         /**
208          * @small
209          * @param mixed $value1 a first
210          * @param mixed $value2 a second
211          * @param mixed $value3 a third
212          * @dataProvider dataSimple
213          */
214         public function testGetAllKeys($value1, $value2, $value3) {
215                 if ($this->cache instanceof MemcachedCacheDriver) {
216                         $this->markTestSkipped('Memcached doesn\'t support getAllKeys anymore');
217                 }
218
219                 $this->assertTrue($this->instance->set('value1', $value1));
220                 $this->assertTrue($this->instance->set('value2', $value2));
221                 $this->assertTrue($this->instance->set('test_value3', $value3));
222
223                 $list = $this->instance->getAllKeys();
224
225                 $this->assertContains('value1', $list);
226                 $this->assertContains('value2', $list);
227                 $this->assertContains('test_value3', $list);
228
229                 $list = $this->instance->getAllKeys('test');
230
231                 $this->assertContains('test_value3', $list);
232         }
233 }