Skip to content

Commit

Permalink
next (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
fezfez committed May 8, 2023
1 parent 9bdfd5d commit fc5427c
Show file tree
Hide file tree
Showing 9 changed files with 330 additions and 30 deletions.
10 changes: 5 additions & 5 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.2"
"symfony/process": "^6.2.10",
"league/flysystem": "^3.15.1"
},
"require-dev": {
"doctrine/coding-standard": "^11.1",
"infection/infection": "^0.26.18",
"phpunit/phpunit": "^9.5.27",
"doctrine/coding-standard": "^12.0",
"phpunit/phpunit": "^10.1.2",
"psalm/plugin-phpunit": "^0.18.4",
"vimeo/psalm": "^5.6"
"vimeo/psalm": "^5.11"
},
"config": {
"allow-plugins": {
Expand Down
11 changes: 0 additions & 11 deletions infection.json

This file was deleted.

19 changes: 7 additions & 12 deletions phpunit.xml
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>
2 changes: 2 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
findUnusedBaselineEntry="true"
findUnusedCode="false"
>
<projectFiles>
<directory name="src" />
Expand Down
49 changes: 49 additions & 0 deletions src/Filesystems/League/LeagueFilesystemAdapterV3.php
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);
}
}
}
56 changes: 56 additions & 0 deletions src/Filesystems/League/LeagueLocalFilesystemAdapterV3.php
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);
}
}
}
4 changes: 2 additions & 2 deletions tests/Compressors/GzipCompressorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
final class GzipCompressorTest extends TestCase
{
/** @return iterable<int, array<string>> */
public function provideCompress(): iterable
public static function provideCompress(): iterable
{
yield ['foo', "gzip 'foo'", 'foo.gz'];
yield ['../foo', "gzip '../foo'", '../foo.gz'];
Expand All @@ -32,7 +32,7 @@ public function testCompress(string $actual, string $expected, string $path): vo
}

/** @return iterable<int, array<string>> */
public function provideDecompress(): iterable
public static function provideDecompress(): iterable
{
yield ['foo.gz', "gzip -d 'foo.gz'", 'foo'];
yield ['../foo.gz', "gzip -d '../foo.gz'", '../foo'];
Expand Down
104 changes: 104 additions & 0 deletions tests/Filesystems/League/LeagueFilesystemAdapaterV3Test.php
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');
}
}

0 comments on commit fc5427c

Please sign in to comment.