@@ -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