|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use PHPUnit\Framework\TestCase; |
|
4
|
|
|
use Silviooosilva\CacheerPhp\Cacheer; |
|
5
|
|
|
use Silviooosilva\CacheerPhp\CacheStore\RedisCacheStore; |
|
6
|
|
|
|
|
7
|
|
|
class RedisTest 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 testFlushCacheDataFromDatabase() |
|
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
|
|
|
} |
|
262
|
|
|
|