Skip to content

Commit

Permalink
add test on destination existence in move method
Browse files Browse the repository at this point in the history
  • Loading branch information
olinox14 committed Apr 2, 2024
1 parent 268678d commit 0422e2a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,7 @@ Path-php is under the [MIT](http://opensource.org/licenses/MIT) licence.

* [x] add an '$erase' argument to the copy method, which default to true
* [x] fix the copy method when $follow_symlinks is false and source is a symlink
* [ ] add 'ignore' and 'errorOnExistingDestination' to the copyTree method
* [ ] move: test if destination exists after it has been computed
* [x] move: test if destination exists after it has been computed
* [ ] study the interest of implementing a 'mergeTree' method
* [ ] review copyTree performances

Expand All @@ -191,4 +190,5 @@ Path-php is under the [MIT](http://opensource.org/licenses/MIT) licence.
* [ ] multi os compat (windows)
* [ ] handle protocols (ftp, sftp, file, smb, http, ...etc)
* [ ] handle unc paths (windows)
* [ ] improve error management and tracebacks
* [ ] improve error management and tracebacks
* [ ] add 'ignore' and 'errorOnExistingDestination' to the copyTree method
7 changes: 4 additions & 3 deletions src/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,6 @@ public function copy(string|self $destination, bool $follow_symlinks = false, bo
* @throws FileExistsException If the destination path or directory already exists.
* @throws FileNotFoundException If the source file or directory does not exist.
* @throws IOException
* TODO: implement an 'ignore' callback property or an 'ignorePattern' property
* TODO: implement a 'errorOnExistingDestination' property (default: True)
*/
public function copyTree(string|self $destination, bool $follow_symlinks = false): self
{
Expand Down Expand Up @@ -561,6 +559,7 @@ public function copyTree(string|self $destination, bool $follow_symlinks = false
* @return Path
* @throws IOException
* @throws FileNotFoundException
* @throws FileExistsException
*/
public function move(string|self $destination): self
{
Expand All @@ -574,7 +573,9 @@ public function move(string|self $destination): self
$destination = $destination->append($this->basename());
}

// TODO: test here if destination exists
if ($destination->exists()) {
throw new FileExistsException('File or directory already exists at ' . $destination->path());
}

$success = $this->builtin->rename($this->path, $destination->path());

Expand Down
34 changes: 34 additions & 0 deletions tests/unit/PathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1608,6 +1608,40 @@ public function testMoveWithError()
$path->move($destination);
}


/**
* @throws IOException|FileNotFoundException
* @throws FileExistsException
*/
public function testMoveTargetExist()
{
$path = $this->getMock('foo.ext', 'move');
$path->method('exists')->willReturn(True);
$path->method('basename')->willReturn('foo.ext');

$destination = "/bar";

$newPath = $this->getMockBuilder(TestablePath::class)->disableOriginalConstructor()->getMock();
$newPath->method('isDir')->willReturn(True);

$newDest = $destination . "/foo.ext";
$extendedNewPath = $this->getMockBuilder(TestablePath::class)->disableOriginalConstructor()->getMock();
$extendedNewPath->method('path')->willReturn($newDest);
$extendedNewPath->method('exists')->willReturn(true);

$newPath->method('append')->with('foo.ext')->willReturn($extendedNewPath);

$path->method('cast')->with($destination)->willReturn($newPath);

$this->builtin
->expects(self::never())
->method('rename');

$this->expectException(FileExistsException::class);

$result = $path->move($destination);
}

/**
* @throws IOException
*/
Expand Down

0 comments on commit 0422e2a

Please sign in to comment.