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