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