From 50a9a0e1047c21fd347953a1e4519f237ae1621a Mon Sep 17 00:00:00 2001 From: Michael Donat Date: Sun, 11 May 2014 19:37:54 +0100 Subject: [PATCH] fixes #22, changed behaviour of rename --- src/VirtualFileSystem/Container.php | 36 +++++++------ tests/VirtualFileSystem/ContainerTest.php | 61 +++++++++++++---------- 2 files changed, 55 insertions(+), 42 deletions(-) diff --git a/src/VirtualFileSystem/Container.php b/src/VirtualFileSystem/Container.php index 3b1865b..f0099a1 100644 --- a/src/VirtualFileSystem/Container.php +++ b/src/VirtualFileSystem/Container.php @@ -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); diff --git a/tests/VirtualFileSystem/ContainerTest.php b/tests/VirtualFileSystem/ContainerTest.php index 6e2af12..2d54d2c 100644 --- a/tests/VirtualFileSystem/ContainerTest.php +++ b/tests/VirtualFileSystem/ContainerTest.php @@ -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'); }