1 | <?php |
||
2 | |||
3 | use PHPUnit\Framework\TestCase; |
||
4 | use Silviooosilva\CacheerPhp\Cacheer; |
||
5 | use Silviooosilva\CacheerPhp\CacheStore\ArrayCacheStore; |
||
6 | |||
7 | class ArrayCacheStoreTest extends TestCase |
||
8 | { |
||
9 | |||
10 | private $cache; |
||
11 | |||
12 | protected function setUp(): void |
||
13 | { |
||
14 | $this->cache = new Cacheer(); |
||
15 | $this->cache->setDriver()->useArrayDriver(); |
||
16 | $this->cache->setConfig()->setTimeZone('America/Toronto'); |
||
17 | } |
||
18 | |||
19 | protected function tearDown(): void |
||
20 | { |
||
21 | $this->cache->flushCache(); |
||
22 | } |
||
23 | |||
24 | public function testUsingArrayDriverSetsProperInstance() |
||
25 | { |
||
26 | $this->assertInstanceOf(ArrayCacheStore::class, $this->cache->cacheStore); |
||
27 | } |
||
28 | |||
29 | public function testPutAndGetCacheInArray() |
||
30 | { |
||
31 | $cacheKey = 'test_key'; |
||
32 | $cacheData = ['name' => 'John Doe', 'email' => '[email protected]']; |
||
33 | |||
34 | $this->cache->putCache($cacheKey, $cacheData, '', 3600); |
||
35 | |||
36 | $result = $this->cache->getCache($cacheKey); |
||
37 | |||
38 | $this->assertNotEmpty($result); |
||
39 | $this->assertEquals($cacheData, $result); |
||
40 | } |
||
41 | |||
42 | public function testExpiredCacheInArray() |
||
43 | { |
||
44 | $cacheKey = 'expired_key'; |
||
45 | $cacheData = ['name' => 'Expired User', 'email' => '[email protected]']; |
||
46 | |||
47 | $this->cache->putCache($cacheKey, $cacheData, '', 1); |
||
48 | sleep(3); |
||
49 | |||
50 | $this->assertEquals("Cache stored successfully", $this->cache->getMessage()); |
||
51 | $this->assertEmpty($this->cache->getCache($cacheKey)); |
||
52 | $this->assertFalse($this->cache->isSuccess()); |
||
53 | } |
||
54 | |||
55 | |||
56 | public function testOverwriteExistingCacheInArray() |
||
57 | { |
||
58 | $cacheKey = 'overwrite_key'; |
||
59 | $initialCacheData = ['name' => 'Initial Data', 'email' => '[email protected]']; |
||
60 | $newCacheData = ['name' => 'New Data', 'email' => '[email protected]']; |
||
61 | |||
62 | $this->cache->putCache($cacheKey, $initialCacheData); |
||
63 | $this->assertEquals("Cache stored successfully", $this->cache->getMessage()); |
||
64 | |||
65 | $this->cache->appendCache($cacheKey, $newCacheData); |
||
66 | $this->assertEquals("Cache appended successfully", $this->cache->getMessage()); |
||
67 | $this->assertEquals($newCacheData, $this->cache->getCache($cacheKey)); |
||
68 | } |
||
69 | |||
70 | |||
71 | public function testPutManyCacheItemsInArray() |
||
72 | { |
||
73 | $items = [ |
||
74 | [ |
||
75 | 'cacheKey' => 'user_1_profile', |
||
76 | 'cacheData' => [ |
||
77 | ['name' => 'John Doe', 'email' => '[email protected]'], |
||
78 | ['name' => 'John Doe', 'email' => '[email protected]'], |
||
79 | ['name' => 'John Doe', 'email' => '[email protected]'], |
||
80 | ['name' => 'John Doe', 'email' => '[email protected]'] |
||
81 | ] |
||
82 | ], |
||
83 | [ |
||
84 | 'cacheKey' => 'user_2_profile', |
||
85 | 'cacheData' => [ |
||
86 | ['name' => 'Jane Doe', 'email' => '[email protected]'], |
||
87 | ['name' => 'Jane Doe', 'email' => '[email protected]'], |
||
88 | ['name' => 'Jane Doe', 'email' => '[email protected]'], |
||
89 | ['name' => 'Jane Doe', 'email' => '[email protected]'] |
||
90 | ] |
||
91 | ] |
||
92 | ]; |
||
93 | |||
94 | $this->cache->putMany($items); |
||
95 | foreach ($items as $item) { |
||
96 | |||
97 | $this->assertEquals($item['cacheData'], $this->cache->getCache($item['cacheKey'])); |
||
98 | } |
||
99 | } |
||
100 | |||
101 | public function testHasCacheFromArray() |
||
102 | { |
||
103 | $cacheKey = 'test_key'; |
||
104 | $cacheData = ['name' => 'SÃlvio Silva', 'role' => 'Developer']; |
||
105 | |||
106 | $this->cache->putCache($cacheKey, $cacheData); |
||
107 | |||
108 | $this->assertEquals("Cache stored successfully", $this->cache->getMessage()); |
||
109 | $this->assertTrue($this->cache->isSuccess()); |
||
110 | |||
111 | $this->cache->has($cacheKey); |
||
112 | $this->assertTrue($this->cache->isSuccess()); |
||
113 | } |
||
114 | |||
115 | public function testRenewCacheFromArray() |
||
116 | { |
||
117 | $cacheKey = 'expired_key'; |
||
118 | $cacheData = ['name' => 'Expired User', 'email' => '[email protected]']; |
||
119 | |||
120 | // Define TTL de 10 seg para que a chave ainda exista quando renovarmos |
||
121 | $this->cache->putCache($cacheKey, $cacheData, '', 120); |
||
122 | $this->assertEquals("Cache stored successfully", $this->cache->getMessage()); |
||
123 | sleep(2); |
||
124 | |||
125 | // Verifica que a chave existe antes de renovar |
||
126 | $this->assertNotEmpty($this->cache->getCache($cacheKey)); |
||
127 | |||
128 | $this->cache->renewCache($cacheKey, 7200); |
||
129 | $this->assertTrue($this->cache->isSuccess()); |
||
130 | $this->assertNotEmpty($this->cache->getCache($cacheKey)); |
||
131 | } |
||
132 | |||
133 | public function testRenewCacheWithNamespaceFromArray() |
||
134 | { |
||
135 | $cacheKey = 'expired_key'; |
||
136 | $namespace = 'expired_namespace'; |
||
137 | $cacheData = ['name' => 'Expired User', 'email' => '[email protected]']; |
||
138 | |||
139 | $this->cache->putCache($cacheKey, $cacheData, $namespace, 120); |
||
140 | $this->assertEquals("Cache stored successfully", $this->cache->getMessage()); |
||
141 | sleep(2); |
||
142 | |||
143 | $this->assertNotEmpty($this->cache->getCache($cacheKey, $namespace)); |
||
144 | |||
145 | $this->cache->renewCache($cacheKey, 7200, $namespace); |
||
146 | $this->assertTrue($this->cache->isSuccess()); |
||
147 | $this->assertNotEmpty($this->cache->getCache($cacheKey, $namespace)); |
||
148 | } |
||
149 | |||
150 | public function testClearCacheDataFromArray() |
||
151 | { |
||
152 | $cacheKey = 'test_key'; |
||
153 | $data = 'test_data'; |
||
154 | |||
155 | $this->cache->putCache($cacheKey, $data); |
||
156 | $this->assertEquals("Cache stored successfully", $this->cache->getMessage()); |
||
157 | |||
158 | $this->cache->clearCache($cacheKey); |
||
159 | $this->assertTrue($this->cache->isSuccess()); |
||
160 | $this->assertEquals("Cache cleared successfully", $this->cache->getMessage()); |
||
161 | |||
162 | $this->assertEmpty($this->cache->getCache($cacheKey)); |
||
163 | } |
||
164 | |||
165 | public function testFlushCacheDataFromArray() |
||
166 | { |
||
167 | $key1 = 'test_key1'; |
||
168 | $data1 = 'test_data1'; |
||
169 | |||
170 | $key2 = 'test_key2'; |
||
171 | $data2 = 'test_data2'; |
||
172 | |||
173 | $this->cache->putCache($key1, $data1); |
||
174 | $this->cache->putCache($key2, $data2); |
||
175 | $this->assertTrue($this->cache->isSuccess()); |
||
176 | $this->assertTrue($this->cache->isSuccess()); |
||
177 | |||
178 | $this->cache->flushCache(); |
||
179 | |||
180 | $this->assertTrue($this->cache->isSuccess()); |
||
181 | $this->assertEquals("Cache flushed successfully", $this->cache->getMessage()); |
||
182 | } |
||
183 | |||
184 | public function test_remember_saves_and_recover_values() |
||
185 | { |
||
186 | $this->cache->flushCache(); |
||
187 | |||
188 | $value = $this->cache->remember('remember_test_key', 60, function () { |
||
189 | return 'valor_teste'; |
||
190 | }); |
||
191 | |||
192 | $this->assertEquals('valor_teste', $value); |
||
193 | |||
194 | $cachedValue = $this->cache->remember('remember_test_key', 60, function (){ |
||
195 | return 'novo_valor'; |
||
196 | }); |
||
197 | |||
198 | |||
199 | $this->assertEquals('valor_teste', $cachedValue); |
||
200 | } |
||
201 | |||
202 | public function test_remember_forever_saves_value_indefinitely() |
||
203 | { |
||
204 | $this->cache->flushCache(); |
||
205 | |||
206 | $value = $this->cache->rememberForever('remember_forever_key', function () { |
||
207 | return 'valor_eterno'; |
||
208 | }); |
||
209 | $this->assertEquals('valor_eterno', $value); |
||
210 | |||
211 | $cachedValue = $this->cache->rememberForever('remember_forever_key', function () { |
||
212 | return 'novo_valor'; |
||
213 | }); |
||
214 | |||
215 | $this->assertEquals('valor_eterno', $cachedValue); |
||
216 | } |
||
217 | |||
218 | |||
219 | public function test_get_and_forget() |
||
220 | { |
||
221 | $cacheKey = 'cache_key_test'; |
||
222 | $this->cache->putCache($cacheKey, 10); |
||
223 | |||
224 | $this->assertTrue($this->cache->isSuccess()); |
||
225 | |||
226 | $cacheData = $this->cache->getAndForget($cacheKey); |
||
227 | |||
228 | $this->assertTrue($this->cache->isSuccess()); |
||
229 | $this->assertEquals(10, $cacheData); |
||
230 | |||
231 | $oldCacheData = $this->cache->getAndForget($cacheKey); |
||
232 | |||
233 | $this->assertNull($oldCacheData); |
||
234 | $this->assertFalse($this->cache->isSuccess()); |
||
235 | |||
236 | $noCacheData = $this->cache->getAndForget('non_existent_cache_key'); |
||
237 | $this->assertNull($noCacheData); |
||
238 | } |
||
239 | |||
240 | public function test_store_if_not_present_with_add_function() |
||
241 | { |
||
242 | $existentKey = 'cache_key_test'; |
||
243 | |||
244 | $nonExistentKey = 'non_existent_key'; |
||
245 | |||
246 | $this->cache->putCache($existentKey, 'existent_data'); |
||
247 | |||
248 | $this->assertTrue($this->cache->isSuccess()); |
||
249 | $this->assertEquals('existent_data', $this->cache->getCache($existentKey)); |
||
250 | |||
251 | $addCache = $this->cache->add($existentKey, 100); |
||
252 | |||
253 | $this->assertTrue($addCache); |
||
254 | $this->assertNotEquals(100, 'existent_data'); |
||
255 | |||
256 | $addNonExistentKey = $this->cache->add($nonExistentKey, 'non_existent_data'); |
||
257 | |||
258 | $this->assertFalse($addNonExistentKey); |
||
259 | $this->assertEquals('non_existent_data', $this->cache->getCache($nonExistentKey)); |
||
260 | $this->assertTrue($this->cache->isSuccess()); |
||
261 | |||
262 | } |
||
263 | |||
264 | public function test_increment_function() { |
||
265 | |||
266 | $cacheKey = 'test_increment'; |
||
267 | $cacheData = 2025; |
||
268 | |||
269 | $this->cache->putCache($cacheKey, $cacheData); |
||
270 | |||
271 | $this->assertTrue($this->cache->isSuccess()); |
||
272 | $this->assertEquals($cacheData, $this->cache->getCache($cacheKey)); |
||
273 | $this->assertIsNumeric($this->cache->getCache($cacheKey)); |
||
274 | |||
275 | $increment = $this->cache->increment($cacheKey, 2); |
||
276 | $this->assertTrue($increment); |
||
277 | |||
278 | $this->assertEquals(2027, $this->cache->getCache($cacheKey)); |
||
279 | |||
280 | } |
||
281 | |||
282 | public function test_decrement_function() { |
||
283 | |||
284 | $cacheKey = 'test_decrement'; |
||
285 | $cacheData = 2027; |
||
286 | |||
287 | $this->cache->putCache($cacheKey, $cacheData); |
||
288 | |||
289 | $this->assertTrue($this->cache->isSuccess()); |
||
290 | $this->assertEquals($cacheData, $this->cache->getCache($cacheKey)); |
||
291 | $this->assertIsNumeric($this->cache->getCache($cacheKey)); |
||
292 | |||
293 | $increment = $this->cache->decrement($cacheKey, 2); |
||
294 | $this->assertTrue($increment); |
||
295 | |||
296 | $this->assertEquals(2025, $this->cache->getCache($cacheKey)); |
||
297 | |||
298 | } |
||
299 | |||
300 | public function test_get_many_cache_items() |
||
301 | { |
||
302 | $cacheItems = [ |
||
303 | 'key1' => 'value1', |
||
304 | 'key2' => 'value2', |
||
305 | 'key3' => 'value3' |
||
306 | ]; |
||
307 | foreach ($cacheItems as $key => $value) { |
||
308 | $this->cache->putCache($key, $value); |
||
309 | } |
||
310 | $this->assertTrue($this->cache->isSuccess()); |
||
311 | $retrievedItems = $this->cache->getMany(array_keys($cacheItems)); |
||
312 | $this->assertTrue($this->cache->isSuccess()); |
||
313 | $this->assertCount(3, $retrievedItems); |
||
314 | $this->assertEquals($cacheItems, $retrievedItems); |
||
315 | } |
||
316 | |||
317 | public function test_get_all_cache_items() |
||
318 | { |
||
319 | $namespace = 'test_namespace'; |
||
320 | $cacheKey = 'test_key'; |
||
321 | $cacheData = ['foo' => 'bar']; |
||
322 | |||
323 | $this->cache->putCache($cacheKey, $cacheData, $namespace); |
||
324 | $this->assertTrue($this->cache->isSuccess()); |
||
325 | $retrievedItems = $this->cache->getAll($namespace); |
||
326 | $this->assertTrue($this->cache->isSuccess()); |
||
327 | |||
328 | $expectedKey = $namespace . ':' . $cacheKey; |
||
329 | $this->assertCount(1, $retrievedItems); |
||
330 | $this->assertArrayHasKey($expectedKey, $retrievedItems); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
331 | $this->assertEquals($cacheData, $retrievedItems[$expectedKey]); |
||
332 | } |
||
333 | } |