mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-01-11 09:48:40 +00:00
impl. local directory tests
This commit is contained in:
parent
2c49d9aee9
commit
4f463ed26e
|
|
@ -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.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
||||
|
|
|
|||
|
|
@ -132,6 +132,14 @@ abstract class FileAbstract implements ContainerInterface
|
|||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getBasename() : string
|
||||
{
|
||||
return \basename($this->path);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -132,6 +132,14 @@ abstract class FileAbstract implements ContainerInterface
|
|||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getBasename() : string
|
||||
{
|
||||
return \basename($this->path);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user