From 12b8eea76cbfc676d803da4f969a86d5d37cc93d Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Tue, 26 Oct 2021 14:42:54 +0200 Subject: [PATCH] fix ftp upload --- System/File/Ftp/File.php | 1 + tests/System/File/Ftp/DirectoryTest.php | 48 +++++++++++++++--------- tests/System/File/Ftp/FileTest.php | 44 +++++++++++++--------- tests/System/File/Ftp/FtpStorageTest.php | 48 ++++++++++++++---------- 4 files changed, 87 insertions(+), 54 deletions(-) diff --git a/System/File/Ftp/File.php b/System/File/Ftp/File.php index a2341b1d3..380d4f7f7 100644 --- a/System/File/Ftp/File.php +++ b/System/File/Ftp/File.php @@ -125,6 +125,7 @@ class File extends FileAbstract implements FileInterface { $exists = self::exists($con, $path); + // @todo: consider to use the php://memory way, used in the seUpBeforeClass in the test if ((ContentPutMode::hasFlag($mode, ContentPutMode::APPEND) && $exists) || (ContentPutMode::hasFlag($mode, ContentPutMode::PREPEND) && $exists) || (ContentPutMode::hasFlag($mode, ContentPutMode::REPLACE) && $exists) diff --git a/tests/System/File/Ftp/DirectoryTest.php b/tests/System/File/Ftp/DirectoryTest.php index d55508cba..5e115a9e2 100644 --- a/tests/System/File/Ftp/DirectoryTest.php +++ b/tests/System/File/Ftp/DirectoryTest.php @@ -31,6 +31,34 @@ final class DirectoryTest extends \PHPUnit\Framework\TestCase public static function setUpBeforeClass() : void { self::$con = Directory::ftpConnect(new HttpUri(self::BASE)); + + if (self::$con === false) { + self::$con = null; + + return; + } + + try { + $mkdir = \ftp_mkdir(self::$con, '0xFF'); + \ftp_rmdir(self::$con, '0xFF'); + + $f = \fopen('php://memory', 'r+'); + \fwrite($f, '0x00'); + \rewind($f); + + $put = \ftp_fput(self::$con, '0x00', $f); + \fclose($f); + + \ftp_delete(self::$con, '0x00'); + + if (!$mkdir || !$put) { + throw new \Exception(); + } + } catch (\Throwable $t) { + self::$con = null; + + var_dump($t->getMessage()); + } } /** @@ -38,26 +66,10 @@ final class DirectoryTest extends \PHPUnit\Framework\TestCase */ protected function setUp() : void { - if (self::$con === false) { + if (self::$con === null) { $this->markTestSkipped( 'The ftp connection is not available.' ); - } else { - try { - $mkdir = \ftp_mkdir(self::$con, '0xFF'); - \ftp_rmdir(self::$con, '0xFF'); - - $put = \ftp_put(self::$con, '0x00'); - \ftp_delete(self::$con, '0x00'); - - if (!$mkdir || !$put) { - throw new \Exception(); - } - } catch (\Throwable $t) { - $this->markTestSkipped( - 'No write permissions on ftp server.' - ); - } } } @@ -894,7 +906,7 @@ final class DirectoryTest extends \PHPUnit\Framework\TestCase public function testNodeInvalid() : void { $dir = new Directory(new HttpUri(self::BASE . __DIR__ . '/dirtest'), '*', true, self::$con); - $dir->next()->next()->next()->next()->next()->next()->next(); + $dir?->next()?->next()?->next()?->next()?->next()?->next()?->next(); self::assertFalse($dir->valid()); } diff --git a/tests/System/File/Ftp/FileTest.php b/tests/System/File/Ftp/FileTest.php index 95e547f29..7fa17b374 100644 --- a/tests/System/File/Ftp/FileTest.php +++ b/tests/System/File/Ftp/FileTest.php @@ -33,6 +33,32 @@ final class FileTest extends \PHPUnit\Framework\TestCase public static function setUpBeforeClass() : void { self::$con = File::ftpConnect(new HttpUri(self::BASE)); + + if (self::$con === false) { + self::$con = null; + + return; + } + + try { + $mkdir = \ftp_mkdir(self::$con, '0xFF'); + \ftp_rmdir(self::$con, '0xFF'); + + $f = \fopen('php://memory', 'r+'); + \fwrite($f, '0x00'); + \rewind($f); + + $put = \ftp_fput(self::$con, '0x00', $f); + \fclose($f); + + \ftp_delete(self::$con, '0x00'); + + if (!$mkdir || !$put) { + throw new \Exception(); + } + } catch (\Throwable $t) { + self::$con = null; + } } /** @@ -40,26 +66,10 @@ final class FileTest extends \PHPUnit\Framework\TestCase */ protected function setUp() : void { - if (self::$con === false) { + if (self::$con === null) { $this->markTestSkipped( 'The ftp connection is not available.' ); - } else { - try { - $mkdir = \ftp_mkdir(self::$con, '0xFF'); - \ftp_rmdir(self::$con, '0xFF'); - - $put = \ftp_put(self::$con, '0x00'); - \ftp_delete(self::$con, '0x00'); - - if (!$mkdir || !$put) { - throw new \Exception(); - } - } catch (\Throwable $t) { - $this->markTestSkipped( - 'No write permissions on ftp server.' - ); - } } } diff --git a/tests/System/File/Ftp/FtpStorageTest.php b/tests/System/File/Ftp/FtpStorageTest.php index 09c1b5f27..b20cda3d2 100644 --- a/tests/System/File/Ftp/FtpStorageTest.php +++ b/tests/System/File/Ftp/FtpStorageTest.php @@ -34,6 +34,34 @@ final class FtpStorageTest extends \PHPUnit\Framework\TestCase public static function setUpBeforeClass() : void { self::$con = Directory::ftpConnect(new HttpUri(self::BASE)); + + if (self::$con === false) { + self::$con = null; + + return; + } + + try { + $mkdir = \ftp_mkdir(self::$con, '0xFF'); + \ftp_rmdir(self::$con, '0xFF'); + + $f = \fopen('php://memory', 'r+'); + \fwrite($f, '0x00'); + \rewind($f); + + $put = \ftp_fput(self::$con, '0x00', $f); + \fclose($f); + + \ftp_delete(self::$con, '0x00'); + + if (!$mkdir || !$put) { + throw new \Exception(); + } + } catch (\Throwable $t) { + self::$con = null; + } + + FtpStorage::with(self::$con); } /** @@ -41,29 +69,11 @@ final class FtpStorageTest extends \PHPUnit\Framework\TestCase */ protected function setUp() : void { - if (self::$con === false) { + if (self::$con === null) { $this->markTestSkipped( 'The ftp connection is not available.' ); - } else { - try { - $mkdir = \ftp_mkdir(self::$con, '0xFF'); - \ftp_rmdir(self::$con, '0xFF'); - - $put = \ftp_put(self::$con, '0x00'); - \ftp_delete(self::$con, '0x00'); - - if (!$mkdir || !$put) { - throw new \Exception(); - } - } catch (\Throwable $t) { - $this->markTestSkipped( - 'No write permissions on ftp server.' - ); - } } - - FtpStorage::with(self::$con); } /**