forked from backup-manager/backup-manager
-
Notifications
You must be signed in to change notification settings - Fork 1
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
9 changed files
with
330 additions
and
30 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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,19 +1,14 @@ | ||
<?xml version="1.0"?> | ||
<phpunit | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
bootstrap="./vendor/autoload.php" | ||
colors="true" | ||
backupGlobals="false" | ||
verbose="true" | ||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"> | ||
<coverage includeUncoveredFiles="true"> | ||
<include> | ||
<directory suffix=".php">src/</directory> | ||
</include> | ||
</coverage> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="./vendor/autoload.php" colors="true" backupGlobals="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd" cacheDirectory=".phpunit.cache"> | ||
<coverage includeUncoveredFiles="true"/> | ||
<testsuites> | ||
<testsuite name="General"> | ||
<directory>tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
<source> | ||
<include> | ||
<directory suffix=".php">src/</directory> | ||
</include> | ||
</source> | ||
</phpunit> |
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
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,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fezfez\BackupManager\Filesystems\League; | ||
|
||
use Fezfez\BackupManager\Filesystems\BackupManagerFilesystemAdapter; | ||
use Fezfez\BackupManager\Filesystems\BackupManagerRessource; | ||
use Fezfez\BackupManager\Filesystems\CantDeleteFile; | ||
use Fezfez\BackupManager\Filesystems\CantReadFile; | ||
use Fezfez\BackupManager\Filesystems\CantWriteFile; | ||
use League\Flysystem\FilesystemOperator; | ||
use Throwable; | ||
|
||
use function sprintf; | ||
|
||
final class LeagueFilesystemAdapterV3 implements BackupManagerFilesystemAdapter | ||
{ | ||
public function __construct(private readonly FilesystemOperator $fileSysteme) | ||
{ | ||
} | ||
|
||
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,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fezfez\BackupManager\Filesystems\League; | ||
|
||
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
104 changes: 104 additions & 0 deletions
104
tests/Filesystems/League/LeagueFilesystemAdapaterV3Test.php
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,104 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fezfez\BackupManager\Tests\Filesystems\League; | ||
|
||
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\Filesystems\League\LeagueFilesystemAdapterV3; | ||
use League\Flysystem\FilesystemOperator; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
use function fopen; | ||
use function fwrite; | ||
use function rewind; | ||
use function stream_get_contents; | ||
|
||
final class LeagueFilesystemAdapaterV3Test 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 LeagueFilesystemAdapterV3($filesystem); | ||
|
||
self::assertSame('my content', stream_get_contents($sUT->readStream('toto')->getResource())); | ||
} | ||
|
||
public function testReadStreamFail(): void | ||
{ | ||
$filesystem = $this->createMock(FilesystemOperator::class); | ||
$filesystem->expects(self::once())->method('readStream')->with('toto')->willThrowException(new Exception()); | ||
$sUT = new LeagueFilesystemAdapterV3($filesystem); | ||
|
||
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 LeagueFilesystemAdapterV3($filesystem); | ||
|
||
$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 LeagueFilesystemAdapterV3($filesystem); | ||
|
||
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 LeagueFilesystemAdapterV3($filesystem); | ||
|
||
$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 LeagueFilesystemAdapterV3($filesystem); | ||
|
||
self::expectExceptionCode(0); | ||
self::expectExceptionMessage('cant delete file toto'); | ||
self::expectException(CantDeleteFile::class); | ||
|
||
$sUT->delete('toto'); | ||
} | ||
} |
Oops, something went wrong.