impl. local directory tests

This commit is contained in:
Dennis Eichhorn 2020-10-02 21:18:13 +02:00
parent 2c49d9aee9
commit 4f463ed26e
6 changed files with 151 additions and 43 deletions

View File

@ -59,6 +59,15 @@ interface ContainerInterface
*/ */
public function getName() : string; 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. * Get absolute path of the resource.
* *

View File

@ -548,9 +548,9 @@ class Directory extends FileAbstract implements DirectoryInterface, FtpContainer
*/ */
public function addNode(ContainerInterface $node) : self public function addNode(ContainerInterface $node) : self
{ {
$this->count += $node->getCount(); $this->count += $node->getCount();
$this->size += $node->getSize(); $this->size += $node->getSize();
$this->nodes[$node->getName()] = $node; $this->nodes[$node->getBasename()] = $node;
$node->createNode(); $node->createNode();

View File

@ -132,6 +132,14 @@ abstract class FileAbstract implements ContainerInterface
return $this->name; return $this->name;
} }
/**
* {@inheritdoc}
*/
public function getBasename() : string
{
return \basename($this->path);
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */

View File

@ -168,9 +168,9 @@ final class Directory extends FileAbstract implements DirectoryInterface, LocalC
*/ */
public function addNode(ContainerInterface $node) : self public function addNode(ContainerInterface $node) : self
{ {
$this->count += $node->getCount(); $this->count += $node->getCount();
$this->size += $node->getSize(); $this->size += $node->getSize();
$this->nodes[$node->getName()] = $node; $this->nodes[$node->getBasename()] = $node;
$node->createNode(); $node->createNode();
@ -446,6 +446,8 @@ final class Directory extends FileAbstract implements DirectoryInterface, LocalC
*/ */
public function getNode(string $name) : ?ContainerInterface 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) { if (isset($this->nodes[$name]) && $this->nodes[$name] instanceof self) {
$this->nodes[$name]->index(); $this->nodes[$name]->index();
} }
@ -453,6 +455,22 @@ final class Directory extends FileAbstract implements DirectoryInterface, LocalC
return $this->nodes[$name] ?? null; 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 * Create directory
* *
@ -542,7 +560,7 @@ final class Directory extends FileAbstract implements DirectoryInterface, LocalC
*/ */
public function offsetSet($offset, $value) : void public function offsetSet($offset, $value) : void
{ {
if ($offset === null) { if ($offset === null || !isset($this->nodes[$offset])) {
$this->addNode($value); $this->addNode($value);
} else { } else {
$this->nodes[$offset] = $value; $this->nodes[$offset] = $value;
@ -554,6 +572,8 @@ final class Directory extends FileAbstract implements DirectoryInterface, LocalC
*/ */
public function offsetExists($offset) public function offsetExists($offset)
{ {
$offset = isset($this->nodes[$offset]) ? $offset : $this->path . '/' . $offset;
return isset($this->nodes[$offset]); return isset($this->nodes[$offset]);
} }
@ -562,7 +582,11 @@ final class Directory extends FileAbstract implements DirectoryInterface, LocalC
*/ */
public function offsetUnset($offset) : void public function offsetUnset($offset) : void
{ {
$offset = isset($this->nodes[$offset]) ? $offset : $this->path . '/' . $offset;
if (isset($this->nodes[$offset])) { if (isset($this->nodes[$offset])) {
$this->nodes[$offset]->deleteNode();
unset($this->nodes[$offset]); unset($this->nodes[$offset]);
} }
} }
@ -628,18 +652,9 @@ final class Directory extends FileAbstract implements DirectoryInterface, LocalC
*/ */
public function deleteNode() : bool public function deleteNode() : bool
{ {
self::delete($this->path); // @todo: update parent
if (!isset($this->nodes[$this->path])) { return self::delete($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;
} }
/** /**

View File

@ -132,6 +132,14 @@ abstract class FileAbstract implements ContainerInterface
return $this->name; return $this->name;
} }
/**
* {@inheritdoc}
*/
public function getBasename() : string
{
return \basename($this->path);
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */

View File

@ -37,11 +37,6 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase
\rmdir($dirPath); \rmdir($dirPath);
} }
public function testStaticRemove() : void
{
self::markTestIncomplete();
}
/** /**
* @testdox A directory can be checked for existence * @testdox A directory can be checked for existence
* @covers phpOMS\System\File\Local\Directory * @covers phpOMS\System\File\Local\Directory
@ -554,98 +549,171 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase
$dir->addNode(new Directory(__DIR__ . '/nodedir')); $dir->addNode(new Directory(__DIR__ . '/nodedir'));
self::assertTrue(\file_exists(__DIR__ . '/nodedir')); self::assertTrue(\file_exists(__DIR__ . '/nodedir'));
self::assertTrue($dir->deleteNode()); self::assertTrue($dir->getNode('nodedir')->deleteNode());
self::assertFalse(\file_exists(__DIR__ . '/nodedir')); self::assertFalse(\file_exists(__DIR__ . '/nodedir'));
} }
public function testNodeCopy() : void 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 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 public function testNodeExists() : void
{ {
self::markTestIncomplete(); $dir = new Directory(__DIR__);
self::assertTrue($dir->isExisting('dirtest'));
self::assertFalse($dir->isExisting('invalid'));
} }
public function testParentOutput() : void public function testParentOutput() : void
{ {
self::markTestIncomplete(); $dir = new Directory(__DIR__ . '/dirtest');
}
self::assertEquals(__DIR__, $dir->parentNode()->getPath());
}
public function testNodeNext() : void public function testNodeNext() : void
{ {
self::markTestIncomplete(); $dir = new Directory(__DIR__ . '/dirtest');
self::assertEquals(__DIR__ . '/dirtest/test.txt', $dir->next()->getPath());
} }
public function testNodeCurrent() : void public function testNodeCurrent() : void
{ {
self::markTestIncomplete(); $dir = new Directory(__DIR__ . '/dirtest');
self::assertEquals(__DIR__ . '/dirtest/sub', $dir->current()->getPath());
} }
public function testNodeKey() : void 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 public function testNodeArrayRead() : void
{ {
self::markTestIncomplete(); $dir = new Directory(__DIR__ . '/dirtest');
self::assertEquals('test', $dir['test.txt']->getName());
} }
public function testNodeArraySet() : void 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 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 public function testNodeArrayExists() : void
{ {
self::markTestIncomplete(); $dir = new Directory(__DIR__);
self::assertTrue(isset($dir['dirtest']));
self::assertFalse(isset($dir['invalid']));
} }
public function testNodeCreatedAt() : void 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 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 public function testNodeOwner() : void
{ {
self::markTestIncomplete(); $dir = new Directory(__DIR__ . '/dirtest');
self::assertNotEmpty($dir->getOwner());
} }
public function testNodePermission() : void public function testNodePermission() : void
{ {
self::markTestIncomplete(); $dir = new Directory(__DIR__ . '/dirtest');
self::assertGreaterThan(0, $dir->getPermission());
} }
public function testDirname() : void public function testDirname() : void
{ {
self::markTestIncomplete(); $dir = new Directory(__DIR__ . '/dirtest');
self::assertEquals('dirtest', $dir->next()->getDirname());
} }
public function testName() : void 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 public function testDirpath() : void
{ {
self::markTestIncomplete(); $dir = new Directory(__DIR__ . '/dirtest');
self::assertEquals(__DIR__ . '/dirtest', $dir->next()->getDirpath());
} }
} }