|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use PHPUnit\Framework\TestCase; |
|
4
|
|
|
use Silviooosilva\CacheerPhp\Cacheer; |
|
5
|
|
|
use Silviooosilva\CacheerPhp\Core\Connect; |
|
6
|
|
|
|
|
7
|
|
|
class DatabaseCacheStoreTest extends TestCase |
|
8
|
|
|
{ |
|
9
|
|
|
private $cache; |
|
10
|
|
|
|
|
11
|
|
|
protected function setUp(): void |
|
12
|
|
|
{ |
|
13
|
|
|
$this->cache = new Cacheer(); |
|
14
|
|
|
$this->cache->setConfig()->setDatabaseConnection(Connect::getInstance()->getAttribute(PDO::ATTR_DRIVER_NAME)); |
|
15
|
|
|
$this->cache->setDriver()->useDatabaseDriver(); |
|
16
|
|
|
$this->cache->setConfig()->setTimeZone('America/Toronto'); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
protected function tearDown(): void |
|
20
|
|
|
{ |
|
21
|
|
|
$this->cache->flushCache(); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function testPutCacheInDatabase() |
|
25
|
|
|
{ |
|
26
|
|
|
$cacheKey = 'test_key'; |
|
27
|
|
|
$cacheData = ['name' => 'John Doe', 'email' => '[email protected]']; |
|
28
|
|
|
|
|
29
|
|
|
$this->cache->putCache($cacheKey, $cacheData, '', 3600); |
|
30
|
|
|
|
|
31
|
|
|
$result = $this->cache->getCache($cacheKey); |
|
32
|
|
|
|
|
33
|
|
|
$this->assertNotEmpty($result); |
|
34
|
|
|
$this->assertEquals($cacheData, $result); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function testGetCacheFromDatabase() |
|
38
|
|
|
{ |
|
39
|
|
|
$cacheKey = 'test_key02'; |
|
40
|
|
|
$cacheData = ['name' => 'Jane Doe', 'email' => '[email protected]']; |
|
41
|
|
|
|
|
42
|
|
|
$this->cache->putCache($cacheKey, $cacheData, '', 3600); |
|
43
|
|
|
$this->assertEquals("Cache Stored Successfully", $this->cache->getMessage()); |
|
44
|
|
|
|
|
45
|
|
|
$result = $this->cache->getCache($cacheKey); |
|
46
|
|
|
|
|
47
|
|
|
$this->assertNotEmpty($result); |
|
48
|
|
|
$this->assertEquals($cacheData, $result); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function testExpiredCacheInDatabase() |
|
52
|
|
|
{ |
|
53
|
|
|
$cacheKey = 'expired_key'; |
|
54
|
|
|
$cacheData = ['name' => 'Expired User', 'email' => '[email protected]']; |
|
55
|
|
|
|
|
56
|
|
|
$this->cache->putCache($cacheKey, $cacheData, '', -3600); |
|
57
|
|
|
$this->assertEquals("Cache Stored Successfully", $this->cache->getMessage()); |
|
58
|
|
|
|
|
59
|
|
|
$this->assertEmpty($this->cache->getCache($cacheKey)); |
|
60
|
|
|
$this->assertFalse($this->cache->isSuccess()); |
|
61
|
|
|
} |
|
62
|
|
|
public function testOverwriteExistingCacheInDatabase() |
|
63
|
|
|
{ |
|
64
|
|
|
|
|
65
|
|
|
$cacheKey = 'overwrite_key'; |
|
66
|
|
|
$initialCacheData = ['name' => 'Initial Data', 'email' => '[email protected]']; |
|
67
|
|
|
$newCacheData = ['name' => 'New Data', 'email' => '[email protected]']; |
|
68
|
|
|
|
|
69
|
|
|
$expirationTime = date('Y-m-d H:i:s', time() + 3600); |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
$db = Connect::getInstance(); |
|
73
|
|
|
$query = $db->prepare("INSERT INTO cacheer_table (cacheKey, cacheData, cacheNamespace, expirationTime) VALUES (?, ?, ?, ?)"); |
|
74
|
|
|
$query->bindValue(1, $cacheKey); |
|
75
|
|
|
$query->bindValue(2, serialize($initialCacheData)); |
|
76
|
|
|
$query->bindValue(3, ''); |
|
77
|
|
|
$query->bindValue(4, $expirationTime); |
|
78
|
|
|
|
|
79
|
|
|
$this->assertTrue($query->execute()); |
|
80
|
|
|
|
|
81
|
|
|
$this->cache->appendCache($cacheKey, $newCacheData); |
|
82
|
|
|
$this->assertEquals("Cache updated successfully.", $this->cache->getMessage()); |
|
83
|
|
|
|
|
84
|
|
|
$driver = Connect::getInstance()->getAttribute(PDO::ATTR_DRIVER_NAME); |
|
85
|
|
|
$nowFunction = ($driver === 'sqlite') ? "DATETIME('now', 'localtime')" : "NOW()"; |
|
86
|
|
|
|
|
87
|
|
|
$query = $db->prepare("SELECT cacheData FROM cacheer_table WHERE cacheKey = ? AND cacheNamespace = ? AND expirationTime > $nowFunction"); |
|
88
|
|
|
$query->bindValue(1, $cacheKey); |
|
89
|
|
|
$query->bindValue(2, ''); |
|
90
|
|
|
|
|
91
|
|
|
$this->assertTrue($query->execute()); |
|
92
|
|
|
|
|
93
|
|
|
$result = $query->fetch(PDO::FETCH_ASSOC); |
|
94
|
|
|
|
|
95
|
|
|
$this->assertEquals($newCacheData, unserialize($result['cacheData'])); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function testPutManyCacheItemsInDatabase(): void |
|
99
|
|
|
{ |
|
100
|
|
|
|
|
101
|
|
|
$items = [ |
|
102
|
|
|
[ |
|
103
|
|
|
'cacheKey' => 'user_1_profile', |
|
104
|
|
|
'cacheData' => [ |
|
105
|
|
|
['name' => 'John Doe', 'email' => '[email protected]'], |
|
106
|
|
|
['name' => 'John Doe', 'email' => '[email protected]'], |
|
107
|
|
|
['name' => 'John Doe', 'email' => '[email protected]'], |
|
108
|
|
|
['name' => 'John Doe', 'email' => '[email protected]'] |
|
109
|
|
|
] |
|
110
|
|
|
], |
|
111
|
|
|
[ |
|
112
|
|
|
'cacheKey' => 'user_2_profile', |
|
113
|
|
|
'cacheData' => [ |
|
114
|
|
|
['name' => 'Jane Doe', 'email' => '[email protected]'], |
|
115
|
|
|
['name' => 'Jane Doe', 'email' => '[email protected]'], |
|
116
|
|
|
['name' => 'Jane Doe', 'email' => '[email protected]'], |
|
117
|
|
|
['name' => 'Jane Doe', 'email' => '[email protected]'] |
|
118
|
|
|
] |
|
119
|
|
|
] |
|
120
|
|
|
]; |
|
121
|
|
|
|
|
122
|
|
|
$this->cache->putMany($items); |
|
123
|
|
|
|
|
124
|
|
|
foreach ($items as $item) { |
|
125
|
|
|
$this->assertEquals($item['cacheData'], $this->cache->getCache($item['cacheKey'])); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function testAppendCacheWithNamespaceInDatabase(): void |
|
130
|
|
|
{ |
|
131
|
|
|
$cacheKey = 'test_append_key_ns'; |
|
132
|
|
|
$namespace = 'test_namespace'; |
|
133
|
|
|
|
|
134
|
|
|
$initialData = ['initial' => 'data']; |
|
135
|
|
|
$additionalData = ['new' => 'data']; |
|
136
|
|
|
|
|
137
|
|
|
$expectedData = array_merge($initialData, $additionalData); |
|
138
|
|
|
|
|
139
|
|
|
// Armazena os dados iniciais no cache com namespace |
|
140
|
|
|
$this->cache->putCache($cacheKey, $initialData, $namespace); |
|
141
|
|
|
$this->assertTrue($this->cache->isSuccess()); |
|
142
|
|
|
|
|
143
|
|
|
// Adiciona novos dados ao cache existente com namespace |
|
144
|
|
|
$this->cache->appendCache($cacheKey, $additionalData, $namespace); |
|
145
|
|
|
$this->assertTrue($this->cache->isSuccess()); |
|
146
|
|
|
|
|
147
|
|
|
// Verifica se os dados no cache são os esperados |
|
148
|
|
|
$cachedData = $this->cache->getCache($cacheKey, $namespace); |
|
149
|
|
|
$this->assertEquals($expectedData, $cachedData); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
public function testDataOutputShouldBeOfTypeJson() |
|
153
|
|
|
{ |
|
154
|
|
|
$this->cache->useFormatter(); |
|
155
|
|
|
|
|
156
|
|
|
$cacheKey = "key_json"; |
|
157
|
|
|
$cacheData = "data_json"; |
|
158
|
|
|
|
|
159
|
|
|
$this->cache->putCache($cacheKey, $cacheData); |
|
160
|
|
|
$this->assertTrue($this->cache->isSuccess()); |
|
161
|
|
|
|
|
162
|
|
|
$cacheOutput = $this->cache->getCache($cacheKey)->toJson(); |
|
163
|
|
|
$this->assertTrue($this->cache->isSuccess()); |
|
164
|
|
|
$this->assertJson($cacheOutput); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
public function testDataOutputShouldBeOfTypeArray() |
|
168
|
|
|
{ |
|
169
|
|
|
|
|
170
|
|
|
$this->cache->useFormatter(); |
|
171
|
|
|
|
|
172
|
|
|
$cacheKey = "key_array"; |
|
173
|
|
|
$cacheData = "data_array"; |
|
174
|
|
|
|
|
175
|
|
|
$this->cache->putCache($cacheKey, $cacheData); |
|
176
|
|
|
$this->assertTrue($this->cache->isSuccess()); |
|
177
|
|
|
|
|
178
|
|
|
$cacheOutput = $this->cache->getCache($cacheKey)->toArray(); |
|
179
|
|
|
$this->assertTrue($this->cache->isSuccess()); |
|
180
|
|
|
$this->assertIsArray($cacheOutput); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
public function testDataOutputShouldBeOfTypeObject() |
|
184
|
|
|
{ |
|
185
|
|
|
$this->cache->useFormatter(); |
|
186
|
|
|
|
|
187
|
|
|
$cacheKey = "key_object"; |
|
188
|
|
|
$cacheData = ["id" => 123]; |
|
189
|
|
|
|
|
190
|
|
|
$this->cache->putCache($cacheKey, $cacheData); |
|
191
|
|
|
$this->assertTrue($this->cache->isSuccess()); |
|
192
|
|
|
|
|
193
|
|
|
$cacheOutput = $this->cache->getCache($cacheKey)->toObject(); |
|
194
|
|
|
$this->assertTrue($this->cache->isSuccess()); |
|
195
|
|
|
$this->assertIsObject($cacheOutput); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
|
|
199
|
|
|
public function testClearCacheDataFromDatabase(): void |
|
200
|
|
|
{ |
|
201
|
|
|
$cacheKey = 'test_key'; |
|
202
|
|
|
$data = 'test_data'; |
|
203
|
|
|
|
|
204
|
|
|
$this->cache->putCache($cacheKey, $data); |
|
205
|
|
|
$this->assertEquals("Cache Stored Successfully", $this->cache->getMessage()); |
|
206
|
|
|
|
|
207
|
|
|
$this->cache->clearCache($cacheKey); |
|
208
|
|
|
$this->assertTrue($this->cache->isSuccess()); |
|
209
|
|
|
$this->assertEquals("Cache deleted successfully!", $this->cache->getMessage()); |
|
210
|
|
|
|
|
211
|
|
|
$this->assertEmpty($this->cache->getCache($cacheKey)); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
|
|
215
|
|
|
public function testFlushCacheDataFromDatabase(): void |
|
216
|
|
|
{ |
|
217
|
|
|
$key1 = 'test_key1'; |
|
218
|
|
|
$data1 = 'test_data1'; |
|
219
|
|
|
|
|
220
|
|
|
$key2 = 'test_key2'; |
|
221
|
|
|
$data2 = 'test_data2'; |
|
222
|
|
|
|
|
223
|
|
|
$this->cache->putCache($key1, $data1); |
|
224
|
|
|
$this->cache->putCache($key2, $data2); |
|
225
|
|
|
$this->assertTrue($this->cache->isSuccess()); |
|
226
|
|
|
$this->assertTrue($this->cache->isSuccess()); |
|
227
|
|
|
|
|
228
|
|
|
$this->cache->flushCache(); |
|
229
|
|
|
$this->assertTrue($this->cache->isSuccess()); |
|
230
|
|
|
$this->assertEquals("Flush finished successfully", $this->cache->getMessage()); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
public function test_remember_saves_and_recover_values() |
|
234
|
|
|
{ |
|
235
|
|
|
$value = $this->cache->remember('remember_test_key', 60, function () { |
|
236
|
|
|
return 'valor_teste'; |
|
237
|
|
|
}); |
|
238
|
|
|
|
|
239
|
|
|
$this->assertEquals('valor_teste', $value); |
|
240
|
|
|
|
|
241
|
|
|
$cachedValue = $this->cache->remember('remember_test_key', 60, function (){ |
|
242
|
|
|
return 'novo_valor'; |
|
243
|
|
|
}); |
|
244
|
|
|
|
|
245
|
|
|
|
|
246
|
|
|
$this->assertEquals('valor_teste', $cachedValue); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
public function test_remember_forever_saves_value_indefinitely() |
|
250
|
|
|
{ |
|
251
|
|
|
|
|
252
|
|
|
$value = $this->cache->rememberForever('remember_forever_key', function () { |
|
253
|
|
|
return 'valor_eterno'; |
|
254
|
|
|
}); |
|
255
|
|
|
$this->assertEquals('valor_eterno', $value); |
|
256
|
|
|
|
|
257
|
|
|
$cachedValue = $this->cache->rememberForever('remember_forever_key', function () { |
|
258
|
|
|
return 'novo_valor'; |
|
259
|
|
|
}); |
|
260
|
|
|
|
|
261
|
|
|
$this->assertEquals('valor_eterno', $cachedValue); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
public function test_get_and_forget() |
|
265
|
|
|
{ |
|
266
|
|
|
$cacheKey = 'cache_key_test'; |
|
267
|
|
|
$this->cache->putCache($cacheKey, 10); |
|
268
|
|
|
|
|
269
|
|
|
$this->assertTrue($this->cache->isSuccess()); |
|
270
|
|
|
|
|
271
|
|
|
$cacheData = $this->cache->getAndForget($cacheKey); |
|
272
|
|
|
|
|
273
|
|
|
$this->assertTrue($this->cache->isSuccess()); |
|
274
|
|
|
$this->assertEquals(10, $cacheData); |
|
275
|
|
|
|
|
276
|
|
|
$oldCacheData = $this->cache->getAndForget($cacheKey); |
|
277
|
|
|
|
|
278
|
|
|
$this->assertNull($oldCacheData); |
|
279
|
|
|
$this->assertFalse($this->cache->isSuccess()); |
|
280
|
|
|
|
|
281
|
|
|
$noCacheData = $this->cache->getAndForget('non_existent_cache_key'); |
|
282
|
|
|
$this->assertNull($noCacheData); |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
public function test_store_if_not_present_with_add_function() |
|
286
|
|
|
{ |
|
287
|
|
|
$existentKey = 'cache_key_test'; |
|
288
|
|
|
|
|
289
|
|
|
$nonExistentKey = 'non_existent_key'; |
|
290
|
|
|
|
|
291
|
|
|
$this->cache->putCache($existentKey, 'existent_data'); |
|
292
|
|
|
|
|
293
|
|
|
$this->assertTrue($this->cache->isSuccess()); |
|
294
|
|
|
$this->assertEquals('existent_data', $this->cache->getCache($existentKey)); |
|
295
|
|
|
|
|
296
|
|
|
$addCache = $this->cache->add($existentKey, 100); |
|
297
|
|
|
|
|
298
|
|
|
$this->assertTrue($addCache); |
|
299
|
|
|
$this->assertNotEquals(100, 'existent_data'); |
|
300
|
|
|
|
|
301
|
|
|
$addNonExistentKey = $this->cache->add($nonExistentKey, 'non_existent_data'); |
|
302
|
|
|
|
|
303
|
|
|
$this->assertFalse($addNonExistentKey); |
|
304
|
|
|
$this->assertEquals('non_existent_data', $this->cache->getCache($nonExistentKey)); |
|
305
|
|
|
$this->assertTrue($this->cache->isSuccess()); |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
|
|
309
|
|
|
public function test_increment_function() { |
|
310
|
|
|
|
|
311
|
|
|
$cacheKey = 'test_increment'; |
|
312
|
|
|
$cacheData = 2025; |
|
313
|
|
|
|
|
314
|
|
|
$this->cache->putCache($cacheKey, $cacheData); |
|
315
|
|
|
|
|
316
|
|
|
$this->assertTrue($this->cache->isSuccess()); |
|
317
|
|
|
$this->assertEquals($cacheData, $this->cache->getCache($cacheKey)); |
|
318
|
|
|
$this->assertIsNumeric($this->cache->getCache($cacheKey)); |
|
319
|
|
|
|
|
320
|
|
|
$increment = $this->cache->increment($cacheKey, 2); |
|
321
|
|
|
$this->assertTrue($increment); |
|
322
|
|
|
|
|
323
|
|
|
$this->assertEquals(2027, $this->cache->getCache($cacheKey)); |
|
324
|
|
|
|
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
public function test_decrement_function() { |
|
328
|
|
|
|
|
329
|
|
|
$cacheKey = 'test_decrement'; |
|
330
|
|
|
$cacheData = 2027; |
|
331
|
|
|
|
|
332
|
|
|
$this->cache->putCache($cacheKey, $cacheData); |
|
333
|
|
|
|
|
334
|
|
|
$this->assertTrue($this->cache->isSuccess()); |
|
335
|
|
|
$this->assertEquals($cacheData, $this->cache->getCache($cacheKey)); |
|
336
|
|
|
$this->assertIsNumeric($this->cache->getCache($cacheKey)); |
|
337
|
|
|
|
|
338
|
|
|
$increment = $this->cache->decrement($cacheKey, 2); |
|
339
|
|
|
$this->assertTrue($increment); |
|
340
|
|
|
|
|
341
|
|
|
$this->assertEquals(2025, $this->cache->getCache($cacheKey)); |
|
342
|
|
|
|
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
public function test_get_many_cache_items() |
|
346
|
|
|
{ |
|
347
|
|
|
$cacheItems = [ |
|
348
|
|
|
'key1' => 'value1', |
|
349
|
|
|
'key2' => 'value2', |
|
350
|
|
|
'key3' => 'value3' |
|
351
|
|
|
]; |
|
352
|
|
|
foreach ($cacheItems as $key => $value) { |
|
353
|
|
|
$this->cache->putCache($key, $value); |
|
354
|
|
|
} |
|
355
|
|
|
$this->assertTrue($this->cache->isSuccess()); |
|
356
|
|
|
$retrievedItems = $this->cache->getMany(array_keys($cacheItems)); |
|
357
|
|
|
$this->assertTrue($this->cache->isSuccess()); |
|
358
|
|
|
$this->assertCount(3, $retrievedItems); |
|
359
|
|
|
$this->assertEquals($cacheItems, $retrievedItems); |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
public function test_get_all_cache_items() |
|
363
|
|
|
{ |
|
364
|
|
|
$namespace = 'test_namespace'; |
|
365
|
|
|
$cacheKey = 'test_key'; |
|
366
|
|
|
$cacheData = ['foo' => 'bar']; |
|
367
|
|
|
|
|
368
|
|
|
$this->cache->putCache($cacheKey, $cacheData, $namespace); |
|
369
|
|
|
$this->assertTrue($this->cache->isSuccess()); |
|
370
|
|
|
$retrievedItems = $this->cache->getAll($namespace); |
|
371
|
|
|
$this->assertTrue($this->cache->isSuccess()); |
|
372
|
|
|
$this->assertCount(1, $retrievedItems); |
|
373
|
|
|
$this->assertArrayHasKey($cacheKey, $retrievedItems); |
|
|
|
|
|
|
374
|
|
|
$this->assertEquals($cacheData, $retrievedItems[$cacheKey]); |
|
375
|
|
|
} |
|
376
|
|
|
} |
|
377
|
|
|
|