Skip to content

Commit

Permalink
feat: improve error management of TemporaryFile
Browse files Browse the repository at this point in the history
If a file has been removed, the error is now more clear.
  • Loading branch information
Nek- committed Aug 25, 2021
1 parent e94f808 commit 02fe888
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Utils/Tempfile/TemporaryFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace Nekland\Utils\Tempfile;

use Nekland\Utils\Exception\LogicException;
use Nekland\Utils\Exception\RuntimeException;
use Nekland\Utils\Tempfile\Exception\CannotCreateFileException;
use Nekland\Utils\Tempfile\Exception\ImpossibleToUpdateFileException;
Expand Down Expand Up @@ -89,6 +90,10 @@ public function hasBeenRemoved()
*/
public function getContent()
{
if ($this->removed) {
throw new LogicException('The file has been removed definitely and cannot be accessed anymore');
}

$content = \file_get_contents($this->file);

if ($content === false) {
Expand Down
10 changes: 10 additions & 0 deletions tests/Nekland/Utils/Tempfile/TemporaryFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Nekland\Utils\Test\Tempfile;

use Nekland\Utils\Exception\LogicException;
use Nekland\Utils\Tempfile\TemporaryFile;
use PHPUnit\Framework\TestCase;

Expand All @@ -27,4 +28,13 @@ public function testItSetContentsAndRetrieveIt()
$file->setContent('foobar');
$this->assertEquals('foobar', $file->getContent());
}

public function testItCannotGetContentOfRemovedFile()
{
$this->expectException(LogicException::class);
$file = new TemporaryFile();
$file->setContent('hello');
$file->remove();
$file->getContent();
}
}

0 comments on commit 02fe888

Please sign in to comment.