Skip to content

Commit

Permalink
Merge pull request #57 from Admiral-Enigma/feature-exists-function
Browse files Browse the repository at this point in the history
Add a function to check if the temporary directory exist without catching PathAlreadyExists exception
  • Loading branch information
freekmurze authored Aug 23, 2022
2 parents 6ccf471 + 09eaa3e commit e2818d8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/TemporaryDirectory.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function create(): self
$this->deleteDirectory($this->getFullPath());
}

if (file_exists($this->getFullPath())) {
if ($this->exists()) {
throw PathAlreadyExists::create($this->getFullPath());
}

Expand Down Expand Up @@ -99,6 +99,11 @@ public function delete(): bool
return $this->deleteDirectory($this->getFullPath());
}

public function exists(): bool
{
return file_exists($this->getFullPath());
}

protected function getFullPath(): string
{
return $this->location.(! empty($this->name) ? DIRECTORY_SEPARATOR.$this->name : '');
Expand Down
13 changes: 13 additions & 0 deletions tests/TemporaryDirectoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,19 @@ public function it_should_return_true_on_deleted_file_is_not_existed()
$this->assertTrue($temporaryDirectory);
}

/** @test */
public function it_exists_function_should_tell_if_directory_exists()
{
$temporaryDirectory = (new TemporaryDirectory())
->name($this->temporaryDirectory);

$this->assertFalse($temporaryDirectory->exists());

$temporaryDirectory->create();

$this->assertTrue($temporaryDirectory->exists());
}

protected function deleteDirectory(string $path): bool
{
if (is_link($path)) {
Expand Down

0 comments on commit e2818d8

Please sign in to comment.