From 2f37ad1200c7d307f109cb879aaa0dc54bbf52ae Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sat, 3 Oct 2020 13:56:28 +0200 Subject: [PATCH] ftp directory tests working --- System/File/Ftp/Directory.php | 6 ++-- System/File/Ftp/File.php | 24 +++++++++++++ System/File/Local/Directory.php | 18 ++++++---- tests/System/File/Ftp/DirectoryTest.php | 26 +++++++------- tests/System/File/Ftp/FileTest.php | 44 +++++++++++------------ tests/System/File/Local/DirectoryTest.php | 4 +-- 6 files changed, 75 insertions(+), 47 deletions(-) diff --git a/System/File/Ftp/Directory.php b/System/File/Ftp/Directory.php index 7115f636d..a8aaf98d9 100644 --- a/System/File/Ftp/Directory.php +++ b/System/File/Ftp/Directory.php @@ -120,9 +120,9 @@ class Directory extends FileAbstract implements DirectoryInterface, FtpContainer /** * List all files in directory. * - * @param resource $con FTP connection - * @param string $path Path - * @param string $filter Filter + * @param resource $con FTP connection + * @param string $path Path + * @param string $filter Filter * @param bool $recursive Recursive list * * @return string[] diff --git a/System/File/Ftp/File.php b/System/File/Ftp/File.php index 298e01f04..2065eb0c3 100644 --- a/System/File/Ftp/File.php +++ b/System/File/Ftp/File.php @@ -582,4 +582,28 @@ class File extends FileAbstract implements FileInterface 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); + } } diff --git a/System/File/Local/Directory.php b/System/File/Local/Directory.php index 07632842b..4bbee8f5e 100644 --- a/System/File/Local/Directory.php +++ b/System/File/Local/Directory.php @@ -97,11 +97,11 @@ final class Directory extends FileAbstract implements DirectoryInterface, LocalC } foreach ($iterator as $item) { - if ($item->isDot()) { + if (!$recursive && $item->isDot()) { continue; } - $list[] = \str_replace('\\', '/', $iterator->getSubPathname()); + $list[] = \substr(\str_replace('\\', '/', $iterator->getPathname()), \strlen($path) + 1); } /** @var string[] $list */ @@ -136,14 +136,16 @@ final class Directory extends FileAbstract implements DirectoryInterface, LocalC : new \DirectoryIterator($path); foreach ($iterator as $item) { - if ($item->isDot()) { + if (!$recursive && $item->isDot()) { continue; } + $subPath = \substr($iterator->getPathname(), \strlen($path) + 1); + 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), \RecursiveIteratorIterator::SELF_FIRST) as $item ) { + $subPath = $iterator->getSubPathname(); + if ($item->isDir()) { - \mkdir($to . '/' . $iterator->getSubPathname()); + \mkdir($to . '/' . $subPath); } else { - \copy($from . '/' . $iterator->getSubPathname(), $to . '/' . $iterator->getSubPathname()); + \copy($from . '/' . $subPath, $to . '/' . $subPath); } } diff --git a/tests/System/File/Ftp/DirectoryTest.php b/tests/System/File/Ftp/DirectoryTest.php index 98f119edc..1d6117840 100644 --- a/tests/System/File/Ftp/DirectoryTest.php +++ b/tests/System/File/Ftp/DirectoryTest.php @@ -502,7 +502,7 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase public function testList() : void { $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([ 'sub', @@ -513,14 +513,14 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase public function testNodeOutput() : void { $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')); } 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'))); self::assertTrue(\file_exists(__DIR__ . '/nodedir')); @@ -535,7 +535,7 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase 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'))); self::assertTrue(\file_exists(__DIR__ . '/nodedir')); @@ -545,7 +545,7 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase 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->getNode('nodedir')->copyNode(__DIR__ . '/nodedir2'); @@ -557,7 +557,7 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase 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->getNode('nodedir')->moveNode(__DIR__ . '/nodedir2'); @@ -569,7 +569,7 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase 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('dirtest')); @@ -615,7 +615,7 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase 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')); self::assertTrue(\file_exists(__DIR__ . '/nodedir')); @@ -629,7 +629,7 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase 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'))); self::assertTrue(\file_exists(__DIR__ . '/nodedir')); @@ -639,7 +639,7 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase 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::assertFalse(isset($dir['invalid'])); @@ -648,7 +648,7 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase public function testNodeCreatedAt() : void { $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()); @@ -661,7 +661,7 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase public function testNodeChangedAt() : void { $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()); @@ -710,6 +710,6 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase { $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()); } } diff --git a/tests/System/File/Ftp/FileTest.php b/tests/System/File/Ftp/FileTest.php index 52771d699..4e9bc2ca0 100644 --- a/tests/System/File/Ftp/FileTest.php +++ b/tests/System/File/Ftp/FileTest.php @@ -659,7 +659,7 @@ class FileTest extends \PHPUnit\Framework\TestCase \unlink($testFile); } - $file = new File($testFile); + $file = new File(new HttpUri(self::BASE . $testFile), self::$con); self::assertTrue($file->setContent('test')); self::assertEquals('test', $file->getContent()); @@ -673,7 +673,7 @@ class FileTest extends \PHPUnit\Framework\TestCase \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('test2')); self::assertEquals('test2', $file->getContent()); @@ -688,7 +688,7 @@ class FileTest extends \PHPUnit\Framework\TestCase \unlink($testFile); } - $file = new File($testFile); + $file = new File(new HttpUri(self::BASE . $testFile), self::$con); self::assertTrue($file->setContent('test')); self::assertTrue($file->appendContent('2')); self::assertEquals('test2', $file->getContent()); @@ -703,7 +703,7 @@ class FileTest extends \PHPUnit\Framework\TestCase \unlink($testFile); } - $file = new File($testFile); + $file = new File(new HttpUri(self::BASE . $testFile), self::$con); self::assertTrue($file->setContent('test')); self::assertTrue($file->prependContent('2')); self::assertEquals('2test', $file->getContent()); @@ -714,7 +714,7 @@ class FileTest extends \PHPUnit\Framework\TestCase public function testNodeExtension() : void { $testFile = __DIR__ . '/test.txt'; - $file = new File($testFile); + $file = new File(new HttpUri(self::BASE . $testFile), self::$con); self::assertEquals('txt', $file->getExtension()); } @@ -726,7 +726,7 @@ class FileTest extends \PHPUnit\Framework\TestCase \unlink($testFile); } - $file = new File($testFile); + $file = new File(new HttpUri(self::BASE . $testFile), self::$con); $file->createNode(); @@ -743,7 +743,7 @@ class FileTest extends \PHPUnit\Framework\TestCase \unlink($testFile); } - $file = new File($testFile); + $file = new File(new HttpUri(self::BASE . $testFile), self::$con); $file->createNode(); @@ -756,7 +756,7 @@ class FileTest extends \PHPUnit\Framework\TestCase public function testNodeOwner() : void { $testFile = __DIR__ . '/dirtest/test.txt'; - $file = new File($testFile); + $file = new File(new HttpUri(self::BASE . $testFile), self::$con); self::assertNotEmpty($file->getOwner()); } @@ -764,7 +764,7 @@ class FileTest extends \PHPUnit\Framework\TestCase public function testNodePermission() : void { $testFile = __DIR__ . '/dirtest/test.txt'; - $file = new File($testFile); + $file = new File(new HttpUri(self::BASE . $testFile), self::$con); self::assertGreaterThan(0, $file->getPermission()); } @@ -772,7 +772,7 @@ class FileTest extends \PHPUnit\Framework\TestCase public function testDirname() : void { $testFile = __DIR__ . '/dirtest/test.txt'; - $file = new File($testFile); + $file = new File(new HttpUri(self::BASE . $testFile), self::$con); self::assertEquals('dirtest', $file->getDirname()); } @@ -780,7 +780,7 @@ class FileTest extends \PHPUnit\Framework\TestCase public function testName() : void { $testFile = __DIR__ . '/dirtest/test.txt'; - $file = new File($testFile); + $file = new File(new HttpUri(self::BASE . $testFile), self::$con); self::assertEquals('test', $file->getName()); } @@ -788,7 +788,7 @@ class FileTest extends \PHPUnit\Framework\TestCase public function testBaseame() : void { $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()); } @@ -796,7 +796,7 @@ class FileTest extends \PHPUnit\Framework\TestCase public function testDirpath() : void { $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()); } @@ -804,7 +804,7 @@ class FileTest extends \PHPUnit\Framework\TestCase public function testParentOutput() : void { $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()); } @@ -816,7 +816,7 @@ class FileTest extends \PHPUnit\Framework\TestCase \unlink($testFile); } - $file = new File($testFile); + $file = new File(new HttpUri(self::BASE . $testFile), self::$con); $file->createNode(); self::assertTrue(\file_exists($testFile)); @@ -831,7 +831,7 @@ class FileTest extends \PHPUnit\Framework\TestCase \unlink($testFile); } - $file = new File($testFile); + $file = new File(new HttpUri(self::BASE . $testFile), self::$con); $file->createNode(); self::assertTrue(\file_exists($testFile)); @@ -846,7 +846,7 @@ class FileTest extends \PHPUnit\Framework\TestCase \unlink($testFile); } - $file = new File($testFile); + $file = new File(new HttpUri(self::BASE . $testFile), self::$con); $file->createNode(); self::assertTrue($file->copyNode(__DIR__ . '/test2.txt')); @@ -864,7 +864,7 @@ class FileTest extends \PHPUnit\Framework\TestCase \unlink($testFile); } - $file = new File($testFile); + $file = new File(new HttpUri(self::BASE . $testFile), self::$con); $file->createNode(); self::assertTrue($file->moveNode(__DIR__ . '/test2.txt')); @@ -876,8 +876,8 @@ class FileTest extends \PHPUnit\Framework\TestCase public function testNodeExists() : void { - $file = new File(__DIR__ . '/dirtest/test.txt'); - $file2 = new File(__DIR__ . '/invalid.txt'); + $file = new File(new HttpUri(self::BASE . __DIR__ . '/dirtest/test.txt'), self::$con); + $file2 = new File(new HttpUri(self::BASE . __DIR__ . '/invalid.txt'), self::$con); self::assertTrue($file->isExisting()); self::assertFalse($file2->isExisting()); @@ -885,14 +885,14 @@ class FileTest extends \PHPUnit\Framework\TestCase 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()); } 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()); } diff --git a/tests/System/File/Local/DirectoryTest.php b/tests/System/File/Local/DirectoryTest.php index c4ac7cb7d..3bff4d689 100644 --- a/tests/System/File/Local/DirectoryTest.php +++ b/tests/System/File/Local/DirectoryTest.php @@ -701,7 +701,7 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase { $dir = new Directory(__DIR__ . '/dirtest'); - self::assertEquals('dirtest', $dir->next()->getDirname()); + self::assertEquals('dirtest', $dir->next()->getDirName()); } public function testName() : void @@ -722,6 +722,6 @@ class DirectoryTest extends \PHPUnit\Framework\TestCase { $dir = new Directory(__DIR__ . '/dirtest'); - self::assertEquals(__DIR__ . '/dirtest', $dir->next()->getDirpath()); + self::assertEquals(__DIR__ . '/dirtest', $dir->next()->getDirPath()); } }