Skip to content

Commit

Permalink
Merge pull request #18 from Nekland/feat/add-new-exception-while-gett…
Browse files Browse the repository at this point in the history
…ing-content-but-file-removed

feat: improve error management of TemporaryFile
  • Loading branch information
Nek- authored Aug 25, 2021
2 parents e94f808 + 02fe888 commit 46e9918
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 46e9918

Please sign in to comment.