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
|
|
|
$this->cache->getCache($cacheKey, '', '2 seconds'); |
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
|
|
|
|
99
|
|
|
private function removeDirectoryRecursively($dir) |
100
|
|
|
{ |
101
|
|
|
if (!is_dir($dir)) { |
102
|
|
|
return; |
103
|
|
|
} |
104
|
|
|
$items = scandir($dir); |
105
|
|
|
foreach ($items as $item) { |
106
|
|
|
if ($item === '.' || $item === '..') { |
107
|
|
|
continue; |
108
|
|
|
} |
109
|
|
|
$path = $dir . DIRECTORY_SEPARATOR . $item; |
110
|
|
|
if (is_dir($path)) { |
111
|
|
|
$this->removeDirectoryRecursively($path); |
112
|
|
|
} else { |
113
|
|
|
unlink($path); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
rmdir($dir); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function testUseDefaultDriverCreatesCacheDirInProjectRoot() |
120
|
|
|
{ |
121
|
|
|
$cacheer = new Cacheer(); |
122
|
|
|
$driver = new CacheDriver($cacheer); |
123
|
|
|
|
124
|
|
|
$projectRoot = EnvHelper::getRootPath(); |
125
|
|
|
$expectedCacheDir = $projectRoot . DIRECTORY_SEPARATOR . "CacheerPHP" . DIRECTORY_SEPARATOR . "Cache"; |
126
|
|
|
|
127
|
|
|
if (is_dir($expectedCacheDir)) { |
128
|
|
|
$this->removeDirectoryRecursively($expectedCacheDir); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$driver->useDefaultDriver(); |
132
|
|
|
|
133
|
|
|
$this->assertDirectoryExists($expectedCacheDir); |
134
|
|
|
|
135
|
|
|
if (is_dir($expectedCacheDir)) { |
136
|
|
|
$this->removeDirectoryRecursively($expectedCacheDir); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function testPutCacheWithNamespace() |
141
|
|
|
{ |
142
|
|
|
$cacheKey = 'namespace_key'; |
143
|
|
|
$data = 'namespace_data'; |
144
|
|
|
$namespace = 'my_namespace'; |
145
|
|
|
|
146
|
|
|
$this->cache->putCache($cacheKey, $data, $namespace); |
147
|
|
|
$this->assertTrue($this->cache->isSuccess()); |
148
|
|
|
|
149
|
|
|
$cachedData = $this->cache->getCache($cacheKey, $namespace); |
150
|
|
|
$this->assertEquals($data, $cachedData); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function testClearCacheWithNamespace() |
154
|
|
|
{ |
155
|
|
|
$cacheKey = 'namespace_key_clear'; |
156
|
|
|
$data = 'namespace_data_clear'; |
157
|
|
|
$namespace = 'clear_namespace'; |
158
|
|
|
|
159
|
|
|
$this->cache->putCache($cacheKey, $data, $namespace); |
160
|
|
|
$this->assertTrue($this->cache->isSuccess()); |
161
|
|
|
|
162
|
|
|
$this->cache->clearCache($cacheKey, $namespace); |
163
|
|
|
$this->assertTrue($this->cache->isSuccess()); |
164
|
|
|
|
165
|
|
|
$cachedData = $this->cache->getCache($cacheKey, $namespace); |
166
|
|
|
$this->assertFalse($this->cache->isSuccess()); |
167
|
|
|
$this->assertNull($cachedData); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function testFlushCacheRemovesNamespacedFiles() |
171
|
|
|
{ |
172
|
|
|
$cacheKey = 'ns_flush_key'; |
173
|
|
|
$data = 'ns_flush_data'; |
174
|
|
|
$namespace = 'flush_namespace'; |
175
|
|
|
|
176
|
|
|
$this->cache->putCache($cacheKey, $data, $namespace); |
177
|
|
|
$this->assertTrue($this->cache->isSuccess()); |
178
|
|
|
|
179
|
|
|
$this->cache->flushCache(); |
180
|
|
|
|
181
|
|
|
$cachedData = $this->cache->getCache($cacheKey, $namespace); |
182
|
|
|
$this->assertFalse($this->cache->isSuccess()); |
183
|
|
|
$this->assertNull($cachedData); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function testAppendCacheWithDifferentTypes() |
187
|
|
|
{ |
188
|
|
|
$cacheKey = 'append_type_key'; |
189
|
|
|
$initialData = ['a' => 1]; |
190
|
|
|
$additionalData = ['b' => 2]; |
191
|
|
|
$expectedData = ['a' => 1, 'b' => 2]; |
192
|
|
|
|
193
|
|
|
$this->cache->putCache($cacheKey, $initialData); |
194
|
|
|
$this->cache->appendCache($cacheKey, $additionalData); |
195
|
|
|
$this->assertEquals($expectedData, $this->cache->getCache($cacheKey)); |
196
|
|
|
|
197
|
|
|
$this->cache->appendCache($cacheKey, ['c' => 'string']); |
198
|
|
|
$expectedData['c'] = 'string'; |
199
|
|
|
$this->assertEquals($expectedData, $this->cache->getCache($cacheKey)); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
} |
203
|
|
|
|