ftp directory tests working

This commit is contained in:
Dennis Eichhorn 2020-10-03 13:56:28 +02:00
parent f77ab5e980
commit 2f37ad1200
6 changed files with 75 additions and 47 deletions

View File

@ -120,9 +120,9 @@ class Directory extends FileAbstract implements DirectoryInterface, FtpContainer
/** /**
* List all files in directory. * List all files in directory.
* *
* @param resource $con FTP connection * @param resource $con FTP connection
* @param string $path Path * @param string $path Path
* @param string $filter Filter * @param string $filter Filter
* @param bool $recursive Recursive list * @param bool $recursive Recursive list
* *
* @return string[] * @return string[]

View File

@ -582,4 +582,28 @@ class File extends FileAbstract implements FileInterface
return $extension[1] ?? ''; return $extension[1] ?? '';
} }
/**
* Gets the directory name of a file.
*
* @return string returns the directory name of the file
*
* @since 1.0.0
*/
public function getDirName() : string
{
return \basename(\dirname($this->path));
}
/**
* Gets the directory path of a file.
*
* @return string returns the directory path of the file
*
* @since 1.0.0
*/
public function getDirPath() : string
{
return \dirname($this->path);
}
} }

View File

@ -97,11 +97,11 @@ final class Directory extends FileAbstract implements DirectoryInterface, LocalC
} }
foreach ($iterator as $item) { foreach ($iterator as $item) {
if ($item->isDot()) { if (!$recursive && $item->isDot()) {
continue; continue;
} }
$list[] = \str_replace('\\', '/', $iterator->getSubPathname()); $list[] = \substr(\str_replace('\\', '/', $iterator->getPathname()), \strlen($path) + 1);
} }
/** @var string[] $list */ /** @var string[] $list */
@ -136,14 +136,16 @@ final class Directory extends FileAbstract implements DirectoryInterface, LocalC
: new \DirectoryIterator($path); : new \DirectoryIterator($path);
foreach ($iterator as $item) { foreach ($iterator as $item) {
if ($item->isDot()) { if (!$recursive && $item->isDot()) {
continue; continue;
} }
$subPath = \substr($iterator->getPathname(), \strlen($path) + 1);
if ((empty($extension) || $item->getExtension() === $extension) if ((empty($extension) || $item->getExtension() === $extension)
&& (empty($exclude) || (!(bool) \preg_match('/' . $exclude . '/', $iterator->getSubPathname()))) && (empty($exclude) || (!(bool) \preg_match('/' . $exclude . '/', $subPath)))
) { ) {
$list[] = \str_replace('\\', '/', $iterator->getSubPathname()); $list[] = \str_replace('\\', '/', $subPath);
} }
} }
@ -398,10 +400,12 @@ final class Directory extends FileAbstract implements DirectoryInterface, LocalC
new \RecursiveDirectoryIterator($from, \RecursiveDirectoryIterator::SKIP_DOTS), new \RecursiveDirectoryIterator($from, \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::SELF_FIRST) as $item \RecursiveIteratorIterator::SELF_FIRST) as $item
) { ) {
$subPath = $iterator->getSubPathname();
if ($item->isDir()) { if ($item->isDir()) {
\mkdir($to . '/' . $iterator->getSubPathname()); \mkdir($to . '/' . $subPath);
} else { } else {
\copy($from . '/' . $iterator->getSubPathname(), $to . '/' . $iterator->getSubPathname()); \copy($from . '/' . $subPath, $to . '/' . $subPath);
} }
} }

View File

@ -502,7 +502,7 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase
public function testList() : void public function testList() : void
{ {
$dirTestPath = __DIR__ . '/dirtest'; $dirTestPath = __DIR__ . '/dirtest';
$dir = new Directory(new HttpUri(self::BASE . '/' . $dirTestPath), '*', true, self::$con); $dir = new Directory(new HttpUri(self::BASE . $dirTestPath), '*', true, self::$con);
self::assertEquals([ self::assertEquals([
'sub', 'sub',
@ -513,14 +513,14 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase
public function testNodeOutput() : void public function testNodeOutput() : void
{ {
$dirTestPath = __DIR__ . '/dirtest'; $dirTestPath = __DIR__ . '/dirtest';
$dir = new Directory(new HttpUri(self::BASE . '/' . $dirTestPath), '*', true, self::$con); $dir = new Directory(new HttpUri(self::BASE . $dirTestPath), '*', true, self::$con);
self::assertInstanceOf(Directory::class, $dir->getNode('sub')); self::assertInstanceOf(Directory::class, $dir->getNode('sub'));
} }
public function testNodeCreate() : void public function testNodeCreate() : void
{ {
$dir = new Directory(new HttpUri(self::BASE . '/' . __DIR__), '*', true, self::$con); $dir = new Directory(new HttpUri(self::BASE . __DIR__), '*', true, self::$con);
$dir->addNode(new Directory(new HttpUri(self::BASE . __DIR__ . '/nodedir'))); $dir->addNode(new Directory(new HttpUri(self::BASE . __DIR__ . '/nodedir')));
self::assertTrue(\file_exists(__DIR__ . '/nodedir')); self::assertTrue(\file_exists(__DIR__ . '/nodedir'));
@ -535,7 +535,7 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase
public function testNodeDelete() : void public function testNodeDelete() : void
{ {
$dir = new Directory(new HttpUri(self::BASE . '/' . __DIR__), '*', true, self::$con); $dir = new Directory(new HttpUri(self::BASE . __DIR__), '*', true, self::$con);
$dir->addNode(new Directory(new HttpUri(self::BASE . __DIR__ . '/nodedir'))); $dir->addNode(new Directory(new HttpUri(self::BASE . __DIR__ . '/nodedir')));
self::assertTrue(\file_exists(__DIR__ . '/nodedir')); self::assertTrue(\file_exists(__DIR__ . '/nodedir'));
@ -545,7 +545,7 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase
public function testNodeCopy() : void public function testNodeCopy() : void
{ {
$dir = new Directory(new HttpUri(self::BASE . '/' . __DIR__), '*', true, self::$con); $dir = new Directory(new HttpUri(self::BASE . __DIR__), '*', true, self::$con);
$dir->addNode(new Directory(new HttpUri(self::BASE . __DIR__ . '/nodedir'))); $dir->addNode(new Directory(new HttpUri(self::BASE . __DIR__ . '/nodedir')));
$dir->getNode('nodedir')->copyNode(__DIR__ . '/nodedir2'); $dir->getNode('nodedir')->copyNode(__DIR__ . '/nodedir2');
@ -557,7 +557,7 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase
public function testNodeMove() : void public function testNodeMove() : void
{ {
$dir = new Directory(new HttpUri(self::BASE . '/' . __DIR__), '*', true, self::$con); $dir = new Directory(new HttpUri(self::BASE . __DIR__), '*', true, self::$con);
$dir->addNode(new Directory(new HttpUri(self::BASE . __DIR__ . '/nodedir'))); $dir->addNode(new Directory(new HttpUri(self::BASE . __DIR__ . '/nodedir')));
$dir->getNode('nodedir')->moveNode(__DIR__ . '/nodedir2'); $dir->getNode('nodedir')->moveNode(__DIR__ . '/nodedir2');
@ -569,7 +569,7 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase
public function testNodeExists() : void public function testNodeExists() : void
{ {
$dir = new Directory(new HttpUri(self::BASE . '/' . __DIR__), '*', true, self::$con); $dir = new Directory(new HttpUri(self::BASE . __DIR__), '*', true, self::$con);
self::assertTrue($dir->isExisting()); self::assertTrue($dir->isExisting());
self::assertTrue($dir->isExisting('dirtest')); self::assertTrue($dir->isExisting('dirtest'));
@ -615,7 +615,7 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase
public function testNodeArraySet() : void public function testNodeArraySet() : void
{ {
$dir = new Directory(new HttpUri(self::BASE . '/' . __DIR__), '*', true, self::$con); $dir = new Directory(new HttpUri(self::BASE . __DIR__), '*', true, self::$con);
$dir[] = new Directory(new HttpUri(self::BASE . __DIR__ . '/nodedir')); $dir[] = new Directory(new HttpUri(self::BASE . __DIR__ . '/nodedir'));
self::assertTrue(\file_exists(__DIR__ . '/nodedir')); self::assertTrue(\file_exists(__DIR__ . '/nodedir'));
@ -629,7 +629,7 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase
public function testNodeArrayRemove() : void public function testNodeArrayRemove() : void
{ {
$dir = new Directory(new HttpUri(self::BASE . '/' . __DIR__), '*', true, self::$con); $dir = new Directory(new HttpUri(self::BASE . __DIR__), '*', true, self::$con);
$dir->addNode(new Directory(new HttpUri(self::BASE . __DIR__ . '/nodedir'))); $dir->addNode(new Directory(new HttpUri(self::BASE . __DIR__ . '/nodedir')));
self::assertTrue(\file_exists(__DIR__ . '/nodedir')); self::assertTrue(\file_exists(__DIR__ . '/nodedir'));
@ -639,7 +639,7 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase
public function testNodeArrayExists() : void public function testNodeArrayExists() : void
{ {
$dir = new Directory(new HttpUri(self::BASE . '/' . __DIR__), '*', true, self::$con); $dir = new Directory(new HttpUri(self::BASE . __DIR__), '*', true, self::$con);
self::assertTrue(isset($dir['dirtest'])); self::assertTrue(isset($dir['dirtest']));
self::assertFalse(isset($dir['invalid'])); self::assertFalse(isset($dir['invalid']));
@ -648,7 +648,7 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase
public function testNodeCreatedAt() : void public function testNodeCreatedAt() : void
{ {
$dirPath = __DIR__ . '/test'; $dirPath = __DIR__ . '/test';
$dir = new Directory(new HttpUri(self::BASE . '/' . $dirPath), '*', true, self::$con); $dir = new Directory(new HttpUri(self::BASE . $dirPath), '*', true, self::$con);
self::assertTrue($dir->createNode()); self::assertTrue($dir->createNode());
@ -661,7 +661,7 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase
public function testNodeChangedAt() : void public function testNodeChangedAt() : void
{ {
$dirPath = __DIR__ . '/test'; $dirPath = __DIR__ . '/test';
$dir = new Directory(new HttpUri(self::BASE . '/' . $dirPath), '*', true, self::$con); $dir = new Directory(new HttpUri(self::BASE . $dirPath), '*', true, self::$con);
self::assertTrue($dir->createNode()); self::assertTrue($dir->createNode());
@ -710,6 +710,6 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase
{ {
$dir = new Directory(new HttpUri(self::BASE . __DIR__ . '/dirtest'), '*', true, self::$con); $dir = new Directory(new HttpUri(self::BASE . __DIR__ . '/dirtest'), '*', true, self::$con);
self::assertEquals(__DIR__ . '/dirtest', $dir->next()->getDirpath()); self::assertEquals(__DIR__ . '/dirtest', $dir->next()->getDirPath());
} }
} }

View File

@ -659,7 +659,7 @@ class FileTest extends \PHPUnit\Framework\TestCase
\unlink($testFile); \unlink($testFile);
} }
$file = new File($testFile); $file = new File(new HttpUri(self::BASE . $testFile), self::$con);
self::assertTrue($file->setContent('test')); self::assertTrue($file->setContent('test'));
self::assertEquals('test', $file->getContent()); self::assertEquals('test', $file->getContent());
@ -673,7 +673,7 @@ class FileTest extends \PHPUnit\Framework\TestCase
\unlink($testFile); \unlink($testFile);
} }
$file = new File($testFile); $file = new File(new HttpUri(self::BASE . $testFile), self::$con);
self::assertTrue($file->setContent('test')); self::assertTrue($file->setContent('test'));
self::assertTrue($file->setContent('test2')); self::assertTrue($file->setContent('test2'));
self::assertEquals('test2', $file->getContent()); self::assertEquals('test2', $file->getContent());
@ -688,7 +688,7 @@ class FileTest extends \PHPUnit\Framework\TestCase
\unlink($testFile); \unlink($testFile);
} }
$file = new File($testFile); $file = new File(new HttpUri(self::BASE . $testFile), self::$con);
self::assertTrue($file->setContent('test')); self::assertTrue($file->setContent('test'));
self::assertTrue($file->appendContent('2')); self::assertTrue($file->appendContent('2'));
self::assertEquals('test2', $file->getContent()); self::assertEquals('test2', $file->getContent());
@ -703,7 +703,7 @@ class FileTest extends \PHPUnit\Framework\TestCase
\unlink($testFile); \unlink($testFile);
} }
$file = new File($testFile); $file = new File(new HttpUri(self::BASE . $testFile), self::$con);
self::assertTrue($file->setContent('test')); self::assertTrue($file->setContent('test'));
self::assertTrue($file->prependContent('2')); self::assertTrue($file->prependContent('2'));
self::assertEquals('2test', $file->getContent()); self::assertEquals('2test', $file->getContent());
@ -714,7 +714,7 @@ class FileTest extends \PHPUnit\Framework\TestCase
public function testNodeExtension() : void public function testNodeExtension() : void
{ {
$testFile = __DIR__ . '/test.txt'; $testFile = __DIR__ . '/test.txt';
$file = new File($testFile); $file = new File(new HttpUri(self::BASE . $testFile), self::$con);
self::assertEquals('txt', $file->getExtension()); self::assertEquals('txt', $file->getExtension());
} }
@ -726,7 +726,7 @@ class FileTest extends \PHPUnit\Framework\TestCase
\unlink($testFile); \unlink($testFile);
} }
$file = new File($testFile); $file = new File(new HttpUri(self::BASE . $testFile), self::$con);
$file->createNode(); $file->createNode();
@ -743,7 +743,7 @@ class FileTest extends \PHPUnit\Framework\TestCase
\unlink($testFile); \unlink($testFile);
} }
$file = new File($testFile); $file = new File(new HttpUri(self::BASE . $testFile), self::$con);
$file->createNode(); $file->createNode();
@ -756,7 +756,7 @@ class FileTest extends \PHPUnit\Framework\TestCase
public function testNodeOwner() : void public function testNodeOwner() : void
{ {
$testFile = __DIR__ . '/dirtest/test.txt'; $testFile = __DIR__ . '/dirtest/test.txt';
$file = new File($testFile); $file = new File(new HttpUri(self::BASE . $testFile), self::$con);
self::assertNotEmpty($file->getOwner()); self::assertNotEmpty($file->getOwner());
} }
@ -764,7 +764,7 @@ class FileTest extends \PHPUnit\Framework\TestCase
public function testNodePermission() : void public function testNodePermission() : void
{ {
$testFile = __DIR__ . '/dirtest/test.txt'; $testFile = __DIR__ . '/dirtest/test.txt';
$file = new File($testFile); $file = new File(new HttpUri(self::BASE . $testFile), self::$con);
self::assertGreaterThan(0, $file->getPermission()); self::assertGreaterThan(0, $file->getPermission());
} }
@ -772,7 +772,7 @@ class FileTest extends \PHPUnit\Framework\TestCase
public function testDirname() : void public function testDirname() : void
{ {
$testFile = __DIR__ . '/dirtest/test.txt'; $testFile = __DIR__ . '/dirtest/test.txt';
$file = new File($testFile); $file = new File(new HttpUri(self::BASE . $testFile), self::$con);
self::assertEquals('dirtest', $file->getDirname()); self::assertEquals('dirtest', $file->getDirname());
} }
@ -780,7 +780,7 @@ class FileTest extends \PHPUnit\Framework\TestCase
public function testName() : void public function testName() : void
{ {
$testFile = __DIR__ . '/dirtest/test.txt'; $testFile = __DIR__ . '/dirtest/test.txt';
$file = new File($testFile); $file = new File(new HttpUri(self::BASE . $testFile), self::$con);
self::assertEquals('test', $file->getName()); self::assertEquals('test', $file->getName());
} }
@ -788,7 +788,7 @@ class FileTest extends \PHPUnit\Framework\TestCase
public function testBaseame() : void public function testBaseame() : void
{ {
$testFile = __DIR__ . '/dirtest/test.txt'; $testFile = __DIR__ . '/dirtest/test.txt';
$file = new File($testFile); $file = new File(new HttpUri(self::BASE . $testFile), self::$con);
self::assertEquals('test.txt', $file->getBasename()); self::assertEquals('test.txt', $file->getBasename());
} }
@ -796,7 +796,7 @@ class FileTest extends \PHPUnit\Framework\TestCase
public function testDirpath() : void public function testDirpath() : void
{ {
$testFile = __DIR__ . '/dirtest/test.txt'; $testFile = __DIR__ . '/dirtest/test.txt';
$file = new File($testFile); $file = new File(new HttpUri(self::BASE . $testFile), self::$con);
self::assertEquals(__DIR__ . '/dirtest', $file->getDirPath()); self::assertEquals(__DIR__ . '/dirtest', $file->getDirPath());
} }
@ -804,7 +804,7 @@ class FileTest extends \PHPUnit\Framework\TestCase
public function testParentOutput() : void public function testParentOutput() : void
{ {
$testFile = __DIR__ . '/dirtest/test.txt'; $testFile = __DIR__ . '/dirtest/test.txt';
$file = new File($testFile); $file = new File(new HttpUri(self::BASE . $testFile), self::$con);
self::assertEquals(__DIR__ . '/dirtest', $file->getDirPath()); self::assertEquals(__DIR__ . '/dirtest', $file->getDirPath());
} }
@ -816,7 +816,7 @@ class FileTest extends \PHPUnit\Framework\TestCase
\unlink($testFile); \unlink($testFile);
} }
$file = new File($testFile); $file = new File(new HttpUri(self::BASE . $testFile), self::$con);
$file->createNode(); $file->createNode();
self::assertTrue(\file_exists($testFile)); self::assertTrue(\file_exists($testFile));
@ -831,7 +831,7 @@ class FileTest extends \PHPUnit\Framework\TestCase
\unlink($testFile); \unlink($testFile);
} }
$file = new File($testFile); $file = new File(new HttpUri(self::BASE . $testFile), self::$con);
$file->createNode(); $file->createNode();
self::assertTrue(\file_exists($testFile)); self::assertTrue(\file_exists($testFile));
@ -846,7 +846,7 @@ class FileTest extends \PHPUnit\Framework\TestCase
\unlink($testFile); \unlink($testFile);
} }
$file = new File($testFile); $file = new File(new HttpUri(self::BASE . $testFile), self::$con);
$file->createNode(); $file->createNode();
self::assertTrue($file->copyNode(__DIR__ . '/test2.txt')); self::assertTrue($file->copyNode(__DIR__ . '/test2.txt'));
@ -864,7 +864,7 @@ class FileTest extends \PHPUnit\Framework\TestCase
\unlink($testFile); \unlink($testFile);
} }
$file = new File($testFile); $file = new File(new HttpUri(self::BASE . $testFile), self::$con);
$file->createNode(); $file->createNode();
self::assertTrue($file->moveNode(__DIR__ . '/test2.txt')); self::assertTrue($file->moveNode(__DIR__ . '/test2.txt'));
@ -876,8 +876,8 @@ class FileTest extends \PHPUnit\Framework\TestCase
public function testNodeExists() : void public function testNodeExists() : void
{ {
$file = new File(__DIR__ . '/dirtest/test.txt'); $file = new File(new HttpUri(self::BASE . __DIR__ . '/dirtest/test.txt'), self::$con);
$file2 = new File(__DIR__ . '/invalid.txt'); $file2 = new File(new HttpUri(self::BASE . __DIR__ . '/invalid.txt'), self::$con);
self::assertTrue($file->isExisting()); self::assertTrue($file->isExisting());
self::assertFalse($file2->isExisting()); self::assertFalse($file2->isExisting());
@ -885,14 +885,14 @@ class FileTest extends \PHPUnit\Framework\TestCase
public function testNodeParent() : void public function testNodeParent() : void
{ {
$file = new File(__DIR__ . '/dirtest/test.txt'); $file = new File(new HttpUri(self::BASE . __DIR__ . '/dirtest/test.txt'), self::$con);
self::assertEquals('Local', $file->getParent()->getName()); self::assertEquals('Local', $file->getParent()->getName());
} }
public function testNodeDirectory() : void public function testNodeDirectory() : void
{ {
$file = new File(__DIR__ . '/dirtest/test.txt'); $file = new File(new HttpUri(self::BASE . __DIR__ . '/dirtest/test.txt'), self::$con);
self::assertEquals('dirtest', $file->getDirectory()->getName()); self::assertEquals('dirtest', $file->getDirectory()->getName());
} }

View File

@ -701,7 +701,7 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase
{ {
$dir = new Directory(__DIR__ . '/dirtest'); $dir = new Directory(__DIR__ . '/dirtest');
self::assertEquals('dirtest', $dir->next()->getDirname()); self::assertEquals('dirtest', $dir->next()->getDirName());
} }
public function testName() : void public function testName() : void
@ -722,6 +722,6 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase
{ {
$dir = new Directory(__DIR__ . '/dirtest'); $dir = new Directory(__DIR__ . '/dirtest');
self::assertEquals(__DIR__ . '/dirtest', $dir->next()->getDirpath()); self::assertEquals(__DIR__ . '/dirtest', $dir->next()->getDirPath());
} }
} }