From 4f463ed26efd675a1f6cd17fa5320d59c7afebea Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Fri, 2 Oct 2020 21:18:13 +0200 Subject: [PATCH] impl. local directory tests --- System/File/ContainerInterface.php | 9 ++ System/File/Ftp/Directory.php | 6 +- System/File/Ftp/FileAbstract.php | 8 ++ System/File/Local/Directory.php | 45 ++++++--- System/File/Local/FileAbstract.php | 8 ++ tests/System/File/Local/DirectoryTest.php | 118 +++++++++++++++++----- 6 files changed, 151 insertions(+), 43 deletions(-) diff --git a/System/File/ContainerInterface.php b/System/File/ContainerInterface.php index c67c11623..248a4b3d3 100644 --- a/System/File/ContainerInterface.php +++ b/System/File/ContainerInterface.php @@ -59,6 +59,15 @@ interface ContainerInterface */ public function getName() : string; + /** + * Get base name of the resource incl. extension if available. + * + * @return string + * + * @since 1.0.0 + */ + public function getBasename() : string; + /** * Get absolute path of the resource. * diff --git a/System/File/Ftp/Directory.php b/System/File/Ftp/Directory.php index 0c60d85f8..78b4c7cc4 100644 --- a/System/File/Ftp/Directory.php +++ b/System/File/Ftp/Directory.php @@ -548,9 +548,9 @@ class Directory extends FileAbstract implements DirectoryInterface, FtpContainer */ public function addNode(ContainerInterface $node) : self { - $this->count += $node->getCount(); - $this->size += $node->getSize(); - $this->nodes[$node->getName()] = $node; + $this->count += $node->getCount(); + $this->size += $node->getSize(); + $this->nodes[$node->getBasename()] = $node; $node->createNode(); diff --git a/System/File/Ftp/FileAbstract.php b/System/File/Ftp/FileAbstract.php index e82348366..58dddb6f8 100644 --- a/System/File/Ftp/FileAbstract.php +++ b/System/File/Ftp/FileAbstract.php @@ -132,6 +132,14 @@ abstract class FileAbstract implements ContainerInterface return $this->name; } + /** + * {@inheritdoc} + */ + public function getBasename() : string + { + return \basename($this->path); + } + /** * {@inheritdoc} */ diff --git a/System/File/Local/Directory.php b/System/File/Local/Directory.php index 7a0765fdc..980fe2f00 100644 --- a/System/File/Local/Directory.php +++ b/System/File/Local/Directory.php @@ -168,9 +168,9 @@ final class Directory extends FileAbstract implements DirectoryInterface, LocalC */ public function addNode(ContainerInterface $node) : self { - $this->count += $node->getCount(); - $this->size += $node->getSize(); - $this->nodes[$node->getName()] = $node; + $this->count += $node->getCount(); + $this->size += $node->getSize(); + $this->nodes[$node->getBasename()] = $node; $node->createNode(); @@ -446,6 +446,8 @@ final class Directory extends FileAbstract implements DirectoryInterface, LocalC */ public function getNode(string $name) : ?ContainerInterface { + $name = isset($this->nodes[$name]) ? $name : $this->path . '/' . $name; + if (isset($this->nodes[$name]) && $this->nodes[$name] instanceof self) { $this->nodes[$name]->index(); } @@ -453,6 +455,22 @@ final class Directory extends FileAbstract implements DirectoryInterface, LocalC return $this->nodes[$name] ?? null; } + /** + * Check if the child node exists + * + * @param string $name Child node name + * + * @return bool + * + * @since 1.0.0 + */ + public function isExisting(string $name) : bool + { + $name = isset($this->nodes[$name]) ? $name : $this->path . '/' . $name; + + return isset($this->nodes[$name]); + } + /** * Create directory * @@ -542,7 +560,7 @@ final class Directory extends FileAbstract implements DirectoryInterface, LocalC */ public function offsetSet($offset, $value) : void { - if ($offset === null) { + if ($offset === null || !isset($this->nodes[$offset])) { $this->addNode($value); } else { $this->nodes[$offset] = $value; @@ -554,6 +572,8 @@ final class Directory extends FileAbstract implements DirectoryInterface, LocalC */ public function offsetExists($offset) { + $offset = isset($this->nodes[$offset]) ? $offset : $this->path . '/' . $offset; + return isset($this->nodes[$offset]); } @@ -562,7 +582,11 @@ final class Directory extends FileAbstract implements DirectoryInterface, LocalC */ public function offsetUnset($offset) : void { + $offset = isset($this->nodes[$offset]) ? $offset : $this->path . '/' . $offset; + if (isset($this->nodes[$offset])) { + $this->nodes[$offset]->deleteNode(); + unset($this->nodes[$offset]); } } @@ -628,18 +652,9 @@ final class Directory extends FileAbstract implements DirectoryInterface, LocalC */ public function deleteNode() : bool { - self::delete($this->path); + // @todo: update parent - if (!isset($this->nodes[$this->path])) { - return false; - } - - $this->count -= $this->nodes[$this->path]->getCount(); - $this->size -= $this->nodes[$this->path]->getSize(); - - unset($this->nodes[$this->path]); - - return true; + return self::delete($this->path); } /** diff --git a/System/File/Local/FileAbstract.php b/System/File/Local/FileAbstract.php index a7b9373e8..170cda776 100644 --- a/System/File/Local/FileAbstract.php +++ b/System/File/Local/FileAbstract.php @@ -132,6 +132,14 @@ abstract class FileAbstract implements ContainerInterface return $this->name; } + /** + * {@inheritdoc} + */ + public function getBasename() : string + { + return \basename($this->path); + } + /** * {@inheritdoc} */ diff --git a/tests/System/File/Local/DirectoryTest.php b/tests/System/File/Local/DirectoryTest.php index 88cf15dea..371a9df44 100644 --- a/tests/System/File/Local/DirectoryTest.php +++ b/tests/System/File/Local/DirectoryTest.php @@ -37,11 +37,6 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase \rmdir($dirPath); } - public function testStaticRemove() : void - { - self::markTestIncomplete(); - } - /** * @testdox A directory can be checked for existence * @covers phpOMS\System\File\Local\Directory @@ -554,98 +549,171 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase $dir->addNode(new Directory(__DIR__ . '/nodedir')); self::assertTrue(\file_exists(__DIR__ . '/nodedir')); - self::assertTrue($dir->deleteNode()); + self::assertTrue($dir->getNode('nodedir')->deleteNode()); self::assertFalse(\file_exists(__DIR__ . '/nodedir')); } public function testNodeCopy() : void { - self::markTestIncomplete(); + $dir = new Directory(__DIR__); + $dir->addNode(new Directory(__DIR__ . '/nodedir')); + + $dir->getNode('nodedir')->copyNode(__DIR__ . '/nodedir2'); + self::assertTrue(\file_exists(__DIR__ . '/nodedir2')); + + \rmdir(__DIR__ . '/nodedir'); + \rmdir(__DIR__ . '/nodedir2'); } public function testNodeMove() : void { - self::markTestIncomplete(); + $dir = new Directory(__DIR__); + $dir->addNode(new Directory(__DIR__ . '/nodedir')); + + $dir->getNode('nodedir')->moveNode(__DIR__ . '/nodedir2'); + self::assertFalse(\file_exists(__DIR__ . '/nodedir')); + self::assertTrue(\file_exists(__DIR__ . '/nodedir2')); + + \rmdir(__DIR__ . '/nodedir2'); } public function testNodeExists() : void { - self::markTestIncomplete(); + $dir = new Directory(__DIR__); + + self::assertTrue($dir->isExisting('dirtest')); + self::assertFalse($dir->isExisting('invalid')); } public function testParentOutput() : void { - self::markTestIncomplete(); - } + $dir = new Directory(__DIR__ . '/dirtest'); + self::assertEquals(__DIR__, $dir->parentNode()->getPath()); + } public function testNodeNext() : void { - self::markTestIncomplete(); + $dir = new Directory(__DIR__ . '/dirtest'); + + self::assertEquals(__DIR__ . '/dirtest/test.txt', $dir->next()->getPath()); } public function testNodeCurrent() : void { - self::markTestIncomplete(); + $dir = new Directory(__DIR__ . '/dirtest'); + + self::assertEquals(__DIR__ . '/dirtest/sub', $dir->current()->getPath()); } public function testNodeKey() : void { - self::markTestIncomplete(); + $dir = new Directory(__DIR__ . '/dirtest'); + + self::assertEquals('sub', $dir->key()); + $dir->next(); + self::assertEquals('test.txt', $dir->key()); } public function testNodeArrayRead() : void { - self::markTestIncomplete(); + $dir = new Directory(__DIR__ . '/dirtest'); + + self::assertEquals('test', $dir['test.txt']->getName()); } public function testNodeArraySet() : void { - self::markTestIncomplete(); + $dir = new Directory(__DIR__); + $dir[] = new Directory(__DIR__ . '/nodedir'); + + self::assertTrue(\file_exists(__DIR__ . '/nodedir')); + \rmdir(__DIR__ . '/nodedir'); } public function testNodeArrayRemove() : void { - self::markTestIncomplete(); + $dir = new Directory(__DIR__); + $dir->addNode(new Directory(__DIR__ . '/nodedir')); + + self::assertTrue(\file_exists(__DIR__ . '/nodedir')); + unset($dir['nodedir']); + self::assertFalse(\file_exists(__DIR__ . '/nodedir')); } public function testNodeArrayExists() : void { - self::markTestIncomplete(); + $dir = new Directory(__DIR__); + + self::assertTrue(isset($dir['dirtest'])); + self::assertFalse(isset($dir['invalid'])); } public function testNodeCreatedAt() : void { - self::markTestIncomplete(); + $dirPath = __DIR__ . '/test'; + $dir = new Directory($dirPath); + + self::assertTrue($dir->createNode()); + + $now = new \DateTime('now'); + self::assertEquals($now->format('Y-m-d'), $dir->getCreatedAt()->format('Y-m-d')); + + \rmdir($dirPath); } public function testNodeChangedAt() : void { - self::markTestIncomplete(); + $dirPath = __DIR__ . '/test'; + $dir = new Directory($dirPath); + + self::assertTrue($dir->createNode()); + + $now = new \DateTime('now'); + self::assertEquals($now->format('Y-m-d'), $dir->getChangedAt()->format('Y-m-d')); + + \rmdir($dirPath); } public function testNodeOwner() : void { - self::markTestIncomplete(); + $dir = new Directory(__DIR__ . '/dirtest'); + + self::assertNotEmpty($dir->getOwner()); } public function testNodePermission() : void { - self::markTestIncomplete(); + $dir = new Directory(__DIR__ . '/dirtest'); + + self::assertGreaterThan(0, $dir->getPermission()); } public function testDirname() : void { - self::markTestIncomplete(); + $dir = new Directory(__DIR__ . '/dirtest'); + + self::assertEquals('dirtest', $dir->next()->getDirname()); } public function testName() : void { - self::markTestIncomplete(); + $dir = new Directory(__DIR__ . '/dirtest'); + + self::assertEquals('test', $dir->next()->getName()); + } + + public function testBaseame() : void + { + $dir = new Directory(__DIR__ . '/dirtest'); + + self::assertEquals('test.txt', $dir->next()->getBasename()); } public function testDirpath() : void { - self::markTestIncomplete(); + $dir = new Directory(__DIR__ . '/dirtest'); + + self::assertEquals(__DIR__ . '/dirtest', $dir->next()->getDirpath()); } }