Skip to content

Commit dccc645

Browse files
committed
Added helpers for dumping files
1 parent 6fecde8 commit dccc645

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed

src/Action/Dump.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Terminal42\Restic\Action;
6+
7+
use Symfony\Component\Process\Exception\ProcessFailedException;
8+
use Terminal42\Restic\Action\Result\DumpResult;
9+
10+
class Dump extends AbstractAction
11+
{
12+
private DumpResult $result;
13+
14+
public function __construct(
15+
private readonly string $snapshotId,
16+
private readonly string $targetFileName,
17+
private readonly string $pathOrFile,
18+
) {
19+
}
20+
21+
public function getResult(): DumpResult
22+
{
23+
return $this->result;
24+
}
25+
26+
protected function doGetArguments(): array
27+
{
28+
$arguments = [
29+
'dump',
30+
];
31+
32+
// Always use zip as archive mode for paths
33+
$arguments[] = '-a';
34+
$arguments[] = 'zip';
35+
36+
$arguments[] = '--target';
37+
$arguments[] = $this->targetFileName;
38+
39+
$arguments[] = $this->snapshotId;
40+
$arguments[] = $this->pathOrFile;
41+
42+
return $arguments;
43+
}
44+
45+
protected function updateResult(string $output, ProcessFailedException|null $exception = null): void
46+
{
47+
$this->result = new DumpResult($output, $exception);
48+
}
49+
}

src/Action/Result/DumpResult.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Terminal42\Restic\Action\Result;
6+
7+
class DumpResult extends AbstractActionResult
8+
{
9+
}

src/Toolkit.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Symfony\Component\Console\Style\SymfonyStyle;
88
use Terminal42\Restic\Action\CreateBackup;
9+
use Terminal42\Restic\Action\Dump;
910
use Terminal42\Restic\Action\ForgetOldSnapshots;
1011
use Terminal42\Restic\Action\ListFiles;
1112
use Terminal42\Restic\Action\ListSnapshots;
@@ -94,4 +95,14 @@ public function restoreBackup(string $snapshotId, string $restorePath, array $pa
9495

9596
return $action->getResult();
9697
}
98+
99+
/**
100+
* Dumps a file to the given target file name. In case of a path given, it will create a zip archive, so make sure your $targetFileName
101+
* contains ".zip" in that case. If a file was given, it will not compress it, so make sure you use the appropriate target file name.
102+
*/
103+
public function dumpPathOrFile(string $snapshotId, string $pathOrFile, string $targetFileName): void
104+
{
105+
$action = new Dump($snapshotId, $targetFileName, $pathOrFile);
106+
$this->restic->runAction($action);
107+
}
97108
}

tests/Functional/BackupTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,35 @@ public function testForgetsSnapshotsCorrectly(): void
115115
);
116116
}
117117

118+
public function testDump(): void
119+
{
120+
$restorePath = $this->getRestorePath();
121+
$toolkit = $this->createToolkit(self::getFixtureDirectory('regular-backup'));
122+
$snapshotId = $toolkit->createBackup();
123+
124+
// Test dumping a file
125+
$toolkit->dumpPathOrFile($snapshotId, 'src/Controller/LuckyController.php', $restorePath.'/LuckyController.php');
126+
$this->assertFileEquals(
127+
self::getFixtureDirectory('regular-backup').'/src/Controller/LuckyController.php',
128+
$restorePath.'/LuckyController.php',
129+
);
130+
131+
// Test dumping an entire path
132+
$toolkit->dumpPathOrFile($snapshotId, 'src', $restorePath.'/test.zip');
133+
$this->assertFileExists($restorePath.'/test.zip');
134+
135+
// Unarchive the path and assert the contents
136+
$zip = new \ZipArchive();
137+
$zip->open($restorePath.'/test.zip');
138+
$zip->extractTo($restorePath.'/test');
139+
$zip->close();
140+
141+
$this->assertFileEquals(
142+
self::getFixtureDirectory('regular-backup').'/src/Controller/LuckyController.php',
143+
$restorePath.'/test/src/Controller/LuckyController.php',
144+
);
145+
}
146+
118147
#[DataProvider('restoreDataProvider')]
119148
public function testRestore(string $projectDir, array $pathsToRestore, array $expectedRestoredFiles, int $expectedNumberOfFilesRestored, int $expectedTotalBytes): void
120149
{

0 commit comments

Comments
 (0)