diff --git a/app/Facades/FTP.php b/app/Facades/FTP.php new file mode 100644 index 0000000..bdd2bcf --- /dev/null +++ b/app/Facades/FTP.php @@ -0,0 +1,30 @@ +app->bind('toast', function () { return new Toast; }); + $this->app->bind('ftp', function () { + return new FTP; + }); } } diff --git a/app/StorageProviders/FTP.php b/app/StorageProviders/FTP.php index 88c2a0d..53ea596 100644 --- a/app/StorageProviders/FTP.php +++ b/app/StorageProviders/FTP.php @@ -41,7 +41,7 @@ public function connect(): bool $isConnected = $connection && $this->login($connection); if ($isConnected) { - ftp_close($connection); + \App\Facades\FTP::close($connection); } return $isConnected; @@ -58,31 +58,36 @@ public function delete(array $paths): void if ($connection && $this->login($connection)) { if ($this->storageProvider->credentials['passive']) { - ftp_pasv($connection, true); + \App\Facades\FTP::passive($connection, true); } foreach ($paths as $path) { - ftp_delete($connection, $this->storageProvider->credentials['path'].'/'.$path); + \App\Facades\FTP::delete($connection, $this->storageProvider->credentials['path'].'/'.$path); } } - ftp_close($connection); + \App\Facades\FTP::close($connection); } private function connection(): bool|Connection { $credentials = $this->storageProvider->credentials; - if ($credentials['ssl']) { - return ftp_ssl_connect($credentials['host'], $credentials['port'], 5); - } - return ftp_connect($credentials['host'], $credentials['port'], 5); + return \App\Facades\FTP::connect( + $credentials['host'], + $credentials['port'], + $credentials['ssl'] + ); } - private function login(Connection $connection): bool + private function login(bool|Connection $connection): bool { $credentials = $this->storageProvider->credentials; - return ftp_login($connection, $credentials['username'], $credentials['password']); + return \App\Facades\FTP::login( + $credentials['username'], + $credentials['password'], + $connection + ); } } diff --git a/app/Support/Testing/FTPFake.php b/app/Support/Testing/FTPFake.php new file mode 100644 index 0000000..168c9dd --- /dev/null +++ b/app/Support/Testing/FTPFake.php @@ -0,0 +1,60 @@ +connections[] = compact('host', 'port', 'ssl'); + + return true; + } + + public function login(string $username, string $password, bool|Connection $connection): bool + { + $this->logins[] = compact('username', 'password'); + + return true; + } + + public function close(bool|Connection $connection): void + { + // + } + + public function passive(bool|Connection $connection, bool $passive): void + { + // + } + + public function delete(bool|Connection $connection, string $path): void + { + // + } + + public function assertConnected(string $host): void + { + if (! $this->connections) { + Assert::fail('No connections are made'); + } + $connected = false; + foreach ($this->connections as $connection) { + if ($connection['host'] === $host) { + $connected = true; + break; + } + } + if (! $connected) { + Assert::fail('The expected host is not connected'); + } + Assert::assertTrue(true, $connected); + } +} diff --git a/config/core.php b/config/core.php index 8b141ec..0e489b4 100755 --- a/config/core.php +++ b/config/core.php @@ -430,7 +430,7 @@ ], 'storage_providers_class' => [ \App\Enums\StorageProvider::DROPBOX => \App\StorageProviders\Dropbox::class, - \App\Enums\StorageProvider::FTP => \App\StorageProviders\Ftp::class, + \App\Enums\StorageProvider::FTP => \App\StorageProviders\FTP::class, \App\Enums\StorageProvider::LOCAL => \App\StorageProviders\Local::class, ], diff --git a/tests/Feature/StorageProvidersTest.php b/tests/Feature/StorageProvidersTest.php index 196927c..872c16e 100644 --- a/tests/Feature/StorageProvidersTest.php +++ b/tests/Feature/StorageProvidersTest.php @@ -3,6 +3,7 @@ namespace Tests\Feature; use App\Enums\StorageProvider; +use App\Facades\FTP; use App\Models\Backup; use App\Models\Database; use Illuminate\Foundation\Testing\RefreshDatabase; @@ -24,9 +25,17 @@ public function test_create(array $input): void Http::fake(); } + if ($input['provider'] === StorageProvider::FTP) { + FTP::fake(); + } + $this->post(route('settings.storage-providers.connect'), $input) ->assertSessionDoesntHaveErrors(); + if ($input['provider'] === StorageProvider::FTP) { + FTP::assertConnected($input['host']); + } + $this->assertDatabaseHas('storage_providers', [ 'provider' => $input['provider'], 'profile' => $input['name'], @@ -113,33 +122,33 @@ public static function createData(): array 'global' => 1, ], ], - // [ - // [ - // 'provider' => StorageProvider::FTP, - // 'name' => 'ftp-test', - // 'host' => '1.2.3.4', - // 'port' => '22', - // 'path' => '/home/vito', - // 'username' => 'username', - // 'password' => 'password', - // 'ssl' => 1, - // 'passive' => 1, - // ], - // ], - // [ - // [ - // 'provider' => StorageProvider::FTP, - // 'name' => 'ftp-test', - // 'host' => '1.2.3.4', - // 'port' => '22', - // 'path' => '/home/vito', - // 'username' => 'username', - // 'password' => 'password', - // 'ssl' => 1, - // 'passive' => 1, - // 'global' => 1, - // ], - // ], + [ + [ + 'provider' => StorageProvider::FTP, + 'name' => 'ftp-test', + 'host' => '1.2.3.4', + 'port' => '22', + 'path' => '/home/vito', + 'username' => 'username', + 'password' => 'password', + 'ssl' => 1, + 'passive' => 1, + ], + ], + [ + [ + 'provider' => StorageProvider::FTP, + 'name' => 'ftp-test', + 'host' => '1.2.3.4', + 'port' => '22', + 'path' => '/home/vito', + 'username' => 'username', + 'password' => 'password', + 'ssl' => 1, + 'passive' => 1, + 'global' => 1, + ], + ], [ [ 'provider' => StorageProvider::DROPBOX,