Skip to content

Commit 0d378c4

Browse files
committed
Import tests
1 parent 6411faa commit 0d378c4

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

src/Dashboards/Redis/Compatibility/Cluster/PredisCluster.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
use RobiNN\Pca\Dashboards\Redis\Compatibility\RedisModules;
1919
use Throwable;
2020

21+
/**
22+
* @method bool restore(string $key, int $ttl, string $value)
23+
*/
2124
class PredisCluster extends PredisClient implements RedisCompatibilityInterface {
2225
use RedisJson;
2326
use RedisModules;

src/Dashboards/Redis/Compatibility/Predis.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
use Predis\Client;
1212
use Predis\Collection\Iterator\Keyspace;
1313

14+
/**
15+
* @method bool restore(string $key, int $ttl, string $value)
16+
*/
1417
class Predis extends Client implements RedisCompatibilityInterface {
1518
use RedisJson;
1619
use RedisModules;

tests/Dashboards/Redis/RedisTestCase.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,4 +423,76 @@ public function testSlowlog(): void {
423423
$this->redis->execConfig('SET', $config_key, $original_config_value);
424424
$this->assertTrue($this->redis->resetSlowlog());
425425
}
426+
427+
/**
428+
* @throws Exception
429+
*/
430+
public function testExportAndImport(): void {
431+
$keys_to_test = [
432+
'pu:test:key1' => ['value' => 'simple-value', 'ttl' => 120],
433+
'pu:test:key2' => ['value' => 'no-expire-value', 'ttl' => -1],
434+
'pu:test:key3' => ['value' => '{"json": "data"}', 'ttl' => 300],
435+
];
436+
437+
$export_keys_array = [];
438+
foreach ($keys_to_test as $key => $data) {
439+
$this->redis->set($key, $data['value']);
440+
if ($data['ttl'] > 0) {
441+
$this->redis->expire($key, $data['ttl']);
442+
}
443+
$export_keys_array[] = ['key' => $key, 'info' => ['ttl' => $this->redis->ttl($key)]];
444+
}
445+
446+
$exported_json = Helpers::export(
447+
$export_keys_array,
448+
'redis_backup',
449+
fn (string $key): string => bin2hex($this->redis->dump($key)),
450+
true
451+
);
452+
453+
$this->redis->flushDatabase();
454+
foreach (array_keys($keys_to_test) as $key) {
455+
$this->assertSame(0, $this->redis->exists($key));
456+
}
457+
458+
$tmp_file_path = tempnam(sys_get_temp_dir(), 'pu-');
459+
file_put_contents($tmp_file_path, $exported_json);
460+
461+
$_FILES['import'] = [
462+
'name' => 'test_import.json',
463+
'type' => 'application/json',
464+
'tmp_name' => $tmp_file_path,
465+
'error' => UPLOAD_ERR_OK,
466+
'size' => filesize($tmp_file_path),
467+
];
468+
469+
Http::stopRedirect();
470+
471+
Helpers::import(
472+
function (string $key): bool {
473+
$exists = $this->redis->exists($key);
474+
475+
return is_int($exists) && $exists > 0;
476+
},
477+
function (string $key, string $value, int $ttl): bool {
478+
return $this->redis->restoreKeys($key, $ttl * 1000, hex2bin($value));
479+
}
480+
);
481+
482+
foreach ($keys_to_test as $key => $data) {
483+
$this->assertSame(1, $this->redis->exists($key));
484+
$this->assertSame($data['value'], $this->redis->get($key));
485+
486+
$restored_ttl = $this->redis->ttl($key);
487+
if ($data['ttl'] === -1) {
488+
$this->assertSame(-1, $restored_ttl);
489+
} else {
490+
$this->assertGreaterThan(0, $restored_ttl);
491+
$this->assertLessThanOrEqual($data['ttl'], $restored_ttl);
492+
}
493+
}
494+
495+
unlink($tmp_file_path);
496+
unset($_FILES['import']);
497+
}
426498
}

0 commit comments

Comments
 (0)