Skip to content
This repository has been archived by the owner on May 8, 2023. It is now read-only.

Commit

Permalink
Implements local fs
Browse files Browse the repository at this point in the history
  • Loading branch information
fezfez committed Jan 6, 2023
1 parent e0f65bc commit 20bbcb9
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/LeagueLocalFilesystemAdapterV3.php
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);
}
}
}
105 changes: 105 additions & 0 deletions tests/LeagueLocalFilesystemAdapaterV3Test.php
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');
}
}

0 comments on commit 20bbcb9

Please sign in to comment.