Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ git:
depth: false
install: composer update --no-interaction --no-suggest --no-progress
script:
- phpdbg -qrr vendor/bin/phpunit --coverage-clover phpunit.coverage.xml --log-junit phpunit.report.xml
- phpdbg -qrr ./vendor/bin/unit-tests
- sonar-scanner
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,7 @@ This method deletes one or many files and throws an exception if error occurs du
Run tests using a command:

```
phpdbg -qrr vendor/bin/phpunit
```

Check the test coverage:

```
phpdbg -qrr vendor/bin/phpunit --coverage-html coverage tests
phpdbg -qrr ./vendor/bin/unit-tests
```

### Docker
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
"autoload": {
"psr-4": {
"Quillstack\\LocalStorage\\": "src/",
"Quillstack\\Tests\\LocalStorage\\": "tests/",
"Quillstack\\Fixtures\\LocalStorage\\": "tests/fixtures/"
"Quillstack\\LocalStorage\\Tests\\": "tests/"
}
},
"require": {
Expand Down
3 changes: 1 addition & 2 deletions sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ sonar.organization=quillstack

sonar.sources=src
sonar.tests=tests
sonar.php.coverage.reportPaths=phpunit.coverage.xml
sonar.php.tests.reportPath=phpunit.report.xml
sonar.php.coverage.reportPaths=unit-tests.coverage.xml

sonar.language=php
sonar.sourceEncoding=UTF-8
Expand Down
12 changes: 10 additions & 2 deletions src/LocalStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,15 @@ public function missing(string $path): bool
public function save(string $path, mixed $contents): bool
{
try {
$savedBytes = file_put_contents($path, $contents);
$savedBytes = @file_put_contents($path, $contents);
} catch (Throwable $exception) {
throw new LocalFileNotSavedException("File not saved: {$path}", LocalStorageException::ERROR_CODE, $exception);
}

if ($savedBytes === false) {
throw new LocalFileNotSavedException("File not saved: {$path}", LocalStorageException::ERROR_CODE);
}

return $savedBytes > 0;
}

Expand Down Expand Up @@ -78,7 +82,7 @@ public function delete(string $path, string ...$more): bool
private function deleteOne(string $path): bool
{
try {
$deleted = unlink($path);
$deleted = @unlink($path);
} catch (Throwable $exception) {
throw new LocalFileNotDeletedException(
"File not deleted: {$path}",
Expand All @@ -87,6 +91,10 @@ private function deleteOne(string $path): bool
);
}

if ($deleted === false) {
throw new LocalFileNotDeletedException("File not deleted: {$path}", LocalStorageException::ERROR_CODE);
}

return $deleted;
}
}
21 changes: 21 additions & 0 deletions tests/DataProviders/FilesToDeleteDataProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Quillstack\LocalStorage\Tests\DataProviders;

use Quillstack\UnitTests\DataProviderInterface;

class FilesToDeleteDataProvider implements DataProviderInterface
{
public function provides(): array
{
return [
['one'],
['two'],
['three'],
['four'],
['five'],
];
}
}
Empty file added tests/Fixtures/empty.txt
Empty file.
1 change: 1 addition & 0 deletions tests/Fixtures/word.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello
136 changes: 0 additions & 136 deletions tests/LocalStorageTest.php

This file was deleted.

126 changes: 126 additions & 0 deletions tests/Unit/TestLocalStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

declare(strict_types=1);

namespace Quillstack\LocalStorage\Tests\Unit;

use Quillstack\LocalStorage\Exceptions\LocalFileNotDeletedException;
use Quillstack\LocalStorage\Exceptions\LocalFileNotExistsException;
use Quillstack\LocalStorage\Exceptions\LocalFileNotSavedException;
use Quillstack\LocalStorage\LocalStorage;
use Quillstack\LocalStorage\Tests\DataProviders\FilesToDeleteDataProvider;
use Quillstack\UnitTests\AssertEmpty;
use Quillstack\UnitTests\AssertEqual;
use Quillstack\UnitTests\AssertExceptions;
use Quillstack\UnitTests\Attributes\ProvidesDataFrom;
use Quillstack\UnitTests\Types\AssertBoolean;

class TestLocalStorage
{
public function __construct(
private LocalStorage $storage,
private AssertExceptions $assertExceptions,
private AssertEqual $assertEqual,
private AssertBoolean $assertBoolean,
private AssertEmpty $assertEmpty
) {
//
}

public function getEmptyFile()
{
$path = dirname(__FILE__) . '/../Fixtures/empty.txt';
$contests = $this->storage->get($path);

$this->assertEmpty->isEmpty($contests);
}

public function getNonEmptyFile()
{
$path = dirname(__FILE__) . '/../Fixtures/word.txt';
$contests = $this->storage->get($path);

$this->assertEqual->equal('hello', $contests);
}

public function saveFile()
{
$path = dirname(__FILE__) . '/../Fixtures/world.txt';
$this->storage->save($path, 'world');
$contests = $this->storage->get($path);
$this->storage->delete($path);

$this->assertEqual->equal('world', $contests);
}

#[ProvidesDataFrom(FilesToDeleteDataProvider::class)]
public function deleteOneFile(string $filename)
{
$path = dirname(__FILE__) . "/../Fixtures/{$filename}.txt";
$this->storage->save($path, $filename);
$contests = $this->storage->get($path);
$deleted = $this->storage->delete($path);

$this->assertEqual->equal($filename, $contests);
$this->assertBoolean->isTrue($deleted);
}

public function deleteManyFiles()
{
$files = [
'one',
'two',
'three',
'four',
'five',
];

foreach ($files as $file) {
$this->storage->save($file, $file);
}

$deleted = $this->storage->delete(...$files);

$this->assertBoolean->isTrue($deleted);
}

public function notExistingFile()
{
$this->assertExceptions->expect(LocalFileNotExistsException::class);

$this->storage->get('not-exists');
}

public function missingFile()
{
$missing = $this->storage->missing('not-exists');
$exists = $this->storage->exists('not-exists');

$this->assertBoolean->isTrue($missing);
$this->assertBoolean->isFalse($exists);
}

public function existsFile()
{
$path = dirname(__FILE__) . '/../Fixtures/empty.txt';
$missing = $this->storage->missing($path);
$exists = $this->storage->exists($path);

$this->assertBoolean->isFalse($missing);
$this->assertBoolean->isTrue($exists);
}

public function notSaved()
{
$this->assertExceptions->expect(LocalFileNotSavedException::class);

$this->storage->save('/dir/not/exists', 'world');
}

public function notDeleted()
{
$this->assertExceptions->expect(LocalFileNotDeletedException::class);

$this->storage->delete('/file-not-exists');
}
}
7 changes: 7 additions & 0 deletions tests/unit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

declare(strict_types=1);

return [
\Quillstack\LocalStorage\Tests\Unit\TestLocalStorage::class,
];
2 changes: 2 additions & 0 deletions unit-tests.coverage.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0"?>
<coverage><project><file name="/var/www/html/src/LocalStorage.php"><line num="20" count="1"/><line num="21" count="1"/><line num="24" count="1"/><line num="32" count="1"/><line num="40" count="1"/><line num="49" count="1"/><line num="50" count="0"/><line num="51" count="0"/><line num="54" count="1"/><line num="55" count="1"/><line num="58" count="1"/><line num="66" count="1"/><line num="68" count="1"/><line num="69" count="1"/><line num="72" count="1"/><line num="85" count="1"/><line num="86" count="0"/><line num="87" count="0"/><line num="88" count="0"/><line num="89" count="0"/><line num="94" count="1"/><line num="95" count="1"/><line num="98" count="1"/></file></project></coverage>