continue file test impl.

This commit is contained in:
Dennis Eichhorn 2020-10-03 11:21:06 +02:00
parent 6adf0fda8e
commit be99c96ac6
4 changed files with 325 additions and 35 deletions

View File

@ -75,6 +75,26 @@ class Directory extends FileAbstract implements DirectoryInterface, FtpContainer
return $con;
}
/**
* Constructor.
*
* @param HttpUri $uri Uri
* @param string $filter Filter
*
* @since 1.0.0
*/
public function __construct(HttpUri $uri, string $filter = '*', bool $initialize = true)
{
$this->con = self::ftpConnect($uri);
$this->filter = \ltrim($filter, '\\/');
parent::__construct($uri->getPath());
if ($initialize && \file_exists($this->path)) {
$this->index();
}
}
/**
* List all files in directory.
*
@ -354,7 +374,9 @@ class Directory extends FileAbstract implements DirectoryInterface, FtpContainer
*/
public static function copy($con, string $from, string $to, bool $overwrite = false) : bool
{
if (!self::exists($con, $from)) {
if (!self::exists($con, $from)
|| (!$overwrite && self::exists($con, $to))
) {
return false;
}

View File

@ -374,7 +374,9 @@ final class Directory extends FileAbstract implements DirectoryInterface, LocalC
*/
public static function copy(string $from, string $to, bool $overwrite = false) : bool
{
if (!\is_dir($from)) {
if (!\is_dir($from)
|| (!$overwrite && \file_exists($to))
) {
return false;
}
@ -383,8 +385,6 @@ final class Directory extends FileAbstract implements DirectoryInterface, LocalC
} elseif ($overwrite && \file_exists($to)) {
self::delete($to);
self::create($to, 0755, true);
} else {
return false;
}
foreach ($iterator = new \RecursiveIteratorIterator(

View File

@ -28,6 +28,8 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase
private $con = null;
private $dir = null;
protected function setUp() : void
{
if ($this->con === null) {
@ -501,7 +503,7 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase
public function testList() : void
{
$dirTestPath = __DIR__ . '/dirtest';
$dir = new Directory($dirTestPath);
$dir = new Directory(new HttpUri(self::BASE . '/' . $dirTestPath));
self::assertEquals([
'sub',
@ -512,20 +514,20 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase
public function testNodeOutput() : void
{
$dirTestPath = __DIR__ . '/dirtest';
$dir = new Directory($dirTestPath);
$dir = new Directory(new HttpUri(self::BASE . '/' . $dirTestPath));
self::assertInstanceOf(Directory::class, $dir->getNode('sub'));
}
public function testNodeCreate() : void
{
$dir = new Directory(__DIR__);
$dir->addNode(new Directory(__DIR__ . '/nodedir'));
$dir = new Directory(new HttpUri(self::BASE . '/' . __DIR__));
$dir->addNode(new Directory(new HttpUri(self::BASE . '/' . __DIR__ . '/nodedir')));
self::assertTrue(\file_exists(__DIR__ . '/nodedir'));
\rmdir(__DIR__ . '/nodedir');
$dir = new Directory(__DIR__ . '/nodedir2');
$dir = new Directory(new HttpUri(self::BASE . '/' . __DIR__ . '/nodedir2'));
$dir->createNode();
self::assertTrue(\file_exists(__DIR__ . '/nodedir2'));
@ -534,8 +536,8 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase
public function testNodeDelete() : void
{
$dir = new Directory(__DIR__);
$dir->addNode(new Directory(__DIR__ . '/nodedir'));
$dir = new Directory(new HttpUri(self::BASE . '/' . __DIR__));
$dir->addNode(new Directory(new HttpUri(self::BASE . '/' . __DIR__ . '/nodedir')));
self::assertTrue(\file_exists(__DIR__ . '/nodedir'));
self::assertTrue($dir->getNode('nodedir')->deleteNode());
@ -544,8 +546,8 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase
public function testNodeCopy() : void
{
$dir = new Directory(__DIR__);
$dir->addNode(new Directory(__DIR__ . '/nodedir'));
$dir = new Directory(new HttpUri(self::BASE . '/' . __DIR__));
$dir->addNode(new Directory(new HttpUri(self::BASE . '/' . __DIR__ . '/nodedir')));
$dir->getNode('nodedir')->copyNode(__DIR__ . '/nodedir2');
self::assertTrue(\file_exists(__DIR__ . '/nodedir2'));
@ -556,8 +558,8 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase
public function testNodeMove() : void
{
$dir = new Directory(__DIR__);
$dir->addNode(new Directory(__DIR__ . '/nodedir'));
$dir = new Directory(new HttpUri(self::BASE . '/' . __DIR__));
$dir->addNode(new Directory(new HttpUri(self::BASE . '/' . __DIR__ . '/nodedir')));
$dir->getNode('nodedir')->moveNode(__DIR__ . '/nodedir2');
self::assertFalse(\file_exists(__DIR__ . '/nodedir'));
@ -568,7 +570,7 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase
public function testNodeExists() : void
{
$dir = new Directory(__DIR__);
$dir = new Directory(new HttpUri(self::BASE . '/' . __DIR__));
self::assertTrue($dir->isExisting());
self::assertTrue($dir->isExisting('dirtest'));
@ -577,28 +579,28 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase
public function testParentOutput() : void
{
$dir = new Directory(__DIR__ . '/dirtest');
$dir = new Directory(new HttpUri(self::BASE . '/' . __DIR__ . '/dirtest'));
self::assertEquals(__DIR__, $dir->parentNode()->getPath());
}
public function testNodeNext() : void
{
$dir = new Directory(__DIR__ . '/dirtest');
$dir = new Directory(new HttpUri(self::BASE . '/' . __DIR__ . '/dirtest'));
self::assertEquals(__DIR__ . '/dirtest/test.txt', $dir->next()->getPath());
}
public function testNodeCurrent() : void
{
$dir = new Directory(__DIR__ . '/dirtest');
$dir = new Directory(new HttpUri(self::BASE . '/' . __DIR__ . '/dirtest'));
self::assertEquals(__DIR__ . '/dirtest/sub', $dir->current()->getPath());
}
public function testNodeKey() : void
{
$dir = new Directory(__DIR__ . '/dirtest');
$dir = new Directory(new HttpUri(self::BASE . '/' . __DIR__ . '/dirtest'));
self::assertEquals('sub', $dir->key());
$dir->next();
@ -607,20 +609,20 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase
public function testNodeArrayRead() : void
{
$dir = new Directory(__DIR__ . '/dirtest');
$dir = new Directory(new HttpUri(self::BASE . '/' . __DIR__ . '/dirtest'));
self::assertEquals('test', $dir['test.txt']->getName());
}
public function testNodeArraySet() : void
{
$dir = new Directory(__DIR__);
$dir[] = new Directory(__DIR__ . '/nodedir');
$dir = new Directory(new HttpUri(self::BASE . '/' . __DIR__));
$dir[] = new Directory(new HttpUri(self::BASE . '/' . __DIR__ . '/nodedir'));
self::assertTrue(\file_exists(__DIR__ . '/nodedir'));
\rmdir(__DIR__ . '/nodedir');
$dir['nodedir'] = new Directory(__DIR__ . '/nodedir');
$dir['nodedir'] = new Directory(new HttpUri(self::BASE . '/' . __DIR__ . '/nodedir'));
self::assertTrue(\file_exists(__DIR__ . '/nodedir'));
\rmdir(__DIR__ . '/nodedir');
@ -628,8 +630,8 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase
public function testNodeArrayRemove() : void
{
$dir = new Directory(__DIR__);
$dir->addNode(new Directory(__DIR__ . '/nodedir'));
$dir = new Directory(new HttpUri(self::BASE . '/' . __DIR__));
$dir->addNode(new Directory(new HttpUri(self::BASE . '/' . __DIR__ . '/nodedir')));
self::assertTrue(\file_exists(__DIR__ . '/nodedir'));
unset($dir['nodedir']);
@ -638,7 +640,7 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase
public function testNodeArrayExists() : void
{
$dir = new Directory(__DIR__);
$dir = new Directory(new HttpUri(self::BASE . '/' . __DIR__));
self::assertTrue(isset($dir['dirtest']));
self::assertFalse(isset($dir['invalid']));
@ -647,7 +649,7 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase
public function testNodeCreatedAt() : void
{
$dirPath = __DIR__ . '/test';
$dir = new Directory($dirPath);
$dir = new Directory(new HttpUri(self::BASE . '/' . $dirPath));
self::assertTrue($dir->createNode());
@ -660,7 +662,7 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase
public function testNodeChangedAt() : void
{
$dirPath = __DIR__ . '/test';
$dir = new Directory($dirPath);
$dir = new Directory(new HttpUri(self::BASE . '/' . $dirPath));
self::assertTrue($dir->createNode());
@ -672,42 +674,42 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase
public function testNodeOwner() : void
{
$dir = new Directory(__DIR__ . '/dirtest');
$dir = new Directory(new HttpUri(self::BASE . '/' . __DIR__ . '/dirtest'));
self::assertNotEmpty($dir->getOwner());
}
public function testNodePermission() : void
{
$dir = new Directory(__DIR__ . '/dirtest');
$dir = new Directory(new HttpUri(self::BASE . '/' . __DIR__ . '/dirtest'));
self::assertGreaterThan(0, $dir->getPermission());
}
public function testDirname() : void
{
$dir = new Directory(__DIR__ . '/dirtest');
$dir = new Directory(new HttpUri(self::BASE . '/' . __DIR__ . '/dirtest'));
self::assertEquals('dirtest', $dir->next()->getDirname());
}
public function testName() : void
{
$dir = new Directory(__DIR__ . '/dirtest');
$dir = new Directory(new HttpUri(self::BASE . '/' . __DIR__ . '/dirtest'));
self::assertEquals('test', $dir->next()->getName());
}
public function testBaseame() : void
{
$dir = new Directory(__DIR__ . '/dirtest');
$dir = new Directory(new HttpUri(self::BASE . '/' . __DIR__ . '/dirtest'));
self::assertEquals('test.txt', $dir->next()->getBasename());
}
public function testDirpath() : void
{
$dir = new Directory(__DIR__ . '/dirtest');
$dir = new Directory(new HttpUri(self::BASE . '/' . __DIR__ . '/dirtest'));
self::assertEquals(__DIR__ . '/dirtest', $dir->next()->getDirpath());
}

View File

@ -43,6 +43,16 @@ class FileTest extends \PHPUnit\Framework\TestCase
}
}
public function testConnection() : void
{
self::assertNotEquals(false, File::ftpConnect(new HttpUri(self::BASE . '/test')));
}
public function testInvalidConnection() : void
{
self::assertFalse(File::ftpConnect(new HttpUri('ftp://orange-management.org:21')));
}
/**
* @testdox A file without content can be created
* @covers phpOMS\System\File\Ftp\File
@ -552,6 +562,17 @@ class FileTest extends \PHPUnit\Framework\TestCase
File::delete($this->con, $newPath);
}
public function testStaticOwner() : void
{
$dirTestPath = __DIR__ . '/dirtest/test.txt';
self::assertNotEmpty(File::owner($this->con, $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
* @covers phpOMS\System\File\Ftp\File
@ -629,4 +650,249 @@ class FileTest extends \PHPUnit\Framework\TestCase
File::owner($this->con, __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());
}
}