This repository has been archived by the owner on May 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fezfez\BackupManager; | ||
|
||
use Fezfez\BackupManager\Filesystems\BackupManagerRessource; | ||
use Fezfez\BackupManager\Filesystems\CantDeleteFile; | ||
use Fezfez\BackupManager\Filesystems\CantReadFile; | ||
use Fezfez\BackupManager\Filesystems\CantWriteFile; | ||
use Fezfez\BackupManager\Filesystems\LocalFilesystemAdapter; | ||
use League\Flysystem\FilesystemOperator; | ||
use Throwable; | ||
|
||
use function sprintf; | ||
|
||
final class LeagueLocalFilesystemAdapterV3 implements LocalFilesystemAdapter | ||
{ | ||
public function __construct( | ||
private readonly FilesystemOperator $fileSysteme, | ||
private readonly string $rootPath, | ||
) { | ||
} | ||
|
||
public function getRootPath(): string | ||
{ | ||
return $this->rootPath; | ||
} | ||
|
||
public function readStream(string $path): BackupManagerRessource | ||
{ | ||
try { | ||
return new BackupManagerRessource($this->fileSysteme->readStream($path)); | ||
} catch (Throwable $exception) { | ||
throw new CantReadFile(sprintf('cant read file %s', $path), 0, $exception); | ||
} | ||
} | ||
|
||
public function writeStream(string $path, BackupManagerRessource $resource): void | ||
{ | ||
try { | ||
$this->fileSysteme->writeStream($path, $resource->getResource()); | ||
} catch (Throwable $exception) { | ||
throw new CantWriteFile(sprintf('cant write file %s', $path), 0, $exception); | ||
} | ||
} | ||
|
||
public function delete(string $path): void | ||
{ | ||
try { | ||
$this->fileSysteme->delete($path); | ||
} catch (Throwable $exception) { | ||
throw new CantDeleteFile(sprintf('cant delete file %s', $path), 0, $exception); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fezfez\BackupManager\Tests; | ||
|
||
use Exception; | ||
use Fezfez\BackupManager\Filesystems\BackupManagerRessource; | ||
use Fezfez\BackupManager\Filesystems\CantDeleteFile; | ||
use Fezfez\BackupManager\Filesystems\CantReadFile; | ||
use Fezfez\BackupManager\Filesystems\CantWriteFile; | ||
use Fezfez\BackupManager\LeagueLocalFilesystemAdapterV3; | ||
use League\Flysystem\FilesystemOperator; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
use function fopen; | ||
use function fwrite; | ||
use function rewind; | ||
use function stream_get_contents; | ||
|
||
final class LeagueLocalFilesystemAdapaterV3Test extends TestCase | ||
{ | ||
public function testReadStreamOk(): void | ||
{ | ||
$stream = fopen('php://memory', 'r+'); | ||
fwrite($stream, 'my content'); | ||
rewind($stream); | ||
|
||
$filesystem = $this->createMock(FilesystemOperator::class); | ||
$filesystem->expects(self::once())->method('readStream')->with('toto')->willReturn($stream); | ||
$sUT = new LeagueLocalFilesystemAdapterV3($filesystem, 'toto'); | ||
|
||
self::assertSame('my content', stream_get_contents($sUT->readStream('toto')->getResource())); | ||
self::assertSame('toto', $sUT->getRootPath()); | ||
} | ||
|
||
public function testReadStreamFail(): void | ||
{ | ||
$filesystem = $this->createMock(FilesystemOperator::class); | ||
$filesystem->expects(self::once())->method('readStream')->with('toto')->willThrowException(new Exception()); | ||
$sUT = new LeagueLocalFilesystemAdapterV3($filesystem, 'toto'); | ||
|
||
self::expectExceptionCode(0); | ||
self::expectExceptionMessage('cant read file toto'); | ||
self::expectException(CantReadFile::class); | ||
|
||
$sUT->readStream('toto'); | ||
} | ||
|
||
public function testWriteStreamOk(): void | ||
{ | ||
$stream = fopen('php://memory', 'r+'); | ||
fwrite($stream, 'my content'); | ||
rewind($stream); | ||
|
||
$backupManagerRessource = new BackupManagerRessource($stream); | ||
|
||
$filesystem = $this->createMock(FilesystemOperator::class); | ||
$filesystem->expects(self::once())->method('writeStream')->with('toto', $stream); | ||
$sUT = new LeagueLocalFilesystemAdapterV3($filesystem, 'toto'); | ||
|
||
$sUT->writeStream('toto', $backupManagerRessource); | ||
} | ||
|
||
public function testWriteStreamFailOnException(): void | ||
{ | ||
$stream = fopen('php://memory', 'r+'); | ||
fwrite($stream, 'my content'); | ||
rewind($stream); | ||
|
||
$backupManagerRessource = new BackupManagerRessource($stream); | ||
|
||
$filesystem = $this->createMock(FilesystemOperator::class); | ||
$filesystem->expects(self::once())->method('writeStream')->with('toto', $stream)->willThrowException(new Exception()); | ||
$sUT = new LeagueLocalFilesystemAdapterV3($filesystem, 'toto'); | ||
|
||
self::expectExceptionCode(0); | ||
self::expectExceptionMessage('cant write file toto'); | ||
self::expectException(CantWriteFile::class); | ||
|
||
$sUT->writeStream('toto', $backupManagerRessource); | ||
} | ||
|
||
public function testDeleteOk(): void | ||
{ | ||
$filesystem = $this->createMock(FilesystemOperator::class); | ||
$filesystem->expects(self::once())->method('delete')->with('toto'); | ||
$sUT = new LeagueLocalFilesystemAdapterV3($filesystem, 'toto'); | ||
|
||
$sUT->delete('toto'); | ||
} | ||
|
||
public function testDeleteFailOnException(): void | ||
{ | ||
$filesystem = $this->createMock(FilesystemOperator::class); | ||
$filesystem->expects(self::once())->method('delete')->with('toto')->willThrowException(new Exception('tutu')); | ||
$sUT = new LeagueLocalFilesystemAdapterV3($filesystem, 'toto'); | ||
|
||
self::expectExceptionCode(0); | ||
self::expectExceptionMessage('cant delete file toto'); | ||
self::expectException(CantDeleteFile::class); | ||
|
||
$sUT->delete('toto'); | ||
} | ||
} |