1 | <?php |
||||
2 | |||||
3 | use PHPUnit\Framework\TestCase; |
||||
4 | use Silviooosilva\CacheerPhp\Cacheer; |
||||
5 | use Silviooosilva\CacheerPhp\CacheStore\RedisCacheStore; |
||||
6 | |||||
7 | class RedisCacheStoreTest extends TestCase |
||||
8 | { |
||||
9 | |||||
10 | /** @var Cacheer */ |
||||
11 | private $cache; |
||||
12 | |||||
13 | protected function setUp(): void |
||||
14 | { |
||||
15 | $this->cache = new Cacheer(); |
||||
16 | $this->cache->setDriver()->useRedisDriver(); |
||||
17 | } |
||||
18 | |||||
19 | protected function tearDown(): void |
||||
20 | { |
||||
21 | $this->cache->flushCache(); |
||||
22 | } |
||||
23 | |||||
24 | public function testUsingRedisDriverSetsProperInstance() |
||||
25 | { |
||||
26 | $this->assertInstanceOf(RedisCacheStore::class, $this->cache->cacheStore); |
||||
27 | } |
||||
28 | |||||
29 | public function testPutCacheInRedis() |
||||
30 | { |
||||
31 | $cacheKey = 'test_key'; |
||||
32 | $cacheData = ['name' => 'SÃlvio Silva', 'role' => 'Developer']; |
||||
33 | |||||
34 | $this->cache->putCache($cacheKey, $cacheData); |
||||
35 | |||||
36 | $this->assertEquals("Cache stored successfully", $this->cache->getMessage()); |
||||
37 | $this->assertNotEmpty($this->cache->getCache($cacheKey)); |
||||
38 | $this->assertEquals($cacheData, $this->cache->getCache($cacheKey)); |
||||
39 | |||||
40 | } |
||||
41 | |||||
42 | public function testGetCacheFromRedis() |
||||
43 | { |
||||
44 | $cacheKey = 'test_key'; |
||||
45 | $cacheData = ['name' => 'SÃlvio Silva', 'role' => 'Developer']; |
||||
46 | |||||
47 | $this->cache->putCache($cacheKey, $cacheData); |
||||
48 | |||||
49 | $this->assertEquals("Cache stored successfully", $this->cache->getMessage()); |
||||
50 | |||||
51 | $data = $this->cache->getCache($cacheKey); |
||||
52 | $this->assertNotEmpty($data); |
||||
53 | $this->assertEquals($cacheData, $data); |
||||
54 | } |
||||
55 | |||||
56 | public function testExpiredCacheInRedis() |
||||
57 | { |
||||
58 | $cacheKey = 'expired_key'; |
||||
59 | $cacheData = ['name' => 'Expired User', 'email' => '[email protected]']; |
||||
60 | |||||
61 | $this->cache->putCache($cacheKey, $cacheData, '', 1); |
||||
62 | sleep(3); |
||||
63 | |||||
64 | $this->assertEquals("Cache stored successfully", $this->cache->getMessage()); |
||||
65 | $this->assertEmpty($this->cache->getCache($cacheKey)); |
||||
66 | $this->assertFalse($this->cache->isSuccess()); |
||||
67 | } |
||||
68 | |||||
69 | public function testOverwriteExistingCacheInRedis() |
||||
70 | { |
||||
71 | $cacheKey = 'overwrite_key'; |
||||
72 | $initialCacheData = ['name' => 'Initial Data', 'email' => '[email protected]']; |
||||
73 | $newCacheData = ['name' => 'New Data', 'email' => '[email protected]']; |
||||
74 | |||||
75 | $this->cache->putCache($cacheKey, $initialCacheData); |
||||
76 | $this->assertEquals("Cache stored successfully", $this->cache->getMessage()); |
||||
77 | |||||
78 | $this->cache->appendCache($cacheKey, $newCacheData); |
||||
79 | $this->assertEquals("Cache appended successfully", $this->cache->getMessage()); |
||||
80 | $this->assertEquals($newCacheData, $this->cache->getCache($cacheKey)); |
||||
81 | } |
||||
82 | |||||
83 | public function testPutManyCacheItemsInRedis() |
||||
84 | { |
||||
85 | $items = [ |
||||
86 | [ |
||||
87 | 'cacheKey' => 'user_1_profile', |
||||
88 | 'cacheData' => [ |
||||
89 | ['name' => 'John Doe', 'email' => '[email protected]'], |
||||
90 | ['name' => 'John Doe', 'email' => '[email protected]'], |
||||
91 | ['name' => 'John Doe', 'email' => '[email protected]'], |
||||
92 | ['name' => 'John Doe', 'email' => '[email protected]'] |
||||
93 | ] |
||||
94 | ], |
||||
95 | [ |
||||
96 | 'cacheKey' => 'user_2_profile', |
||||
97 | 'cacheData' => [ |
||||
98 | ['name' => 'Jane Doe', 'email' => '[email protected]'], |
||||
99 | ['name' => 'Jane Doe', 'email' => '[email protected]'], |
||||
100 | ['name' => 'Jane Doe', 'email' => '[email protected]'], |
||||
101 | ['name' => 'Jane Doe', 'email' => '[email protected]'] |
||||
102 | ] |
||||
103 | ] |
||||
104 | ]; |
||||
105 | |||||
106 | $this->cache->putMany($items); |
||||
107 | foreach ($items as $item) { |
||||
108 | $this->assertEquals($item['cacheData'], $this->cache->getCache($item['cacheKey'])); |
||||
109 | } |
||||
110 | } |
||||
111 | |||||
112 | public function testAppendCacheWithNamespaceInRedis() |
||||
113 | { |
||||
114 | $cacheKey = 'test_append_key_ns'; |
||||
115 | $namespace = 'test_namespace'; |
||||
116 | |||||
117 | $initialData = ['initial' => 'data']; |
||||
118 | $additionalData = ['new' => 'data']; |
||||
119 | |||||
120 | $expectedData = array_merge($initialData, $additionalData); |
||||
121 | |||||
122 | |||||
123 | $this->cache->putCache($cacheKey, $initialData, $namespace); |
||||
124 | $this->assertTrue($this->cache->isSuccess()); |
||||
125 | |||||
126 | |||||
127 | $this->cache->appendCache($cacheKey, $additionalData, $namespace); |
||||
128 | $this->assertTrue($this->cache->isSuccess()); |
||||
129 | |||||
130 | |||||
131 | $cachedData = $this->cache->getCache($cacheKey, $namespace); |
||||
132 | $this->assertEquals($expectedData, $cachedData); |
||||
133 | } |
||||
134 | |||||
135 | public function testDataOutputShouldBeOfTypeArray() |
||||
136 | { |
||||
137 | |||||
138 | $this->cache->useFormatter(); |
||||
139 | |||||
140 | $cacheKey = "key_array"; |
||||
141 | $cacheData = "data_array"; |
||||
142 | |||||
143 | $this->cache->putCache($cacheKey, $cacheData); |
||||
144 | $this->assertTrue($this->cache->isSuccess()); |
||||
145 | |||||
146 | $cacheOutput = $this->cache->getCache($cacheKey)->toArray(); |
||||
147 | $this->assertTrue($this->cache->isSuccess()); |
||||
148 | $this->assertIsArray($cacheOutput); |
||||
149 | } |
||||
150 | |||||
151 | public function testDataOutputShouldBeOfTypeObject() |
||||
152 | { |
||||
153 | $this->cache->useFormatter(); |
||||
154 | |||||
155 | $cacheKey = "key_object"; |
||||
156 | $cacheData = ["id" => 123]; |
||||
157 | |||||
158 | $this->cache->putCache($cacheKey, $cacheData); |
||||
159 | $this->assertTrue($this->cache->isSuccess()); |
||||
160 | |||||
161 | $cacheOutput = $this->cache->getCache($cacheKey)->toObject(); |
||||
162 | $this->assertTrue($this->cache->isSuccess()); |
||||
163 | $this->assertIsObject($cacheOutput); |
||||
164 | } |
||||
165 | |||||
166 | public function testDataOutputShouldBeOfTypeJson() |
||||
167 | { |
||||
168 | $this->cache->useFormatter(); |
||||
169 | |||||
170 | $cacheKey = "key_json"; |
||||
171 | $cacheData = "data_json"; |
||||
172 | |||||
173 | $this->cache->putCache($cacheKey, $cacheData); |
||||
174 | $this->assertTrue($this->cache->isSuccess()); |
||||
175 | |||||
176 | $cacheOutput = $this->cache->getCache($cacheKey)->toJson(); |
||||
177 | $this->assertTrue($this->cache->isSuccess()); |
||||
178 | $this->assertJson($cacheOutput); |
||||
179 | } |
||||
180 | |||||
181 | public function testClearCacheDataFromRedis() |
||||
182 | { |
||||
183 | $cacheKey = 'test_key'; |
||||
184 | $data = 'test_data'; |
||||
185 | |||||
186 | $this->cache->putCache($cacheKey, $data); |
||||
187 | $this->assertEquals("Cache stored successfully", $this->cache->getMessage()); |
||||
188 | |||||
189 | $this->cache->clearCache($cacheKey); |
||||
190 | $this->assertTrue($this->cache->isSuccess()); |
||||
191 | $this->assertEquals("Cache cleared successfully", $this->cache->getMessage()); |
||||
192 | |||||
193 | $this->assertEmpty($this->cache->getCache($cacheKey)); |
||||
194 | } |
||||
195 | |||||
196 | public function testFlushCacheDataFromRedis() |
||||
197 | { |
||||
198 | $key1 = 'test_key1'; |
||||
199 | $data1 = 'test_data1'; |
||||
200 | |||||
201 | $key2 = 'test_key2'; |
||||
202 | $data2 = 'test_data2'; |
||||
203 | |||||
204 | $this->cache->putCache($key1, $data1); |
||||
205 | $this->cache->putCache($key2, $data2); |
||||
206 | $this->assertTrue($this->cache->isSuccess()); |
||||
207 | $this->assertTrue($this->cache->isSuccess()); |
||||
208 | |||||
209 | $this->cache->flushCache(); |
||||
210 | |||||
211 | $this->assertTrue($this->cache->isSuccess()); |
||||
212 | $this->assertEquals("Cache flushed successfully", $this->cache->getMessage()); |
||||
213 | } |
||||
214 | |||||
215 | public function testHasCacheFromRedis() |
||||
216 | { |
||||
217 | $cacheKey = 'test_key'; |
||||
218 | $cacheData = ['name' => 'SÃlvio Silva', 'role' => 'Developer']; |
||||
219 | |||||
220 | $this->cache->putCache($cacheKey, $cacheData); |
||||
221 | |||||
222 | $this->assertEquals("Cache stored successfully", $this->cache->getMessage()); |
||||
223 | $this->assertTrue($this->cache->isSuccess()); |
||||
224 | } |
||||
225 | |||||
226 | public function testRenewCacheFromRedis() |
||||
227 | { |
||||
228 | $cacheKey = 'expired_key'; |
||||
229 | $cacheData = ['name' => 'Expired User', 'email' => '[email protected]']; |
||||
230 | |||||
231 | // Define TTL de 10 seg para que a chave ainda exista quando renovarmos |
||||
232 | $this->cache->putCache($cacheKey, $cacheData, '', 120); |
||||
233 | $this->assertEquals("Cache stored successfully", $this->cache->getMessage()); |
||||
234 | sleep(2); |
||||
235 | |||||
236 | // Verifica que a chave existe antes de renovar |
||||
237 | $this->assertNotEmpty($this->cache->getCache($cacheKey)); |
||||
238 | |||||
239 | $this->cache->renewCache($cacheKey, 7200); |
||||
240 | $this->assertTrue($this->cache->isSuccess()); |
||||
241 | $this->assertNotEmpty($this->cache->getCache($cacheKey)); |
||||
242 | } |
||||
243 | |||||
244 | public function testRenewCacheWithNamespaceFromRedis() |
||||
245 | { |
||||
246 | $cacheKey = 'expired_key'; |
||||
247 | $namespace = 'expired_namespace'; |
||||
248 | $cacheData = ['name' => 'Expired User', 'email' => '[email protected]']; |
||||
249 | |||||
250 | $this->cache->putCache($cacheKey, $cacheData, $namespace, 120); |
||||
251 | $this->assertEquals("Cache stored successfully", $this->cache->getMessage()); |
||||
252 | sleep(2); |
||||
253 | |||||
254 | $this->assertNotEmpty($this->cache->getCache($cacheKey, $namespace)); |
||||
255 | |||||
256 | $this->cache->renewCache($cacheKey, 7200, $namespace); |
||||
257 | $this->assertTrue($this->cache->isSuccess()); |
||||
258 | $this->assertNotEmpty($this->cache->getCache($cacheKey, $namespace)); |
||||
259 | } |
||||
260 | |||||
261 | public function test_remember_saves_and_recover_values() |
||||
262 | { |
||||
263 | $this->cache->flushCache(); |
||||
264 | |||||
265 | $value = $this->cache->remember('remember_test_key', 60, function () { |
||||
266 | return 'valor_teste'; |
||||
267 | }); |
||||
268 | |||||
269 | $this->assertEquals('valor_teste', $value); |
||||
270 | |||||
271 | $cachedValue = $this->cache->remember('remember_test_key', 60, function (){ |
||||
272 | return 'novo_valor'; |
||||
273 | }); |
||||
274 | |||||
275 | |||||
276 | $this->assertEquals('valor_teste', $cachedValue); |
||||
277 | } |
||||
278 | |||||
279 | public function test_remember_forever_saves_value_indefinitely() |
||||
280 | { |
||||
281 | $this->cache->flushCache(); |
||||
282 | |||||
283 | $value = $this->cache->rememberForever('remember_forever_key', function () { |
||||
284 | return 'valor_eterno'; |
||||
285 | }); |
||||
286 | $this->assertEquals('valor_eterno', $value); |
||||
287 | |||||
288 | $cachedValue = $this->cache->rememberForever('remember_forever_key', function () { |
||||
289 | return 'novo_valor'; |
||||
290 | }); |
||||
291 | |||||
292 | $this->assertEquals('valor_eterno', $cachedValue); |
||||
293 | } |
||||
294 | |||||
295 | |||||
296 | public function test_get_and_forget() |
||||
297 | { |
||||
298 | $cacheKey = 'cache_key_test'; |
||||
299 | $this->cache->putCache($cacheKey, 10); |
||||
300 | |||||
301 | $this->assertTrue($this->cache->isSuccess()); |
||||
302 | |||||
303 | $cacheData = $this->cache->getAndForget($cacheKey); |
||||
304 | |||||
305 | $this->assertTrue($this->cache->isSuccess()); |
||||
306 | $this->assertEquals(10, $cacheData); |
||||
307 | |||||
308 | $oldCacheData = $this->cache->getAndForget($cacheKey); |
||||
309 | |||||
310 | $this->assertNull($oldCacheData); |
||||
311 | $this->assertFalse($this->cache->isSuccess()); |
||||
312 | |||||
313 | $noCacheData = $this->cache->getAndForget('non_existent_cache_key'); |
||||
314 | $this->assertNull($noCacheData); |
||||
315 | } |
||||
316 | |||||
317 | public function test_store_if_not_present_with_add_function() |
||||
318 | { |
||||
319 | $existentKey = 'cache_key_test'; |
||||
320 | |||||
321 | $nonExistentKey = 'non_existent_key'; |
||||
322 | |||||
323 | $this->cache->putCache($existentKey, 'existent_data'); |
||||
324 | |||||
325 | $this->assertTrue($this->cache->isSuccess()); |
||||
326 | $this->assertEquals('existent_data', $this->cache->getCache($existentKey)); |
||||
327 | |||||
328 | $addCache = $this->cache->add($existentKey, 100); |
||||
329 | |||||
330 | $this->assertTrue($addCache); |
||||
331 | $this->assertNotEquals(100, 'existent_data'); |
||||
332 | |||||
333 | $addNonExistentKey = $this->cache->add($nonExistentKey, 'non_existent_data'); |
||||
334 | |||||
335 | $this->assertFalse($addNonExistentKey); |
||||
336 | $this->assertEquals('non_existent_data', $this->cache->getCache($nonExistentKey)); |
||||
337 | $this->assertTrue($this->cache->isSuccess()); |
||||
338 | |||||
339 | } |
||||
340 | |||||
341 | public function test_increment_function() { |
||||
342 | |||||
343 | $cacheKey = 'test_increment'; |
||||
344 | $cacheData = 2025; |
||||
345 | |||||
346 | $this->cache->putCache($cacheKey, $cacheData); |
||||
347 | |||||
348 | $this->assertTrue($this->cache->isSuccess()); |
||||
349 | $this->assertEquals($cacheData, $this->cache->getCache($cacheKey)); |
||||
350 | $this->assertIsNumeric($this->cache->getCache($cacheKey)); |
||||
351 | |||||
352 | $increment = $this->cache->increment($cacheKey, 2); |
||||
353 | $this->assertTrue($increment); |
||||
354 | |||||
355 | $this->assertEquals(2027, $this->cache->getCache($cacheKey)); |
||||
356 | |||||
357 | } |
||||
358 | |||||
359 | public function test_decrement_function() { |
||||
360 | |||||
361 | $cacheKey = 'test_decrement'; |
||||
362 | $cacheData = 2027; |
||||
363 | |||||
364 | $this->cache->putCache($cacheKey, $cacheData); |
||||
365 | |||||
366 | $this->assertTrue($this->cache->isSuccess()); |
||||
367 | $this->assertEquals($cacheData, $this->cache->getCache($cacheKey)); |
||||
368 | $this->assertIsNumeric($this->cache->getCache($cacheKey)); |
||||
369 | |||||
370 | $increment = $this->cache->decrement($cacheKey, 2); |
||||
371 | $this->assertTrue($increment); |
||||
372 | |||||
373 | $this->assertEquals(2025, $this->cache->getCache($cacheKey)); |
||||
374 | |||||
375 | } |
||||
376 | |||||
377 | public function test_get_many_cache_items() |
||||
378 | { |
||||
379 | $this->cache->useFormatter(); |
||||
380 | $cacheItems = [ |
||||
381 | 'key1' => 'value1', |
||||
382 | 'key2' => 'value2', |
||||
383 | 'key3' => 'value3' |
||||
384 | ]; |
||||
385 | foreach ($cacheItems as $key => $value) { |
||||
386 | $this->cache->putCache($key, $value); |
||||
387 | } |
||||
388 | $this->assertTrue($this->cache->isSuccess()); |
||||
389 | $retrievedItems = $this->cache->getMany(array_keys($cacheItems)); |
||||
390 | $this->assertTrue($this->cache->isSuccess()); |
||||
391 | $this->assertCount(3, $retrievedItems->toArray()); |
||||
392 | $this->assertEquals($cacheItems, $retrievedItems->toArray()); |
||||
393 | } |
||||
394 | |||||
395 | public function test_get_all_cache_items() |
||||
396 | { |
||||
397 | $namespace = 'test_namespace'; |
||||
398 | $cacheKey = 'test_key'; |
||||
399 | $cacheData = ['foo' => 'bar']; |
||||
400 | |||||
401 | $this->cache->putCache($cacheKey, $cacheData, $namespace); |
||||
402 | $this->assertTrue($this->cache->isSuccess()); |
||||
403 | $retrievedItems = $this->cache->getAll($namespace); |
||||
404 | $this->assertTrue($this->cache->isSuccess()); |
||||
405 | $this->assertCount(1, $retrievedItems); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
406 | $this->assertArrayHasKey($cacheKey, $retrievedItems); |
||||
0 ignored issues
–
show
It seems like
$retrievedItems can also be of type Countable ; however, parameter $array of PHPUnit\Framework\Assert::assertArrayHasKey() does only seem to accept ArrayAccess|array , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
407 | $this->assertEquals($cacheData, $retrievedItems[$cacheKey]); |
||||
408 | } |
||||
409 | |||||
410 | } |
||||
411 |