Skip to content

Commit 227ccd4

Browse files
committed
APCu & Memcached import tests
1 parent 45dd0a1 commit 227ccd4

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

tests/Dashboards/APCuTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,60 @@ public function testGetAllKeysTreeView(): void {
172172

173173
$this->assertEquals($this->sortTreeKeys($expected), $this->sortTreeKeys($result));
174174
}
175+
176+
public function testExportAndImport(): void {
177+
$keys_to_test = [
178+
'e2e:apcu:key1' => ['value' => 'simple-value', 'ttl' => 120],
179+
'e2e:apcu:key2' => ['value' => 'no-expire-value', 'ttl' => 0],
180+
'e2e:apcu:key3' => ['value' => ['json' => 'data'], 'ttl' => 300],
181+
];
182+
183+
$export_keys_array = [];
184+
185+
foreach ($keys_to_test as $key => $data) {
186+
apcu_store($key, $data['value'], $data['ttl']);
187+
$export_keys_array[] = ['key' => $key, 'info' => ['ttl' => $data['ttl']]];
188+
}
189+
190+
$exported_json = Helpers::export(
191+
$export_keys_array,
192+
'apcu_backup',
193+
static fn (string $key): string => base64_encode(serialize(apcu_fetch($key))),
194+
true
195+
);
196+
197+
apcu_clear_cache();
198+
199+
foreach (array_keys($keys_to_test) as $key) {
200+
$this->assertFalse(apcu_exists($key));
201+
}
202+
203+
$tmp_file_path = tempnam(sys_get_temp_dir(), 'pu-');
204+
file_put_contents($tmp_file_path, $exported_json);
205+
206+
$_FILES['import'] = [
207+
'name' => 'test_import.json',
208+
'type' => 'application/json',
209+
'tmp_name' => $tmp_file_path,
210+
'error' => UPLOAD_ERR_OK,
211+
'size' => filesize($tmp_file_path),
212+
];
213+
214+
Http::stopRedirect();
215+
216+
Helpers::import(
217+
static fn (string $key): bool => apcu_exists($key),
218+
static function (string $key, string $value, int $ttl): bool {
219+
return apcu_store($key, unserialize(base64_decode($value), ['allowed_classes' => false]), $ttl);
220+
}
221+
);
222+
223+
foreach ($keys_to_test as $key => $data) {
224+
$this->assertTrue(apcu_exists($key));
225+
$this->assertSame($data['value'], apcu_fetch($key));
226+
}
227+
228+
unlink($tmp_file_path);
229+
unset($_FILES['import']);
230+
}
175231
}

tests/Dashboards/MemcachedTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,4 +240,65 @@ public function testGetAllKeysTreeView(): void {
240240

241241
$this->memcached->flush();
242242
}
243+
244+
/**
245+
* @throws MemcachedException
246+
*/
247+
public function testExportAndImport(): void {
248+
$keys_to_test = [
249+
'e2e:mem:key1' => ['value' => 'simple-value', 'ttl' => 120],
250+
'e2e:mem:key2' => ['value' => 'no-expire-value', 'ttl' => 0],
251+
'e2e:mem:key3' => ['value' => '{"json": "data"}', 'ttl' => 300],
252+
];
253+
254+
$export_keys_array = [];
255+
256+
foreach ($keys_to_test as $key => $data) {
257+
$this->memcached->set($key, $data['value'], $data['ttl']);
258+
$export_keys_array[] = ['key' => urlencode($key), 'info' => ['ttl' => $data['ttl']]];
259+
}
260+
261+
$exported_json = Helpers::export(
262+
$export_keys_array,
263+
'memcached_backup',
264+
function (string $key): ?string {
265+
$value = $this->memcached->getKey(urldecode($key));
266+
267+
return $value !== false ? base64_encode($value) : null;
268+
},
269+
true
270+
);
271+
272+
$this->memcached->flush();
273+
274+
foreach (array_keys($keys_to_test) as $key) {
275+
$this->assertFalse($this->memcached->exists($key));
276+
}
277+
278+
$tmp_file_path = tempnam(sys_get_temp_dir(), 'pu-');
279+
file_put_contents($tmp_file_path, $exported_json);
280+
281+
$_FILES['import'] = [
282+
'name' => 'test_import.json',
283+
'type' => 'application/json',
284+
'tmp_name' => $tmp_file_path,
285+
'error' => UPLOAD_ERR_OK,
286+
'size' => filesize($tmp_file_path),
287+
];
288+
289+
Http::stopRedirect();
290+
291+
Helpers::import(
292+
fn (string $key): bool => $this->memcached->exists($key),
293+
fn (string $key, string $value, int $ttl): bool => $this->memcached->set(urldecode($key), base64_decode($value), $ttl)
294+
);
295+
296+
foreach ($keys_to_test as $key => $data) {
297+
$this->assertTrue($this->memcached->exists($key));
298+
$this->assertSame($data['value'], $this->memcached->getKey($key));
299+
}
300+
301+
unlink($tmp_file_path);
302+
unset($_FILES['import']);
303+
}
243304
}

0 commit comments

Comments
 (0)