From 1576807ae2220158967fb4087da47da5617d0013 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane?= Date: Fri, 6 Jan 2023 15:29:28 +0100 Subject: [PATCH] update deps + add new type on local FS --- composer.json | 12 ++++++------ src/BackupManager.php | 19 +++++++++++++++---- src/BackupManagerContract.php | 17 +++++++++++++++-- src/Filesystems/LocalFilesystemAdapter.php | 10 ++++++++++ src/Procedures/Backup.php | 11 ++++++++--- src/Procedures/BackupProcedure.php | 10 ++++++++-- src/Procedures/Restore.php | 12 +++++++++--- src/Procedures/RestoreProcedure.php | 9 ++++++++- tests/BackupManagerTest.php | 3 ++- tests/Procedures/BackupTest.php | 3 ++- tests/Procedures/RestoreTest.php | 7 ++++--- 11 files changed, 87 insertions(+), 26 deletions(-) create mode 100644 src/Filesystems/LocalFilesystemAdapter.php diff --git a/composer.json b/composer.json index a8964fe..7705408 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/src/BackupManager.php b/src/BackupManager.php index 73d5f87..9e559a6 100644 --- a/src/BackupManager.php +++ b/src/BackupManager.php @@ -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; @@ -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); } } diff --git a/src/BackupManagerContract.php b/src/BackupManagerContract.php index 0b65cd4..05e8536 100644 --- a/src/BackupManagerContract.php +++ b/src/BackupManagerContract.php @@ -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; } diff --git a/src/Filesystems/LocalFilesystemAdapter.php b/src/Filesystems/LocalFilesystemAdapter.php new file mode 100644 index 0000000..0e51431 --- /dev/null +++ b/src/Filesystems/LocalFilesystemAdapter.php @@ -0,0 +1,10 @@ +shellProcessor->__invoke(Process::fromShellCommandline($database->getDumpCommandLine($tmpPath))); diff --git a/src/Procedures/BackupProcedure.php b/src/Procedures/BackupProcedure.php index 34d8018..9241c56 100644 --- a/src/Procedures/BackupProcedure.php +++ b/src/Procedures/BackupProcedure.php @@ -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; } diff --git a/src/Procedures/Restore.php b/src/Procedures/Restore.php index e394d57..d68959e 100644 --- a/src/Procedures/Restore.php +++ b/src/Procedures/Restore.php @@ -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; @@ -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) { diff --git a/src/Procedures/RestoreProcedure.php b/src/Procedures/RestoreProcedure.php index 50add9a..b297990 100644 --- a/src/Procedures/RestoreProcedure.php +++ b/src/Procedures/RestoreProcedure.php @@ -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; } diff --git a/tests/BackupManagerTest.php b/tests/BackupManagerTest.php index 815dfca..c88366a 100644 --- a/tests/BackupManagerTest.php +++ b/tests/BackupManagerTest.php @@ -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; @@ -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); diff --git a/tests/Procedures/BackupTest.php b/tests/Procedures/BackupTest.php index abfec21..6018165 100644 --- a/tests/Procedures/BackupTest.php +++ b/tests/Procedures/BackupTest.php @@ -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; @@ -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); diff --git a/tests/Procedures/RestoreTest.php b/tests/Procedures/RestoreTest.php index a78aedd..deea5d3 100644 --- a/tests/Procedures/RestoreTest.php +++ b/tests/Procedures/RestoreTest.php @@ -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; @@ -18,7 +19,7 @@ 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); @@ -26,8 +27,8 @@ public function testOk(): void $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) {