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
36 changes: 20 additions & 16 deletions src/VirtualFileSystem/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,32 +212,36 @@ public function createStructure(array $structure, $parent = '/') {
public function move($from, $to)
{
$fromNode = $this->fileAt($from);
$toNode = $this->fileAt(dirname($to));
$newNodeName = basename($to);

try {
$nodeAtToPath = $this->fileAt($to);
if ($nodeAtToPath instanceof Directory) {
$newNodeName = basename($from);
$toNode = $nodeAtToPath;
$nodeToOverride = $this->fileAt($to);

if(!is_a($nodeToOverride, get_class($fromNode))) {
//nodes of a different type
throw new \RuntimeException('Can\'t move.');
}

if($nodeToOverride instanceof Directory) {
if($nodeToOverride->size()) {
//nodes of a different type
throw new \RuntimeException('Can\'t override non empty directory.');
}
}

$this->remove($to, true);

} catch (NotFoundException $e) {
$nodeAtToPath = null;

}

$fromNode->setBasename($newNodeName);
$toParent = $this->fileAt(dirname($to));

if ($nodeAtToPath instanceof File) {
if (!$fromNode instanceof File) {
throw new \RuntimeException('Can\'t move directory onto a file');
}
$toNode->remove($nodeAtToPath->basename());
}
$fromNode->setBasename(basename($to));

if ($fromNode instanceof File) {
$toNode->addFile($fromNode);
$toParent->addFile($fromNode);
} else {
$toNode->addDirectory($fromNode);
$toParent->addDirectory($fromNode);
}

$this->remove($from, true);
Expand Down
61 changes: 35 additions & 26 deletions tests/VirtualFileSystem/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,56 +159,65 @@ public function testMovingToDifferentParent()
$this->assertTrue($container->hasFileAt('/dir2/dirMoved/file'), 'Directory child of type File moved');
}

public function testMovingOntoDirectoryMovesIntoThatDirectory()
public function testMovingFileOntoExistingFileOverridesTarget()
{
$container = new Container(new Factory());
$container->root()->addDirectory($dir = new Directory('dir1'));
$container->root()->addDirectory(new Directory('dir2'));
$dir->addDirectory(new Directory('dir11'));
$dir->addDirectory(new Directory('dir12'));
$dir->addFile(new File('file'));
$container->createFile('/file1', 'file1');
$container->createFile('/file2', 'file2');

$container->move('/dir1', '/dir2');
$container->move('/file1', '/file2');

$this->assertTrue($container->hasFileAt('/dir2'), 'Other parent directories not moved');
$this->assertTrue($container->hasFileAt('/dir2/dir1'), 'Directory moved to new location');
$this->assertFalse($container->hasFileAt('/dir1'), 'Directory does not exist at old location');
$this->assertTrue($container->hasFileAt('/dir2/dir1/dir11'), 'Directory child of type Dir moved');
$this->assertTrue($container->hasFileAt('/dir2/dir1/file'), 'Directory child of type File moved');
$this->assertEquals('/dir2/dir1', $dir->path(), 'Moved dir has correct ancestors.');
$this->assertTrue($container->hasFileAt('/file2'));
$this->assertFalse($container->hasFileAt('/file1'));
$this->assertEquals('file1', $container->fileAt('/file2')->data());
}

$container->move('/dir2/dir1/file', '/dir2/');
public function testMovingDirectoryOntoExistingDirectoryOverridesTarget()
{
$container = new Container(new Factory());
$container->createDir('/dir1');
$container->createDir('/dir2');

$this->assertTrue($container->hasFileAt('/dir2/file'));
$container->move('/dir1', '/dir2');

$container->move('/dir2/file', '/');
$this->assertTrue($container->hasFileAt('/dir2'));
$this->assertFalse($container->hasFileAt('/dir1'));
}

public function testMovingNonEmptyDirectoryOntoExistingDirectoryFails()
{
$container = new Container(new Factory());
$container->createDir('/dir1');
$container->createDir('/dir2');
$container->createFile('/dir2/file1', 'file');

$this->assertTrue($container->hasFileAt('/file'));
$this->setExpectedException('\RuntimeException', 'Can\'t override non empty directory.');

$container->move('/dir1', '/dir2');

}

public function testMovingFileOntoExistingFileOverridesTarget()
public function testMovingDirectoryOntoExistingFileThrows()
{
$container = new Container(new Factory());
$container->createFile('/file1', 'file1');
$container->createDir('/dir1');
$container->createFile('/file2', 'file2');

$container->move('/file1', '/file2');
$this->setExpectedException('\RuntimeException', 'Can\'t move.');

$container->move('/dir1', '/file2');

$this->assertTrue($container->hasFileAt('/file2'));
$this->assertFalse($container->hasFileAt('/file1'));
$this->assertEquals('file1', $container->fileAt('/file2')->data());
}

public function testMovingDirectoryOntoExistingFileThrows()
public function testMovingFileOntoExistingDirectoryThrows()
{
$container = new Container(new Factory());
$container->createDir('/dir1');
$container->createFile('/file2', 'file2');

$this->setExpectedException('\RuntimeException', 'Can\'t move directory onto a file');
$this->setExpectedException('\RuntimeException', 'Can\'t move.');

$container->move('/dir1', '/file2');
$container->move('/file2', '/dir1');

}

Expand Down