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