draft ftp dir tests

This commit is contained in:
Dennis Eichhorn 2020-10-03 10:46:03 +02:00
parent 4f463ed26e
commit 6adf0fda8e
8 changed files with 822 additions and 71 deletions

View File

@ -60,7 +60,7 @@ class Directory extends FileAbstract implements DirectoryInterface, FtpContainer
*/ */
public static function ftpConnect(HttpUri $http) public static function ftpConnect(HttpUri $http)
{ {
$con = \ftp_connect($http->getHost(), $http->getPort()); $con = \ftp_connect($http->getHost(), $http->getPort(), 10);
if ($con === false) { if ($con === false) {
return false; return false;
@ -69,7 +69,7 @@ class Directory extends FileAbstract implements DirectoryInterface, FtpContainer
\ftp_login($con, $http->getUser(), $http->getPass()); \ftp_login($con, $http->getUser(), $http->getPass());
if ($http->getPath() !== '') { if ($http->getPath() !== '') {
\ftp_chdir($con, $http->getPath()); @\ftp_chdir($con, $http->getPath());
} }
return $con; return $con;
@ -97,6 +97,10 @@ class Directory extends FileAbstract implements DirectoryInterface, FtpContainer
$detailed = self::parseRawList($con, $path); $detailed = self::parseRawList($con, $path);
foreach ($detailed as $key => $item) { foreach ($detailed as $key => $item) {
if ($filter !== '*' && \preg_match($filter, $key) === 1) {
continue;
}
$list[] = $key; $list[] = $key;
if ($item['type'] === 'dir') { if ($item['type'] === 'dir') {
@ -172,10 +176,6 @@ class Directory extends FileAbstract implements DirectoryInterface, FtpContainer
$countSize = 0; $countSize = 0;
$directories = self::parseRawList($con, $dir); $directories = self::parseRawList($con, $dir);
if ($directories === false) {
return $countSize;
}
foreach ($directories as $key => $filename) { foreach ($directories as $key => $filename) {
if ($key === '..' || $key === '.') { if ($key === '..' || $key === '.') {
continue; continue;
@ -205,10 +205,6 @@ class Directory extends FileAbstract implements DirectoryInterface, FtpContainer
$ignore[] = '.'; $ignore[] = '.';
$ignore[] = '..'; $ignore[] = '..';
if ($files === false) {
return $size;
}
foreach ($files as $key => $t) { foreach ($files as $key => $t) {
if (\in_array($key, $ignore)) { if (\in_array($key, $ignore)) {
continue; continue;
@ -436,7 +432,7 @@ class Directory extends FileAbstract implements DirectoryInterface, FtpContainer
} }
$list = \scandir($from); $list = \scandir($from);
foreach ($list as $key => $item) { foreach ($list as $item) {
if ($item === '.' || $item === '..') { if ($item === '.' || $item === '..') {
continue; continue;
} }
@ -528,6 +524,12 @@ class Directory extends FileAbstract implements DirectoryInterface, FtpContainer
*/ */
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) {
$this->nodes[$name]->index();
}
return $this->nodes[$name] ?? null; return $this->nodes[$name] ?? null;
} }
@ -537,10 +539,6 @@ class Directory extends FileAbstract implements DirectoryInterface, FtpContainer
public function createNode() : bool public function createNode() : bool
{ {
return self::create($this->con, $this->path, $this->permission, true); return self::create($this->con, $this->path, $this->permission, true);
/**
* @todo Orange-Management/phpOMS#??? [p:low] [t:todo] [d:medium]
* Add node to node list
*/
} }
/** /**
@ -562,12 +560,7 @@ class Directory extends FileAbstract implements DirectoryInterface, FtpContainer
*/ */
public function getParent() : ContainerInterface public function getParent() : ContainerInterface
{ {
/** return new self(self::parent($this->path));
* @todo Orange-Management/phpOMS#??? [p:low] [t:todo] [d:medium]
* Implement getParent()
*/
return $this;
} }
/** /**
@ -575,12 +568,7 @@ class Directory extends FileAbstract implements DirectoryInterface, FtpContainer
*/ */
public function copyNode(string $to, bool $overwrite = false) : bool public function copyNode(string $to, bool $overwrite = false) : bool
{ {
/** return self::copy($this->con, $this->path, $to, $overwrite);
* @todo Orange-Management/phpOMS#??? [p:low] [t:todo] [d:medium]
* Implement copyNode()
*/
return true;
} }
/** /**
@ -588,12 +576,7 @@ class Directory extends FileAbstract implements DirectoryInterface, FtpContainer
*/ */
public function moveNode(string $to, bool $overwrite = false) : bool public function moveNode(string $to, bool $overwrite = false) : bool
{ {
/** return self::move($this->con, $this->path, $to, $overwrite);
* @todo Orange-Management/phpOMS#??? [p:low] [t:todo] [d:medium]
* Implement moveNode()
*/
return true;
} }
/** /**
@ -601,12 +584,9 @@ class Directory extends FileAbstract implements DirectoryInterface, FtpContainer
*/ */
public function deleteNode() : bool public function deleteNode() : bool
{ {
/** // @todo: update parent
* @todo Orange-Management/phpOMS#??? [p:low] [t:todo] [d:medium]
* Implement deleteNode()
*/
return true; return self::delete($this->con, $this->path);
} }
/** /**
@ -622,7 +602,13 @@ class Directory extends FileAbstract implements DirectoryInterface, FtpContainer
*/ */
public function current() public function current()
{ {
return \current($this->nodes); $current = \current($this->nodes);
if (isset($current) && $current instanceof self) {
$current->index();
}
return $current;
} }
/** /**
@ -638,7 +624,13 @@ class Directory extends FileAbstract implements DirectoryInterface, FtpContainer
*/ */
public function next() public function next()
{ {
return \next($this->nodes); $next = \next($this->nodes);
if (isset($next) && $next instanceof self) {
$next->index();
}
return $next;
} }
/** /**
@ -656,10 +648,11 @@ class Directory extends FileAbstract implements DirectoryInterface, FtpContainer
*/ */
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]->deleteNode();
$this->addNode($value);
} }
} }
@ -668,6 +661,8 @@ class Directory extends FileAbstract implements DirectoryInterface, FtpContainer
*/ */
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]);
} }
@ -676,7 +671,11 @@ class Directory extends FileAbstract implements DirectoryInterface, FtpContainer
*/ */
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]);
} }
} }
@ -686,12 +685,31 @@ class Directory extends FileAbstract implements DirectoryInterface, FtpContainer
*/ */
public function offsetGet($offset) public function offsetGet($offset)
{ {
/** if (isset($this->nodes[$offset]) && $this->nodes[$offset] instanceof self) {
* @todo Orange-Management/phpOMS#??? [p:low] [t:todo] [d:medium] $this->nodes[$offset]->index();
* Implement offsetGet() }
*/
return 0; return $this->nodes[$offset] ?? null;
}
/**
* Check if the child node exists
*
* @param string $name Child node name. If empty checks if this node exists.
*
* @return bool
*
* @since 1.0.0
*/
public function isExisting(string $name = null) : bool
{
if ($name === null) {
return \file_exists($this->path);
}
$name = isset($this->nodes[$name]) ? $name : $this->path . '/' . $name;
return isset($this->nodes[$name]);
} }
/** /**
@ -699,6 +717,47 @@ class Directory extends FileAbstract implements DirectoryInterface, FtpContainer
*/ */
public function getList() : array public function getList() : array
{ {
return []; $pathLength = \strlen($this->path);
$content = [];
foreach ($this->nodes as $node) {
$content[] = \substr($node->getPath(), $pathLength + 1);
}
return $content;
}
/**
* List all files by extension directory.
*
* @param resource $con FTP connection
* @param string $path Path
* @param string $extension Extension
* @param string $exclude Pattern to exclude
*
* @return array<array|string>
*
* @since 1.0.0
*/
public static function listByExtension($con, string $path, string $extension = '', string $exclude = '') : array
{
$list = [];
$path = \rtrim($path, '\\/');
if (!\file_exists($path)) {
return $list;
}
$files = self::list($con, $path, empty($extension) ? '*' : '/*.\.' . $extension . '$/');
foreach ($files as $file) {
if (!empty($exclude) && \preg_match('/' . $exclude . '/', $file) === 1) {
continue;
}
$list[] = $file;
}
return $list;
} }
} }

View File

@ -76,7 +76,7 @@ class File extends FileAbstract implements FileInterface
*/ */
public static function ftpConnect(HttpUri $http) public static function ftpConnect(HttpUri $http)
{ {
$con = \ftp_connect($http->getHost(), $http->getPort()); $con = \ftp_connect($http->getHost(), $http->getPort(), 10);
if ($con === false) { if ($con === false) {
return false; return false;
@ -85,7 +85,7 @@ class File extends FileAbstract implements FileInterface
\ftp_login($con, $http->getUser(), $http->getPass()); \ftp_login($con, $http->getUser(), $http->getPass());
if ($http->getPath() !== '') { if ($http->getPath() !== '') {
\ftp_chdir($con, $http->getPath()); @\ftp_chdir($con, $http->getPath());
} }
return $con; return $con;

View File

@ -455,17 +455,21 @@ final class Directory extends FileAbstract implements DirectoryInterface, LocalC
return $this->nodes[$name] ?? null; return $this->nodes[$name] ?? null;
} }
/** /**
* Check if the child node exists * Check if the child node exists
* *
* @param string $name Child node name * @param string $name Child node name. If empty checks if this node exists.
* *
* @return bool * @return bool
* *
* @since 1.0.0 * @since 1.0.0
*/ */
public function isExisting(string $name) : bool public function isExisting(string $name = null) : bool
{ {
if ($name === null) {
return \file_exists($this->path);
}
$name = isset($this->nodes[$name]) ? $name : $this->path . '/' . $name; $name = isset($this->nodes[$name]) ? $name : $this->path . '/' . $name;
return isset($this->nodes[$name]); return isset($this->nodes[$name]);
@ -563,7 +567,8 @@ final class Directory extends FileAbstract implements DirectoryInterface, LocalC
if ($offset === null || !isset($this->nodes[$offset])) { if ($offset === null || !isset($this->nodes[$offset])) {
$this->addNode($value); $this->addNode($value);
} else { } else {
$this->nodes[$offset] = $value; $this->nodes[$offset]->deleteNode();
$this->addNode($value);
} }
} }

View File

@ -94,7 +94,7 @@ final class File extends FileAbstract implements FileInterface, LocalContainerIn
return true; return true;
} }
} catch (\Throwable $e) { } catch (\Throwable $e) {
return false; return false; // @codeCoverageIgnore
} }
return false; return false;
@ -295,13 +295,14 @@ final class File extends FileAbstract implements FileInterface, LocalContainerIn
*/ */
public static function pathInfo(string $path) : array public static function pathInfo(string $path) : array
{ {
$info = []; $temp = [];
\preg_match('#^(.*?)[\\\\/]*(([^/\\\\]*?)(\.([^.\\\\/]+?)|))[\\\\/.]*$#m', $path, $info); \preg_match('#^(.*?)[\\\\/]*(([^/\\\\]*?)(\.([^.\\\\/]+?)|))[\\\\/.]*$#m', $path, $temp);
$info['dirname'] = $info[1] ?? ''; $info = [];
$info['basename'] = $info[2] ?? ''; $info['dirname'] = $temp[1] ?? '';
$info['filename'] = $info[3] ?? ''; $info['basename'] = $temp[2] ?? '';
$info['extension'] = $info[5] ?? ''; $info['filename'] = $temp[3] ?? '';
$info['extension'] = $temp[5] ?? '';
return $info; return $info;
} }
@ -420,6 +421,18 @@ final class File extends FileAbstract implements FileInterface, LocalContainerIn
return self::create($this->path); return self::create($this->path);
} }
/**
* Check if the file exists
*
* @return bool
*
* @since 1.0.0
*/
public function isExisting() : bool
{
return \file_exists($this->path);
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -465,7 +478,7 @@ final class File extends FileAbstract implements FileInterface, LocalContainerIn
*/ */
public function appendContent(string $content) : bool public function appendContent(string $content) : bool
{ {
return $this->putContent($content, ContentPutMode::APPEND | ContentPutMode::CREATE); return $this->putContent($content, ContentPutMode::APPEND);
} }
/** /**
@ -473,7 +486,7 @@ final class File extends FileAbstract implements FileInterface, LocalContainerIn
*/ */
public function prependContent(string $content) : bool public function prependContent(string $content) : bool
{ {
return $this->putContent($content, ContentPutMode::PREPEND | ContentPutMode::CREATE); return $this->putContent($content, ContentPutMode::PREPEND);
} }
/** /**
@ -502,6 +515,11 @@ final class File extends FileAbstract implements FileInterface, LocalContainerIn
return new Directory(self::parent($this->path)); return new Directory(self::parent($this->path));
} }
public function getDirectory() : ContainerInterface
{
return new Directory(self::dirpath($this->path));
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */

View File

@ -41,9 +41,19 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase
} }
} }
public function testConnection() : void
{
self::assertNotEquals(false, Directory::ftpConnect(new HttpUri(self::BASE . '/test')));
}
public function testInvalidConnection() : void
{
self::assertFalse(Directory::ftpConnect(new HttpUri('ftp://orange-management.org:21')));
}
/** /**
* @testdox A directory can be created * @testdox A directory can be created
* @covers phpOMS\System\File\Local\Directory * @covers phpOMS\System\File\Ftp\Directory
* @group framework * @group framework
*/ */
public function testStaticCreate() : void public function testStaticCreate() : void
@ -298,6 +308,26 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase
Directory::delete($this->con, __DIR__ . '/newdirtest'); Directory::delete($this->con, __DIR__ . '/newdirtest');
} }
public function testStaticCopyOverwrite() : void
{
$dirTestPath = __DIR__ . '/dirtest';
self::assertTrue(Directory::copy($this->con, $dirTestPath, __DIR__ . '/newdirtest'));
self::assertFalse(Directory::copy($this->con, $dirTestPath, __DIR__ . '/newdirtest', false));
self::assertTrue(Directory::copy($this->con, $dirTestPath, __DIR__ . '/newdirtest', true));
self::assertFileExists(__DIR__ . '/newdirtest/sub/path/test3.txt');
Directory::delete($this->con, __DIR__ . '/newdirtest');
}
public function testStaticInvalidCopyOverwrite() : void
{
$dirTestPath = __DIR__ . '/dirtest';
self::assertTrue(Directory::copy($this->con, $dirTestPath, __DIR__ . '/newdirtest'));
self::assertFalse(Directory::copy($this->con, $dirTestPath, __DIR__ . '/newdirtest', false));
Directory::delete($this->con, __DIR__ . '/newdirtest');
}
/** /**
* @testdox A directory can be moved/renamed to a different path * @testdox A directory can be moved/renamed to a different path
* @covers phpOMS\System\File\Ftp\Directory * @covers phpOMS\System\File\Ftp\Directory
@ -313,6 +343,29 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase
Directory::move($this->con, __DIR__ . '/newdirtest', $dirTestPath); Directory::move($this->con, __DIR__ . '/newdirtest', $dirTestPath);
} }
public function testStaticInvalidMoveOverwrite() : void
{
$dirTestPath = __DIR__ . '/dirtest';
self::assertTrue(Directory::move($this->con, $dirTestPath, __DIR__ . '/newdirtest'));
self::assertFalse(Directory::move($this->con, __DIR__ . '/newdirtest', __DIR__ . '/newdirtest', false));
Directory::move($this->con, __DIR__ . '/newdirtest', $dirTestPath);
}
public function testStaticMoveOverwrite() : void
{
$dirTestPath = __DIR__ . '/dirtest';
self::assertTrue(Directory::move($this->con, $dirTestPath, __DIR__ . '/newdirtest'));
self::assertTrue(Directory::copy($this->con, __DIR__ . '/newdirtest', $dirTestPath));
self::assertFalse(Directory::move($this->con, $dirTestPath, __DIR__ . '/newdirtest', false));
self::assertTrue(Directory::move($this->con, $dirTestPath, __DIR__ . '/newdirtest', true));
Directory::move($this->con, __DIR__ . '/newdirtest', $dirTestPath);
}
/** /**
* @testdox The amount of files in a directory can be returned recursively * @testdox The amount of files in a directory can be returned recursively
* @covers phpOMS\System\File\Ftp\Directory * @covers phpOMS\System\File\Ftp\Directory
@ -357,6 +410,23 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase
self::assertCount(6, Directory::list($this->con, $dirTestPath)); self::assertCount(6, Directory::list($this->con, $dirTestPath));
} }
public function testStaticListFilesByExtension() : void
{
$dirTestPath = __DIR__ . '/dirtest';
self::assertCount(3, Directory::listByExtension($this->con, $dirTestPath, 'txt'));
}
public function testStaticOwner() : void
{
$dirTestPath = __DIR__ . '/dirtest';
self::assertNotEmpty(Directory::owner($this->con, $dirTestPath));
}
public function testDirectoryNameSanitizing() : void
{
self::assertEquals(':/some/test/[path', Directory::sanitize(':#&^$/some%/test/[path!'));
}
/** /**
* @testdox A none-existing directory returns a empty list of files and sub-directories * @testdox A none-existing directory returns a empty list of files and sub-directories
* @covers phpOMS\System\File\Ftp\Directory * @covers phpOMS\System\File\Ftp\Directory
@ -367,6 +437,11 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase
self::assertEquals([], Directory::list($this->con, __DIR__ . '/invalid.txt')); self::assertEquals([], Directory::list($this->con, __DIR__ . '/invalid.txt'));
} }
public function testInvalidListFilesByExtension() : void
{
self::assertEquals([], Directory::listByExtension($this->con, __DIR__ . '/invalid/path/here', 'txt'));
}
/** /**
* @testdox A invalid directory cannot be copied to a new destination * @testdox A invalid directory cannot be copied to a new destination
* @covers phpOMS\System\File\Ftp\Directory * @covers phpOMS\System\File\Ftp\Directory
@ -422,4 +497,218 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase
Directory::owner($this->con, __DIR__ . '/invalid'); Directory::owner($this->con, __DIR__ . '/invalid');
} }
public function testList() : void
{
$dirTestPath = __DIR__ . '/dirtest';
$dir = new Directory($dirTestPath);
self::assertEquals([
'sub',
'test.txt'
], $dir->getList());
}
public function testNodeOutput() : void
{
$dirTestPath = __DIR__ . '/dirtest';
$dir = new Directory($dirTestPath);
self::assertInstanceOf(Directory::class, $dir->getNode('sub'));
}
public function testNodeCreate() : void
{
$dir = new Directory(__DIR__);
$dir->addNode(new Directory(__DIR__ . '/nodedir'));
self::assertTrue(\file_exists(__DIR__ . '/nodedir'));
\rmdir(__DIR__ . '/nodedir');
$dir = new Directory(__DIR__ . '/nodedir2');
$dir->createNode();
self::assertTrue(\file_exists(__DIR__ . '/nodedir2'));
\rmdir(__DIR__ . '/nodedir2');
}
public function testNodeDelete() : void
{
$dir = new Directory(__DIR__);
$dir->addNode(new Directory(__DIR__ . '/nodedir'));
self::assertTrue(\file_exists(__DIR__ . '/nodedir'));
self::assertTrue($dir->getNode('nodedir')->deleteNode());
self::assertFalse(\file_exists(__DIR__ . '/nodedir'));
}
public function testNodeCopy() : void
{
$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
{
$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
{
$dir = new Directory(__DIR__);
self::assertTrue($dir->isExisting());
self::assertTrue($dir->isExisting('dirtest'));
self::assertFalse($dir->isExisting('invalid'));
}
public function testParentOutput() : void
{
$dir = new Directory(__DIR__ . '/dirtest');
self::assertEquals(__DIR__, $dir->parentNode()->getPath());
}
public function testNodeNext() : void
{
$dir = new Directory(__DIR__ . '/dirtest');
self::assertEquals(__DIR__ . '/dirtest/test.txt', $dir->next()->getPath());
}
public function testNodeCurrent() : void
{
$dir = new Directory(__DIR__ . '/dirtest');
self::assertEquals(__DIR__ . '/dirtest/sub', $dir->current()->getPath());
}
public function testNodeKey() : void
{
$dir = new Directory(__DIR__ . '/dirtest');
self::assertEquals('sub', $dir->key());
$dir->next();
self::assertEquals('test.txt', $dir->key());
}
public function testNodeArrayRead() : void
{
$dir = new Directory(__DIR__ . '/dirtest');
self::assertEquals('test', $dir['test.txt']->getName());
}
public function testNodeArraySet() : void
{
$dir = new Directory(__DIR__);
$dir[] = new Directory(__DIR__ . '/nodedir');
self::assertTrue(\file_exists(__DIR__ . '/nodedir'));
\rmdir(__DIR__ . '/nodedir');
$dir['nodedir'] = new Directory(__DIR__ . '/nodedir');
self::assertTrue(\file_exists(__DIR__ . '/nodedir'));
\rmdir(__DIR__ . '/nodedir');
}
public function testNodeArrayRemove() : void
{
$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
{
$dir = new Directory(__DIR__);
self::assertTrue(isset($dir['dirtest']));
self::assertFalse(isset($dir['invalid']));
}
public function testNodeCreatedAt() : void
{
$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
{
$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
{
$dir = new Directory(__DIR__ . '/dirtest');
self::assertNotEmpty($dir->getOwner());
}
public function testNodePermission() : void
{
$dir = new Directory(__DIR__ . '/dirtest');
self::assertGreaterThan(0, $dir->getPermission());
}
public function testDirname() : void
{
$dir = new Directory(__DIR__ . '/dirtest');
self::assertEquals('dirtest', $dir->next()->getDirname());
}
public function testName() : void
{
$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
{
$dir = new Directory(__DIR__ . '/dirtest');
self::assertEquals(__DIR__ . '/dirtest', $dir->next()->getDirpath());
}
} }

View File

@ -581,6 +581,7 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase
{ {
$dir = new Directory(__DIR__); $dir = new Directory(__DIR__);
self::assertTrue($dir->isExisting());
self::assertTrue($dir->isExisting('dirtest')); self::assertTrue($dir->isExisting('dirtest'));
self::assertFalse($dir->isExisting('invalid')); self::assertFalse($dir->isExisting('invalid'));
} }
@ -629,6 +630,11 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase
self::assertTrue(\file_exists(__DIR__ . '/nodedir')); self::assertTrue(\file_exists(__DIR__ . '/nodedir'));
\rmdir(__DIR__ . '/nodedir'); \rmdir(__DIR__ . '/nodedir');
$dir['nodedir'] = new Directory(__DIR__ . '/nodedir');
self::assertTrue(\file_exists(__DIR__ . '/nodedir'));
\rmdir(__DIR__ . '/nodedir');
} }
public function testNodeArrayRemove() : void public function testNodeArrayRemove() : void

View File

@ -31,12 +31,21 @@ class FileTest extends \PHPUnit\Framework\TestCase
*/ */
public function testStaticCreate() : void public function testStaticCreate() : void
{ {
$testFile = __DIR__ . '/test.txt'; $testFile = __DIR__ . '/path/test.txt';
if (\file_exists($testFile)) {
\unlink($testFile);
}
if (\file_exists(__DIR__ . '/path')) {
\rmdir(__DIR__ . '/path');
}
self::assertTrue(File::create($testFile)); self::assertTrue(File::create($testFile));
self::assertTrue(\is_file($testFile)); self::assertTrue(\is_file($testFile));
self::assertEquals('', \file_get_contents($testFile)); self::assertEquals('', \file_get_contents($testFile));
\unlink($testFile); \unlink($testFile);
\rmdir(__DIR__ . '/path');
} }
/** /**
@ -47,6 +56,10 @@ class FileTest extends \PHPUnit\Framework\TestCase
public function testInvalidStaticCreate() : void public function testInvalidStaticCreate() : void
{ {
$testFile = __DIR__ . '/test.txt'; $testFile = __DIR__ . '/test.txt';
if (\file_exists($testFile)) {
\unlink($testFile);
}
self::assertTrue(File::create($testFile)); self::assertTrue(File::create($testFile));
self::assertFalse(File::create($testFile)); self::assertFalse(File::create($testFile));
self::assertTrue(\is_file($testFile)); self::assertTrue(\is_file($testFile));
@ -61,12 +74,21 @@ class FileTest extends \PHPUnit\Framework\TestCase
*/ */
public function testStaticPut() : void public function testStaticPut() : void
{ {
$testFile = __DIR__ . '/test.txt'; $testFile = __DIR__ . '/path/test.txt';
if (\file_exists($testFile)) {
\unlink($testFile);
}
if (\file_exists(__DIR__ . '/path')) {
\rmdir(__DIR__ . '/path');
}
self::assertTrue(File::put($testFile, 'test', ContentPutMode::CREATE)); self::assertTrue(File::put($testFile, 'test', ContentPutMode::CREATE));
self::assertTrue(\is_file($testFile)); self::assertTrue(\is_file($testFile));
self::assertEquals('test', \file_get_contents($testFile)); self::assertEquals('test', \file_get_contents($testFile));
\unlink($testFile); \unlink($testFile);
\rmdir(__DIR__ . '/path');
} }
/** /**
@ -77,6 +99,10 @@ class FileTest extends \PHPUnit\Framework\TestCase
public function testInvalidStaticCreateReplace() : void public function testInvalidStaticCreateReplace() : void
{ {
$testFile = __DIR__ . '/test.txt'; $testFile = __DIR__ . '/test.txt';
if (\file_exists($testFile)) {
\unlink($testFile);
}
self::assertFalse(File::put($testFile, 'test', ContentPutMode::REPLACE)); self::assertFalse(File::put($testFile, 'test', ContentPutMode::REPLACE));
self::assertfalse(\file_exists($testFile)); self::assertfalse(\file_exists($testFile));
} }
@ -89,6 +115,10 @@ class FileTest extends \PHPUnit\Framework\TestCase
public function testInvalidStaticCreateAppend() : void public function testInvalidStaticCreateAppend() : void
{ {
$testFile = __DIR__ . '/test.txt'; $testFile = __DIR__ . '/test.txt';
if (\file_exists($testFile)) {
\unlink($testFile);
}
self::assertFalse(File::put($testFile, 'test', ContentPutMode::APPEND)); self::assertFalse(File::put($testFile, 'test', ContentPutMode::APPEND));
self::assertfalse(\file_exists($testFile)); self::assertfalse(\file_exists($testFile));
} }
@ -101,6 +131,10 @@ class FileTest extends \PHPUnit\Framework\TestCase
public function testInvalidStaticCreatePrepend() : void public function testInvalidStaticCreatePrepend() : void
{ {
$testFile = __DIR__ . '/test.txt'; $testFile = __DIR__ . '/test.txt';
if (\file_exists($testFile)) {
\unlink($testFile);
}
self::assertFalse(File::put($testFile, 'test', ContentPutMode::PREPEND)); self::assertFalse(File::put($testFile, 'test', ContentPutMode::PREPEND));
self::assertfalse(\file_exists($testFile)); self::assertfalse(\file_exists($testFile));
} }
@ -124,6 +158,10 @@ class FileTest extends \PHPUnit\Framework\TestCase
public function testStaticReplace() : void public function testStaticReplace() : void
{ {
$testFile = __DIR__ . '/test.txt'; $testFile = __DIR__ . '/test.txt';
if (\file_exists($testFile)) {
\unlink($testFile);
}
self::assertTrue(File::put($testFile, 'test', ContentPutMode::CREATE)); self::assertTrue(File::put($testFile, 'test', ContentPutMode::CREATE));
self::assertTrue(File::put($testFile, 'test2', ContentPutMode::REPLACE)); self::assertTrue(File::put($testFile, 'test2', ContentPutMode::REPLACE));
@ -140,6 +178,10 @@ class FileTest extends \PHPUnit\Framework\TestCase
public function testStaticSetAlias() : void public function testStaticSetAlias() : void
{ {
$testFile = __DIR__ . '/test.txt'; $testFile = __DIR__ . '/test.txt';
if (\file_exists($testFile)) {
\unlink($testFile);
}
self::assertTrue(File::put($testFile, 'test', ContentPutMode::CREATE)); self::assertTrue(File::put($testFile, 'test', ContentPutMode::CREATE));
self::assertTrue(File::set($testFile, 'test2')); self::assertTrue(File::set($testFile, 'test2'));
@ -156,6 +198,10 @@ class FileTest extends \PHPUnit\Framework\TestCase
public function testStaticAppend() : void public function testStaticAppend() : void
{ {
$testFile = __DIR__ . '/test.txt'; $testFile = __DIR__ . '/test.txt';
if (\file_exists($testFile)) {
\unlink($testFile);
}
self::assertTrue(File::put($testFile, 'test', ContentPutMode::CREATE)); self::assertTrue(File::put($testFile, 'test', ContentPutMode::CREATE));
self::assertTrue(File::put($testFile, 'test2', ContentPutMode::APPEND)); self::assertTrue(File::put($testFile, 'test2', ContentPutMode::APPEND));
@ -172,6 +218,10 @@ class FileTest extends \PHPUnit\Framework\TestCase
public function testStaticAppendAlias() : void public function testStaticAppendAlias() : void
{ {
$testFile = __DIR__ . '/test.txt'; $testFile = __DIR__ . '/test.txt';
if (\file_exists($testFile)) {
\unlink($testFile);
}
self::assertTrue(File::put($testFile, 'test', ContentPutMode::CREATE)); self::assertTrue(File::put($testFile, 'test', ContentPutMode::CREATE));
self::assertTrue(File::append($testFile, 'test2')); self::assertTrue(File::append($testFile, 'test2'));
@ -188,6 +238,10 @@ class FileTest extends \PHPUnit\Framework\TestCase
public function testStaticPrepend() : void public function testStaticPrepend() : void
{ {
$testFile = __DIR__ . '/test.txt'; $testFile = __DIR__ . '/test.txt';
if (\file_exists($testFile)) {
\unlink($testFile);
}
self::assertTrue(File::put($testFile, 'test', ContentPutMode::CREATE)); self::assertTrue(File::put($testFile, 'test', ContentPutMode::CREATE));
self::assertTrue(File::put($testFile, 'test2', ContentPutMode::PREPEND)); self::assertTrue(File::put($testFile, 'test2', ContentPutMode::PREPEND));
@ -204,6 +258,10 @@ class FileTest extends \PHPUnit\Framework\TestCase
public function testStaticPrependAlias() : void public function testStaticPrependAlias() : void
{ {
$testFile = __DIR__ . '/test.txt'; $testFile = __DIR__ . '/test.txt';
if (\file_exists($testFile)) {
\unlink($testFile);
}
self::assertTrue(File::put($testFile, 'test', ContentPutMode::CREATE)); self::assertTrue(File::put($testFile, 'test', ContentPutMode::CREATE));
self::assertTrue(File::prepend($testFile, 'test2')); self::assertTrue(File::prepend($testFile, 'test2'));
@ -220,6 +278,10 @@ class FileTest extends \PHPUnit\Framework\TestCase
public function testStaticGet() : void public function testStaticGet() : void
{ {
$testFile = __DIR__ . '/test.txt'; $testFile = __DIR__ . '/test.txt';
if (\file_exists($testFile)) {
\unlink($testFile);
}
self::assertTrue(File::put($testFile, 'test', ContentPutMode::CREATE)); self::assertTrue(File::put($testFile, 'test', ContentPutMode::CREATE));
self::assertEquals('test', File::get($testFile)); self::assertEquals('test', File::get($testFile));
@ -376,6 +438,10 @@ class FileTest extends \PHPUnit\Framework\TestCase
public function testStaticSize() : void public function testStaticSize() : void
{ {
$testFile = __DIR__ . '/test.txt'; $testFile = __DIR__ . '/test.txt';
if (\file_exists($testFile)) {
\unlink($testFile);
}
File::put($testFile, 'test', ContentPutMode::CREATE); File::put($testFile, 'test', ContentPutMode::CREATE);
self::assertGreaterThan(0, File::size($testFile)); self::assertGreaterThan(0, File::size($testFile));
@ -391,6 +457,10 @@ class FileTest extends \PHPUnit\Framework\TestCase
public function testStaticPermission() : void public function testStaticPermission() : void
{ {
$testFile = __DIR__ . '/test.txt'; $testFile = __DIR__ . '/test.txt';
if (\file_exists($testFile)) {
\unlink($testFile);
}
File::put($testFile, 'test', ContentPutMode::CREATE); File::put($testFile, 'test', ContentPutMode::CREATE);
self::assertGreaterThan(0, File::permission($testFile)); self::assertGreaterThan(0, File::permission($testFile));
@ -409,6 +479,19 @@ class FileTest extends \PHPUnit\Framework\TestCase
self::assertEquals(-1, File::permission($testFile)); self::assertEquals(-1, File::permission($testFile));
} }
public function testPathInfo() : void
{
$testFile = __DIR__ . '/test.txt';
self::assertEquals([
'dirname' => __DIR__,
'basename' => 'test.txt',
'filename' => 'test',
'extension' => 'txt',
],
File::pathInfo($testFile)
);
}
/** /**
* @testdox A file can be copied to a different location * @testdox A file can be copied to a different location
* @covers phpOMS\System\File\Local\File * @covers phpOMS\System\File\Local\File
@ -417,7 +500,11 @@ class FileTest extends \PHPUnit\Framework\TestCase
public function testStaticCopy() : void public function testStaticCopy() : void
{ {
$testFile = __DIR__ . '/test.txt'; $testFile = __DIR__ . '/test.txt';
$newPath = __DIR__ . '/sub/path/testing.txt'; if (\file_exists($testFile)) {
\unlink($testFile);
}
$newPath = __DIR__ . '/sub/path/testing.txt';
File::put($testFile, 'test', ContentPutMode::CREATE); File::put($testFile, 'test', ContentPutMode::CREATE);
@ -440,7 +527,14 @@ class FileTest extends \PHPUnit\Framework\TestCase
public function testInvalidStaticCopy() : void public function testInvalidStaticCopy() : void
{ {
$testFile = __DIR__ . '/test.txt'; $testFile = __DIR__ . '/test.txt';
$newPath = __DIR__ . '/test2.txt'; if (\file_exists($testFile)) {
\unlink($testFile);
}
$newPath = __DIR__ . '/test2.txt';
if (\file_exists($newPath)) {
\unlink($newPath);
}
File::put($testFile, 'test', ContentPutMode::CREATE); File::put($testFile, 'test', ContentPutMode::CREATE);
File::put($newPath, 'test2', ContentPutMode::CREATE); File::put($newPath, 'test2', ContentPutMode::CREATE);
@ -460,7 +554,14 @@ class FileTest extends \PHPUnit\Framework\TestCase
public function testStaticCopyOverwrite() : void public function testStaticCopyOverwrite() : void
{ {
$testFile = __DIR__ . '/test.txt'; $testFile = __DIR__ . '/test.txt';
if (\file_exists($testFile)) {
\unlink($testFile);
}
$newPath = __DIR__ . '/test2.txt'; $newPath = __DIR__ . '/test2.txt';
if (\file_exists($newPath)) {
\unlink($newPath);
}
File::put($testFile, 'test', ContentPutMode::CREATE); File::put($testFile, 'test', ContentPutMode::CREATE);
File::put($newPath, 'test2', ContentPutMode::CREATE); File::put($newPath, 'test2', ContentPutMode::CREATE);
@ -480,7 +581,11 @@ class FileTest extends \PHPUnit\Framework\TestCase
public function testStaticMove() : void public function testStaticMove() : void
{ {
$testFile = __DIR__ . '/test.txt'; $testFile = __DIR__ . '/test.txt';
$newPath = __DIR__ . '/sub/path/testing.txt'; if (\file_exists($testFile)) {
\unlink($testFile);
}
$newPath = __DIR__ . '/sub/path/testing.txt';
File::put($testFile, 'test', ContentPutMode::CREATE); File::put($testFile, 'test', ContentPutMode::CREATE);
@ -502,7 +607,11 @@ class FileTest extends \PHPUnit\Framework\TestCase
public function testInvalidStaticMove() : void public function testInvalidStaticMove() : void
{ {
$testFile = __DIR__ . '/test.txt'; $testFile = __DIR__ . '/test.txt';
$newPath = __DIR__ . '/test2.txt'; if (\file_exists($testFile)) {
\unlink($testFile);
}
$newPath = __DIR__ . '/test2.txt';
File::put($testFile, 'test', ContentPutMode::CREATE); File::put($testFile, 'test', ContentPutMode::CREATE);
File::put($newPath, 'test2', ContentPutMode::CREATE); File::put($newPath, 'test2', ContentPutMode::CREATE);
@ -523,7 +632,11 @@ class FileTest extends \PHPUnit\Framework\TestCase
public function testStaticMoveOverwrite() : void public function testStaticMoveOverwrite() : void
{ {
$testFile = __DIR__ . '/test.txt'; $testFile = __DIR__ . '/test.txt';
$newPath = __DIR__ . '/test2.txt'; if (\file_exists($testFile)) {
\unlink($testFile);
}
$newPath = __DIR__ . '/test2.txt';
File::put($testFile, 'test', ContentPutMode::CREATE); File::put($testFile, 'test', ContentPutMode::CREATE);
File::put($newPath, 'test2', ContentPutMode::CREATE); File::put($newPath, 'test2', ContentPutMode::CREATE);
@ -535,6 +648,17 @@ class FileTest extends \PHPUnit\Framework\TestCase
\unlink($newPath); \unlink($newPath);
} }
public function testStaticOwner() : void
{
$dirTestPath = __DIR__ . '/dirtest/test.txt';
self::assertNotEmpty(File::owner($dirTestPath));
}
public function testFileNameSanitizing() : void
{
self::assertEquals('/some/test/[path.txt', File::sanitize(':#&^$/some%/test/[path!.txt'));
}
/** /**
* @testdox The size of a none-existing file is negative * @testdox The size of a none-existing file is negative
* @covers phpOMS\System\File\Local\File * @covers phpOMS\System\File\Local\File
@ -612,4 +736,249 @@ class FileTest extends \PHPUnit\Framework\TestCase
File::owner(__DIR__ . '/invalid.txt'); File::owner(__DIR__ . '/invalid.txt');
} }
public function testNodeInputOutput() : void
{
$testFile = __DIR__ . '/test.txt';
if (\file_exists($testFile)) {
\unlink($testFile);
}
$file = new File($testFile);
self::assertTrue($file->setContent('test'));
self::assertEquals('test', $file->getContent());
\unlink($testFile);
}
public function testNodeReplace() : void
{
$testFile = __DIR__ . '/test.txt';
if (\file_exists($testFile)) {
\unlink($testFile);
}
$file = new File($testFile);
self::assertTrue($file->setContent('test'));
self::assertTrue($file->setContent('test2'));
self::assertEquals('test2', $file->getContent());
\unlink($testFile);
}
public function testNodeAppend() : void
{
$testFile = __DIR__ . '/test.txt';
if (\file_exists($testFile)) {
\unlink($testFile);
}
$file = new File($testFile);
self::assertTrue($file->setContent('test'));
self::assertTrue($file->appendContent('2'));
self::assertEquals('test2', $file->getContent());
\unlink($testFile);
}
public function testNodePrepend() : void
{
$testFile = __DIR__ . '/test.txt';
if (\file_exists($testFile)) {
\unlink($testFile);
}
$file = new File($testFile);
self::assertTrue($file->setContent('test'));
self::assertTrue($file->prependContent('2'));
self::assertEquals('2test', $file->getContent());
\unlink($testFile);
}
public function testNodeExtension() : void
{
$testFile = __DIR__ . '/test.txt';
$file = new File($testFile);
self::assertEquals('txt', $file->getExtension());
}
public function testNodeCreatedAt() : void
{
$testFile = __DIR__ . '/test.txt';
if (\file_exists($testFile)) {
\unlink($testFile);
}
$file = new File($testFile);
$file->createNode();
$now = new \DateTime('now');
self::assertEquals($now->format('Y-m-d'), $file->getCreatedAt()->format('Y-m-d'));
\unlink($testFile);
}
public function testNodeChangedAt() : void
{
$testFile = __DIR__ . '/test.txt';
if (\file_exists($testFile)) {
\unlink($testFile);
}
$file = new File($testFile);
$file->createNode();
$now = new \DateTime('now');
self::assertEquals($now->format('Y-m-d'), $file->getChangedAt()->format('Y-m-d'));
\unlink($testFile);
}
public function testNodeOwner() : void
{
$testFile = __DIR__ . '/dirtest/test.txt';
$file = new File($testFile);
self::assertNotEmpty($file->getOwner());
}
public function testNodePermission() : void
{
$testFile = __DIR__ . '/dirtest/test.txt';
$file = new File($testFile);
self::assertGreaterThan(0, $file->getPermission());
}
public function testDirname() : void
{
$testFile = __DIR__ . '/dirtest/test.txt';
$file = new File($testFile);
self::assertEquals('dirtest', $file->getDirname());
}
public function testName() : void
{
$testFile = __DIR__ . '/dirtest/test.txt';
$file = new File($testFile);
self::assertEquals('test', $file->getName());
}
public function testBaseame() : void
{
$testFile = __DIR__ . '/dirtest/test.txt';
$file = new File($testFile);
self::assertEquals('test.txt', $file->getBasename());
}
public function testDirpath() : void
{
$testFile = __DIR__ . '/dirtest/test.txt';
$file = new File($testFile);
self::assertEquals(__DIR__ . '/dirtest', $file->getDirPath());
}
public function testParentOutput() : void
{
$testFile = __DIR__ . '/dirtest/test.txt';
$file = new File($testFile);
self::assertEquals(__DIR__ . '/dirtest', $file->getDirPath());
}
public function testNodeCreate() : void
{
$testFile = __DIR__ . '/test.txt';
if (\file_exists($testFile)) {
\unlink($testFile);
}
$file = new File($testFile);
$file->createNode();
self::assertTrue(\file_exists($testFile));
\unlink($testFile);
}
public function testNodeDelete() : void
{
$testFile = __DIR__ . '/test.txt';
if (\file_exists($testFile)) {
\unlink($testFile);
}
$file = new File($testFile);
$file->createNode();
self::assertTrue(\file_exists($testFile));
self::assertTrue($file->deleteNode());
self::assertFalse(\file_exists($testFile));
}
public function testNodeCopy() : void
{
$testFile = __DIR__ . '/test.txt';
if (\file_exists($testFile)) {
\unlink($testFile);
}
$file = new File($testFile);
$file->createNode();
self::assertTrue($file->copyNode(__DIR__ . '/test2.txt'));
self::assertTrue(\file_exists($testFile));
self::assertTrue(\file_exists(__DIR__ . '/test2.txt'));
\unlink($testFile);
\unlink(__DIR__ . '/test2.txt');
}
public function testNodeMove() : void
{
$testFile = __DIR__ . '/test.txt';
if (\file_exists($testFile)) {
\unlink($testFile);
}
$file = new File($testFile);
$file->createNode();
self::assertTrue($file->moveNode(__DIR__ . '/test2.txt'));
self::assertFalse(\file_exists($testFile));
self::assertTrue(\file_exists(__DIR__ . '/test2.txt'));
\unlink(__DIR__ . '/test2.txt');
}
public function testNodeExists() : void
{
$file = new File(__DIR__ . '/dirtest/test.txt');
$file2 = new File(__DIR__ . '/invalid.txt');
self::assertTrue($file->isExisting());
self::assertFalse($file2->isExisting());
}
public function testNodeParent() : void
{
$file = new File(__DIR__ . '/dirtest/test.txt');
self::assertEquals('Local', $file->getParent()->getName());
}
public function testNodeDirectory() : void
{
$file = new File(__DIR__ . '/dirtest/test.txt');
self::assertEquals('dirtest', $file->getDirectory()->getName());
}
} }

View File

@ -904,6 +904,11 @@ class LocalStorageTest extends \PHPUnit\Framework\TestCase
\unlink($newPath); \unlink($newPath);
} }
public function testSanitize() : void
{
self::assertEquals(':/some/test/[path', LocalStorage::sanitize(':#&^$/some%/test/[path!'));
}
/** /**
* @testdox The size of a none-existing file is negative * @testdox The size of a none-existing file is negative
* @covers phpOMS\System\File\Local\LocalStorage<extended> * @covers phpOMS\System\File\Local\LocalStorage<extended>