Skip to content

Commit

Permalink
update deps + add new type on local FS
Browse files Browse the repository at this point in the history
  • Loading branch information
fezfez committed Jan 6, 2023
1 parent 6ebaad0 commit 1576807
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 26 deletions.
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
],
"require": {
"php": "~8.1.0 || ~8.2.0",
"symfony/process": "^v5.4.8 || ^6.0.8"
"symfony/process": "^v5.4.8 || ^6.2"
},
"require-dev": {
"doctrine/coding-standard": "^10.0",
"infection/infection": "^0.26.15",
"phpunit/phpunit": "^9.5.25",
"psalm/plugin-phpunit": "^0.17.0",
"vimeo/psalm": "^4.29.0"
"doctrine/coding-standard": "^11.1",
"infection/infection": "^0.26.16",
"phpunit/phpunit": "^9.5.27",
"psalm/plugin-phpunit": "^0.18.4",
"vimeo/psalm": "^5.4"
},
"config": {
"allow-plugins": {
Expand Down
19 changes: 15 additions & 4 deletions src/BackupManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Fezfez\BackupManager\Databases\Database;
use Fezfez\BackupManager\Filesystems\BackupManagerFilesystemAdapter;
use Fezfez\BackupManager\Filesystems\Destination;
use Fezfez\BackupManager\Filesystems\LocalFilesystemAdapter;
use Fezfez\BackupManager\Procedures\Backup;
use Fezfez\BackupManager\Procedures\BackupProcedure;
use Fezfez\BackupManager\Procedures\Restore;
Expand All @@ -33,13 +34,23 @@ public function __construct(
}

/** @param Destination[] $destinations */
public function backup(BackupManagerFilesystemAdapter $localFileSystem, Database $database, array $destinations, string $localTmpPath, Compressor ...$compression): void
{
public function backup(
LocalFilesystemAdapter $localFileSystem,
Database $database,
array $destinations,
string $localTmpPath,
Compressor ...$compression,
): void {
$this->backupProcedure->__invoke($localFileSystem, $database, $destinations, $localTmpPath, ...$compression);
}

public function restore(BackupManagerFilesystemAdapter $localFileSystem, BackupManagerFilesystemAdapter $to, string $sourcePath, Database $databaseName, Compressor ...$compression): void
{
public function restore(
LocalFilesystemAdapter $localFileSystem,
BackupManagerFilesystemAdapter $to,
string $sourcePath,
Database $databaseName,
Compressor ...$compression,
): void {
$this->restoreProcedure->__invoke($localFileSystem, $to, $sourcePath, $databaseName, ...$compression);
}
}
17 changes: 15 additions & 2 deletions src/BackupManagerContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,24 @@
use Fezfez\BackupManager\Databases\Database;
use Fezfez\BackupManager\Filesystems\BackupManagerFilesystemAdapter;
use Fezfez\BackupManager\Filesystems\Destination;
use Fezfez\BackupManager\Filesystems\LocalFilesystemAdapter;

interface BackupManagerContract
{
/** @param Destination[] $destinations */
public function backup(BackupManagerFilesystemAdapter $localFileSystem, Database $database, array $destinations, string $localTmpPath, Compressor ...$compression): void;
public function backup(
LocalFilesystemAdapter $localFileSystem,
Database $database,
array $destinations,
string $localTmpPath,
Compressor ...$compression,
): void;

public function restore(BackupManagerFilesystemAdapter $localFileSystem, BackupManagerFilesystemAdapter $to, string $sourcePath, Database $databaseName, Compressor ...$compression): void;
public function restore(
LocalFilesystemAdapter $localFileSystem,
BackupManagerFilesystemAdapter $to,
string $sourcePath,
Database $databaseName,
Compressor ...$compression,
): void;
}
10 changes: 10 additions & 0 deletions src/Filesystems/LocalFilesystemAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Fezfez\BackupManager\Filesystems;

interface LocalFilesystemAdapter extends BackupManagerFilesystemAdapter
{
public function getRootPath(): string;
}
11 changes: 8 additions & 3 deletions src/Procedures/Backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

use Fezfez\BackupManager\Compressors\Compressor;
use Fezfez\BackupManager\Databases\Database;
use Fezfez\BackupManager\Filesystems\BackupManagerFilesystemAdapter;
use Fezfez\BackupManager\Filesystems\Destination;
use Fezfez\BackupManager\Filesystems\LocalFilesystemAdapter;
use Fezfez\BackupManager\ShellProcessing\ShellProcessor;
use Symfony\Component\Process\Process;

Expand All @@ -25,8 +25,13 @@ public function __construct(ShellProcessor|null $shellProcessor = null)
}

/** @param Destination[] $destinations */
public function __invoke(BackupManagerFilesystemAdapter $localFileSystem, Database $database, array $destinations, string $localTmpPath, Compressor ...$compressorList): void
{
public function __invoke(
LocalFilesystemAdapter $localFileSystem,
Database $database,
array $destinations,
string $localTmpPath,
Compressor ...$compressorList,
): void {
$tmpPath = sprintf('%s/%s', $localTmpPath, uniqid());

$this->shellProcessor->__invoke(Process::fromShellCommandline($database->getDumpCommandLine($tmpPath)));
Expand Down
10 changes: 8 additions & 2 deletions src/Procedures/BackupProcedure.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@

use Fezfez\BackupManager\Compressors\Compressor;
use Fezfez\BackupManager\Databases\Database;
use Fezfez\BackupManager\Filesystems\BackupManagerFilesystemAdapter;
use Fezfez\BackupManager\Filesystems\Destination;
use Fezfez\BackupManager\Filesystems\LocalFilesystemAdapter;

interface BackupProcedure
{
/** @param Destination[] $destinations */
public function __invoke(BackupManagerFilesystemAdapter $localFileSystem, Database $database, array $destinations, string $localTmpPath, Compressor ...$compressor): void;
public function __invoke(
LocalFilesystemAdapter $localFileSystem,
Database $database,
array $destinations,
string $localTmpPath,
Compressor ...$compressor,
): void;
}
12 changes: 9 additions & 3 deletions src/Procedures/Restore.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Fezfez\BackupManager\Compressors\Compressor;
use Fezfez\BackupManager\Databases\Database;
use Fezfez\BackupManager\Filesystems\BackupManagerFilesystemAdapter;
use Fezfez\BackupManager\Filesystems\LocalFilesystemAdapter;
use Fezfez\BackupManager\ShellProcessing\ShellProcessor;
use Symfony\Component\Process\Process;

Expand All @@ -23,14 +24,19 @@ public function __construct(ShellProcessor|null $shellProcessor = null)
$this->shellProcessor = $shellProcessor ?? new ShellProcessor();
}

public function __invoke(BackupManagerFilesystemAdapter $localFileSystem, BackupManagerFilesystemAdapter $to, string $sourcePath, Database $databaseName, Compressor ...$compressorList): void
{
public function __invoke(
LocalFilesystemAdapter $localFileSystem,
BackupManagerFilesystemAdapter $to,
string $sourcePath,
Database $databaseName,
Compressor ...$compressorList,
): void {
// begin the life of a new working file
$workingFile = sprintf('%s/%s', basename($sourcePath), uniqid());

// download or retrieve the archived backup file

$to->writeStream(basename($workingFile), $localFileSystem->readStream($sourcePath));
$localFileSystem->writeStream(basename($workingFile), $to->readStream($sourcePath));

// decompress the archived backup
foreach ($compressorList as $compressor) {
Expand Down
9 changes: 8 additions & 1 deletion src/Procedures/RestoreProcedure.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@
use Fezfez\BackupManager\Compressors\Compressor;
use Fezfez\BackupManager\Databases\Database;
use Fezfez\BackupManager\Filesystems\BackupManagerFilesystemAdapter;
use Fezfez\BackupManager\Filesystems\LocalFilesystemAdapter;

interface RestoreProcedure
{
public function __invoke(BackupManagerFilesystemAdapter $localFileSystem, BackupManagerFilesystemAdapter $to, string $sourcePath, Database $databaseName, Compressor ...$compression): void;
public function __invoke(
LocalFilesystemAdapter $localFileSystem,
BackupManagerFilesystemAdapter $to,
string $sourcePath,
Database $databaseName,
Compressor ...$compression,
): void;
}
3 changes: 2 additions & 1 deletion tests/BackupManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Fezfez\BackupManager\Databases\Database;
use Fezfez\BackupManager\Filesystems\BackupManagerFilesystemAdapter;
use Fezfez\BackupManager\Filesystems\Destination;
use Fezfez\BackupManager\Filesystems\LocalFilesystemAdapter;
use Fezfez\BackupManager\Procedures\BackupProcedure;
use Fezfez\BackupManager\Procedures\RestoreProcedure;
use PHPUnit\Framework\TestCase;
Expand All @@ -20,7 +21,7 @@ public function testCreateABackupProcedure(): void
$backupProcedure = $this->createMock(BackupProcedure::class);
$restoreProcedure = $this->createMock(RestoreProcedure::class);
$destination = $this->createMock(Destination::class);
$local = $this->createMock(BackupManagerFilesystemAdapter::class);
$local = $this->createMock(LocalFilesystemAdapter::class);
$to = $this->createMock(BackupManagerFilesystemAdapter::class);
$database = $this->createMock(Database::class);
$compressor = $this->createMock(Compressor::class);
Expand Down
3 changes: 2 additions & 1 deletion tests/Procedures/BackupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Fezfez\BackupManager\Filesystems\BackupManagerFilesystemAdapter;
use Fezfez\BackupManager\Filesystems\BackupManagerRessource;
use Fezfez\BackupManager\Filesystems\Destination;
use Fezfez\BackupManager\Filesystems\LocalFilesystemAdapter;
use Fezfez\BackupManager\Procedures\Backup;
use Fezfez\BackupManager\ShellProcessing\ShellProcessor;
use PHPUnit\Framework\TestCase;
Expand All @@ -19,7 +20,7 @@ final class BackupTest extends TestCase
public function testOk(): void
{
$shellProcessor = $this->createMock(ShellProcessor::class);
$local = $this->createMock(BackupManagerFilesystemAdapter::class);
$local = $this->createMock(LocalFilesystemAdapter::class);
$to = $this->createMock(BackupManagerFilesystemAdapter::class);
$database = $this->createMock(Database::class);
$compressor = $this->createMock(Compressor::class);
Expand Down
7 changes: 4 additions & 3 deletions tests/Procedures/RestoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Fezfez\BackupManager\Databases\Database;
use Fezfez\BackupManager\Filesystems\BackupManagerFilesystemAdapter;
use Fezfez\BackupManager\Filesystems\BackupManagerRessource;
use Fezfez\BackupManager\Filesystems\LocalFilesystemAdapter;
use Fezfez\BackupManager\Procedures\Restore;
use Fezfez\BackupManager\ShellProcessing\ShellProcessor;
use PHPUnit\Framework\TestCase;
Expand All @@ -18,16 +19,16 @@ final class RestoreTest extends TestCase
public function testOk(): void
{
$shellProcessor = $this->createMock(ShellProcessor::class);
$local = $this->createMock(BackupManagerFilesystemAdapter::class);
$local = $this->createMock(LocalFilesystemAdapter::class);
$to = $this->createMock(BackupManagerFilesystemAdapter::class);
$database = $this->createMock(Database::class);
$compressor = $this->createMock(Compressor::class);
$ressource = $this->createMock(BackupManagerRessource::class);

$sUT = new Restore($shellProcessor);

$local->expects(self::once())->method('readStream')->with('toto')->willReturn($ressource);
$to->expects(self::once())->method('writeStream')->with(self::anything(), $ressource);
$local->expects(self::once())->method('writeStream')->with(self::anything(), $ressource);
$to->expects(self::once())->method('readStream')->with('toto')->willReturn($ressource);
$compressor->expects(self::once())->method('decompress')->with(self::anything())->willReturn('toto');
$database->expects(self::once())->method('getRestoreCommandLine')->with('toto')->willReturn('a script');
$shellProcessor->expects(self::once())->method('__invoke')->with(self::callback(static function (Process $process) {
Expand Down

0 comments on commit 1576807

Please sign in to comment.