1 | <?php |
||
2 | |||
3 | use PHPUnit\Framework\TestCase; |
||
4 | use Silviooosilva\CacheerPhp\Cacheer; |
||
5 | use Silviooosilva\CacheerPhp\Helpers\EnvHelper; |
||
6 | use Silviooosilva\CacheerPhp\Utils\CacheDriver; |
||
7 | |||
8 | class FileCacheStoreTest extends TestCase |
||
9 | { |
||
10 | private $cache; |
||
11 | private $cacheDir; |
||
12 | |||
13 | protected function setUp(): void |
||
14 | { |
||
15 | $this->cacheDir = __DIR__ . '/cache'; |
||
16 | if (!file_exists($this->cacheDir) || !is_dir($this->cacheDir)) { |
||
17 | mkdir($this->cacheDir, 0755, true); |
||
18 | } |
||
19 | |||
20 | $options = [ |
||
21 | 'cacheDir' => $this->cacheDir, |
||
22 | ]; |
||
23 | |||
24 | $this->cache = new Cacheer($options); |
||
25 | } |
||
26 | |||
27 | protected function tearDown(): void |
||
28 | { |
||
29 | $this->cache->flushCache(); |
||
30 | } |
||
31 | |||
32 | public function testPutCache() |
||
33 | { |
||
34 | $cacheKey = 'test_key'; |
||
35 | $data = 'test_data'; |
||
36 | |||
37 | $this->cache->putCache($cacheKey, $data); |
||
38 | $this->assertTrue($this->cache->isSuccess()); |
||
39 | $this->assertEquals('Cache file created successfully', $this->cache->getMessage()); |
||
40 | |||
41 | $cacheFile = $this->cacheDir . '/' . md5($cacheKey) . '.cache'; |
||
42 | $this->assertFileExists($cacheFile); |
||
43 | $this->assertEquals($data, $this->cache->getCache($cacheKey)); |
||
44 | } |
||
45 | |||
46 | public function testGetCache() |
||
47 | { |
||
48 | $cacheKey = 'test_key'; |
||
49 | $data = 'test_data'; |
||
50 | |||
51 | $this->cache->putCache($cacheKey, $data); |
||
52 | |||
53 | $cachedData = $this->cache->getCache($cacheKey); |
||
54 | $this->assertTrue($this->cache->isSuccess()); |
||
55 | $this->assertEquals($data, $cachedData); |
||
56 | |||
57 | // Recuperar cache fora do perÃodo de expiração |
||
58 | sleep(2); |
||
59 | $cachedData = $this->cache->getCache($cacheKey, '', '2 seconds'); |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
60 | $this->assertFalse($this->cache->isSuccess()); |
||
61 | $this->assertEquals('cacheFile not found, does not exists or expired', $this->cache->getMessage()); |
||
62 | } |
||
63 | |||
64 | public function testClearCache() |
||
65 | { |
||
66 | $cacheKey = 'test_key'; |
||
67 | $data = 'test_data'; |
||
68 | |||
69 | $this->cache->putCache($cacheKey, $data); |
||
70 | $this->cache->clearCache($cacheKey); |
||
71 | |||
72 | $this->assertTrue($this->cache->isSuccess()); |
||
73 | $this->assertEquals('Cache file deleted successfully!', $this->cache->getMessage()); |
||
74 | |||
75 | $cacheFile = $this->cacheDir . '/' . md5($cacheKey) . '.cache'; |
||
76 | $this->assertFileDoesNotExist($cacheFile); |
||
77 | } |
||
78 | |||
79 | public function testFlushCache() |
||
80 | { |
||
81 | $key1 = 'test_key1'; |
||
82 | $data1 = 'test_data1'; |
||
83 | |||
84 | $key2 = 'test_key2'; |
||
85 | $data2 = 'test_data2'; |
||
86 | |||
87 | $this->cache->putCache($key1, $data1); |
||
88 | $this->cache->putCache($key2, $data2); |
||
89 | $this->cache->flushCache(); |
||
90 | |||
91 | $cacheFile1 = $this->cacheDir . '/' . md5($key1) . '.cache'; |
||
92 | $cacheFile2 = $this->cacheDir . '/' . md5($key2) . '.cache'; |
||
93 | |||
94 | $this->assertFileDoesNotExist($cacheFile1); |
||
95 | $this->assertFileDoesNotExist($cacheFile2); |
||
96 | } |
||
97 | |||
98 | public function testAutoFlush() |
||
99 | { |
||
100 | $options = [ |
||
101 | 'cacheDir' => $this->cacheDir, |
||
102 | 'flushAfter' => '10 seconds' |
||
103 | ]; |
||
104 | |||
105 | $this->cache = new Cacheer($options); |
||
106 | $this->cache->putCache('test_key', 'test_data'); |
||
107 | |||
108 | // Verifica se o cache foi criado com sucesso |
||
109 | $this->assertEquals('test_data', $this->cache->getCache('test_key')); |
||
110 | $this->assertTrue($this->cache->isSuccess()); |
||
111 | |||
112 | // Espera 11 segundos para o cache ser limpo automaticamente |
||
113 | sleep(11); |
||
114 | |||
115 | $this->cache = new Cacheer($options); |
||
116 | |||
117 | // Verifica se o cache foi limpo automaticamente |
||
118 | $cachedData = $this->cache->getCache('test_key'); |
||
0 ignored issues
–
show
|
|||
119 | $this->assertFalse($this->cache->isSuccess()); |
||
120 | $this->assertEquals('cacheFile not found, does not exists or expired', $this->cache->getMessage()); |
||
121 | } |
||
122 | |||
123 | public function testAppendCache() |
||
124 | { |
||
125 | $cacheKey = 'test_append_key'; |
||
126 | $initialData = ['initial' => 'data']; |
||
127 | $additionalData = ['new' => 'data']; |
||
128 | $expectedData = array_merge($initialData, $additionalData); |
||
129 | |||
130 | // Armazena os dados iniciais no cache |
||
131 | $this->cache->putCache($cacheKey, $initialData); |
||
132 | $this->assertTrue($this->cache->isSuccess()); |
||
133 | |||
134 | // Adiciona novos dados ao cache existente |
||
135 | $this->cache->appendCache($cacheKey, $additionalData); |
||
136 | $this->assertTrue($this->cache->isSuccess()); |
||
137 | |||
138 | // Verifica se os dados no cache são os esperados |
||
139 | $cachedData = $this->cache->getCache($cacheKey); |
||
140 | $this->assertEquals($expectedData, $cachedData); |
||
141 | |||
142 | // Testa adicionar dados como string |
||
143 | $additionalData = ['string_data' => 'string data']; |
||
144 | $expectedData = array_merge($expectedData, $additionalData); |
||
145 | $this->cache->appendCache($cacheKey, $additionalData); |
||
146 | $cachedData = $this->cache->getCache($cacheKey); |
||
147 | $this->assertEquals($expectedData, $cachedData); |
||
148 | } |
||
149 | |||
150 | public function testAppendCacheFileNotExists() |
||
151 | { |
||
152 | $cacheKey = 'non_existing_key'; |
||
153 | $data = ['data']; |
||
154 | |||
155 | // Tenta adicionar dados a um arquivo de cache que não existe |
||
156 | $this->cache->appendCache($cacheKey, $data); |
||
157 | $this->assertFalse($this->cache->isSuccess()); |
||
158 | $this->assertEquals('cacheFile not found, does not exists or expired', $this->cache->getMessage()); |
||
159 | } |
||
160 | |||
161 | public function testAppendCacheWithNamespace() |
||
162 | { |
||
163 | $cacheKey = 'test_append_key_ns'; |
||
164 | $namespace = 'test_namespace'; |
||
165 | |||
166 | $initialData = ['initial' => 'data']; |
||
167 | $additionalData = ['new' => 'data']; |
||
168 | |||
169 | $expectedData = array_merge($initialData, $additionalData); |
||
170 | |||
171 | // Armazena os dados iniciais no cache com namespace |
||
172 | $this->cache->putCache($cacheKey, $initialData, $namespace); |
||
173 | $this->assertTrue($this->cache->isSuccess()); |
||
174 | |||
175 | // Adiciona novos dados ao cache existente com namespace |
||
176 | $this->cache->appendCache($cacheKey, $additionalData, $namespace); |
||
177 | $this->assertTrue($this->cache->isSuccess()); |
||
178 | |||
179 | // Verifica se os dados no cache são os esperados |
||
180 | $cachedData = $this->cache->getCache($cacheKey, $namespace); |
||
181 | $this->assertEquals($expectedData, $cachedData); |
||
182 | } |
||
183 | |||
184 | public function testDataOutputShouldBeOfTypeJson() |
||
185 | { |
||
186 | $options = [ |
||
187 | 'cacheDir' => $this->cacheDir |
||
188 | ]; |
||
189 | $this->cache = new Cacheer($options, true); |
||
190 | |||
191 | $cacheKey = "key_json"; |
||
192 | $cacheData = "data_json"; |
||
193 | |||
194 | $this->cache->putCache($cacheKey, $cacheData); |
||
195 | $this->assertTrue($this->cache->isSuccess()); |
||
196 | |||
197 | $cacheOutput = $this->cache->getCache($cacheKey)->toJson(); |
||
198 | $this->assertTrue($this->cache->isSuccess()); |
||
199 | $this->assertJson($cacheOutput); |
||
200 | } |
||
201 | |||
202 | public function testDataOutputShouldBeOfTypeArray() |
||
203 | { |
||
204 | $options = [ |
||
205 | 'cacheDir' => $this->cacheDir |
||
206 | ]; |
||
207 | $this->cache = new Cacheer($options, true); |
||
208 | |||
209 | $cacheKey = "key_array"; |
||
210 | $cacheData = "data_array"; |
||
211 | |||
212 | $this->cache->putCache($cacheKey, $cacheData); |
||
213 | $this->assertTrue($this->cache->isSuccess()); |
||
214 | |||
215 | $cacheOutput = $this->cache->getCache($cacheKey)->toArray(); |
||
216 | $this->assertTrue($this->cache->isSuccess()); |
||
217 | $this->assertIsArray($cacheOutput); |
||
218 | } |
||
219 | |||
220 | public function testDataOutputShouldBeOfTypeObject() |
||
221 | { |
||
222 | $options = [ |
||
223 | 'cacheDir' => $this->cacheDir |
||
224 | ]; |
||
225 | $this->cache = new Cacheer($options, true); |
||
226 | |||
227 | $cacheKey = "key_object"; |
||
228 | $cacheData = ["id" => 123]; |
||
229 | |||
230 | $this->cache->putCache($cacheKey, $cacheData); |
||
231 | $this->assertTrue($this->cache->isSuccess()); |
||
232 | |||
233 | $cacheOutput = $this->cache->getCache($cacheKey)->toObject(); |
||
234 | $this->assertTrue($this->cache->isSuccess()); |
||
235 | $this->assertIsObject($cacheOutput); |
||
236 | } |
||
237 | |||
238 | public function testPutMany() |
||
239 | { |
||
240 | $cacheer = new Cacheer(['cacheDir' => __DIR__ . '/cache']); |
||
241 | $items = [ |
||
242 | [ |
||
243 | 'cacheKey' => 'user_1_profile', |
||
244 | 'cacheData' => ['name' => 'John Doe', 'email' => '[email protected]'] |
||
245 | ], |
||
246 | [ |
||
247 | 'cacheKey' => 'user_2_profile', |
||
248 | 'cacheData' => ['name' => 'Jane Doe', 'email' => '[email protected]'] |
||
249 | ], |
||
250 | ]; |
||
251 | |||
252 | $cacheer->putMany($items); |
||
253 | |||
254 | foreach ($items as $item) { |
||
255 | $this->assertEquals($item['cacheData'], $cacheer->getCache($item['cacheKey'])); |
||
256 | } |
||
257 | } |
||
258 | |||
259 | public function test_remember_saves_and_recover_values() |
||
260 | { |
||
261 | $this->cache->flushCache(); |
||
262 | |||
263 | $value = $this->cache->remember('remember_test_key', 60, function () { |
||
264 | return 'valor_teste'; |
||
265 | }); |
||
266 | |||
267 | $this->assertEquals('valor_teste', $value); |
||
268 | |||
269 | $cachedValue = $this->cache->remember('remember_test_key', 60, function (){ |
||
270 | return 'novo_valor'; |
||
271 | }); |
||
272 | |||
273 | |||
274 | $this->assertEquals('valor_teste', $cachedValue); |
||
275 | } |
||
276 | |||
277 | public function test_remember_forever_saves_value_indefinitely() |
||
278 | { |
||
279 | $this->cache->flushCache(); |
||
280 | |||
281 | $value = $this->cache->rememberForever('remember_forever_key', function () { |
||
282 | return 'valor_eterno'; |
||
283 | }); |
||
284 | $this->assertEquals('valor_eterno', $value); |
||
285 | |||
286 | $cachedValue = $this->cache->rememberForever('remember_forever_key', function () { |
||
287 | return 'novo_valor'; |
||
288 | }); |
||
289 | |||
290 | $this->assertEquals('valor_eterno', $cachedValue); |
||
291 | } |
||
292 | |||
293 | public function test_get_and_forget() |
||
294 | { |
||
295 | $cacheKey = 'cache_key_test'; |
||
296 | $this->cache->putCache($cacheKey, 10); |
||
297 | |||
298 | $this->assertTrue($this->cache->isSuccess()); |
||
299 | |||
300 | $cacheData = $this->cache->getAndForget($cacheKey); |
||
301 | |||
302 | $this->assertTrue($this->cache->isSuccess()); |
||
303 | $this->assertEquals(10, $cacheData); |
||
304 | |||
305 | $oldCacheData = $this->cache->getAndForget($cacheKey); |
||
306 | |||
307 | $this->assertNull($oldCacheData); |
||
308 | $this->assertFalse($this->cache->isSuccess()); |
||
309 | |||
310 | $noCacheData = $this->cache->getAndForget('non_existent_cache_key'); |
||
311 | $this->assertNull($noCacheData); |
||
312 | } |
||
313 | |||
314 | public function test_store_if_not_present_with_add_function() |
||
315 | { |
||
316 | $existentKey = 'cache_key_test'; |
||
317 | |||
318 | $nonExistentKey = 'non_existent_key'; |
||
319 | |||
320 | $this->cache->putCache($existentKey, 'existent_data'); |
||
321 | |||
322 | $this->assertTrue($this->cache->isSuccess()); |
||
323 | $this->assertEquals('existent_data', $this->cache->getCache($existentKey)); |
||
324 | |||
325 | $addCache = $this->cache->add($existentKey, 100); |
||
326 | |||
327 | $this->assertTrue($addCache); |
||
328 | $this->assertNotEquals(100, 'existent_data'); |
||
329 | |||
330 | $addNonExistentKey = $this->cache->add($nonExistentKey, 'non_existent_data'); |
||
331 | |||
332 | $this->assertFalse($addNonExistentKey); |
||
333 | $this->assertEquals('non_existent_data', $this->cache->getCache($nonExistentKey)); |
||
334 | $this->assertTrue($this->cache->isSuccess()); |
||
335 | |||
336 | } |
||
337 | |||
338 | public function test_increment_function() { |
||
339 | |||
340 | $cacheKey = 'test_increment'; |
||
341 | $cacheData = 2025; |
||
342 | |||
343 | $this->cache->putCache($cacheKey, $cacheData); |
||
344 | |||
345 | $this->assertTrue($this->cache->isSuccess()); |
||
346 | $this->assertEquals($cacheData, $this->cache->getCache($cacheKey)); |
||
347 | $this->assertIsNumeric($this->cache->getCache($cacheKey)); |
||
348 | |||
349 | $increment = $this->cache->increment($cacheKey, 2); |
||
350 | $this->assertTrue($increment); |
||
351 | |||
352 | $this->assertEquals(2027, $this->cache->getCache($cacheKey)); |
||
353 | |||
354 | } |
||
355 | |||
356 | public function test_decrement_function() { |
||
357 | |||
358 | $cacheKey = 'test_decrement'; |
||
359 | $cacheData = 2027; |
||
360 | |||
361 | $this->cache->putCache($cacheKey, $cacheData); |
||
362 | |||
363 | $this->assertTrue($this->cache->isSuccess()); |
||
364 | $this->assertEquals($cacheData, $this->cache->getCache($cacheKey)); |
||
365 | $this->assertIsNumeric($this->cache->getCache($cacheKey)); |
||
366 | |||
367 | $increment = $this->cache->decrement($cacheKey, 2); |
||
368 | $this->assertTrue($increment); |
||
369 | |||
370 | $this->assertEquals(2025, $this->cache->getCache($cacheKey)); |
||
371 | |||
372 | } |
||
373 | |||
374 | private function removeDirectoryRecursively($dir) |
||
375 | { |
||
376 | if (!is_dir($dir)) { |
||
377 | return; |
||
378 | } |
||
379 | $items = scandir($dir); |
||
380 | foreach ($items as $item) { |
||
381 | if ($item === '.' || $item === '..') { |
||
382 | continue; |
||
383 | } |
||
384 | $path = $dir . DIRECTORY_SEPARATOR . $item; |
||
385 | if (is_dir($path)) { |
||
386 | $this->removeDirectoryRecursively($path); |
||
387 | } else { |
||
388 | unlink($path); |
||
389 | } |
||
390 | } |
||
391 | rmdir($dir); |
||
392 | } |
||
393 | |||
394 | public function testUseDefaultDriverCreatesCacheDirInProjectRoot() |
||
395 | { |
||
396 | $cacheer = new Cacheer(); |
||
397 | $driver = new CacheDriver($cacheer); |
||
398 | |||
399 | $projectRoot = EnvHelper::getRootPath(); |
||
400 | $expectedCacheDir = $projectRoot . DIRECTORY_SEPARATOR . "CacheerPHP" . DIRECTORY_SEPARATOR . "Cache"; |
||
401 | |||
402 | if (is_dir($expectedCacheDir)) { |
||
403 | $this->removeDirectoryRecursively($expectedCacheDir); |
||
404 | } |
||
405 | |||
406 | $driver->useDefaultDriver(); |
||
407 | |||
408 | $this->assertDirectoryExists($expectedCacheDir); |
||
409 | |||
410 | if (is_dir($expectedCacheDir)) { |
||
411 | $this->removeDirectoryRecursively($expectedCacheDir); |
||
412 | } |
||
413 | } |
||
414 | |||
415 | public function testPutCacheWithNamespace() |
||
416 | { |
||
417 | $cacheKey = 'namespace_key'; |
||
418 | $data = 'namespace_data'; |
||
419 | $namespace = 'my_namespace'; |
||
420 | |||
421 | $this->cache->putCache($cacheKey, $data, $namespace); |
||
422 | $this->assertTrue($this->cache->isSuccess()); |
||
423 | |||
424 | $cachedData = $this->cache->getCache($cacheKey, $namespace); |
||
425 | $this->assertEquals($data, $cachedData); |
||
426 | } |
||
427 | |||
428 | public function testClearCacheWithNamespace() |
||
429 | { |
||
430 | $cacheKey = 'namespace_key_clear'; |
||
431 | $data = 'namespace_data_clear'; |
||
432 | $namespace = 'clear_namespace'; |
||
433 | |||
434 | $this->cache->putCache($cacheKey, $data, $namespace); |
||
435 | $this->assertTrue($this->cache->isSuccess()); |
||
436 | |||
437 | $this->cache->clearCache($cacheKey, $namespace); |
||
438 | $this->assertTrue($this->cache->isSuccess()); |
||
439 | |||
440 | $cachedData = $this->cache->getCache($cacheKey, $namespace); |
||
441 | $this->assertFalse($this->cache->isSuccess()); |
||
442 | $this->assertNull($cachedData); |
||
443 | } |
||
444 | |||
445 | public function testFlushCacheRemovesNamespacedFiles() |
||
446 | { |
||
447 | $cacheKey = 'ns_flush_key'; |
||
448 | $data = 'ns_flush_data'; |
||
449 | $namespace = 'flush_namespace'; |
||
450 | |||
451 | $this->cache->putCache($cacheKey, $data, $namespace); |
||
452 | $this->assertTrue($this->cache->isSuccess()); |
||
453 | |||
454 | $this->cache->flushCache(); |
||
455 | |||
456 | $cachedData = $this->cache->getCache($cacheKey, $namespace); |
||
457 | $this->assertFalse($this->cache->isSuccess()); |
||
458 | $this->assertNull($cachedData); |
||
459 | } |
||
460 | |||
461 | public function testAppendCacheWithDifferentTypes() |
||
462 | { |
||
463 | $cacheKey = 'append_type_key'; |
||
464 | $initialData = ['a' => 1]; |
||
465 | $additionalData = ['b' => 2]; |
||
466 | $expectedData = ['a' => 1, 'b' => 2]; |
||
467 | |||
468 | $this->cache->putCache($cacheKey, $initialData); |
||
469 | $this->cache->appendCache($cacheKey, $additionalData); |
||
470 | $this->assertEquals($expectedData, $this->cache->getCache($cacheKey)); |
||
471 | |||
472 | $this->cache->appendCache($cacheKey, ['c' => 'string']); |
||
473 | $expectedData['c'] = 'string'; |
||
474 | $this->assertEquals($expectedData, $this->cache->getCache($cacheKey)); |
||
475 | } |
||
476 | |||
477 | } |
||
478 |