mirror of
https://github.com/vitodeploy/vito.git
synced 2025-04-17 17:01:37 +00:00
Fix FTP and add more tests (#274)
This commit is contained in:
parent
8c487a64fa
commit
431da1b728
30
app/Facades/FTP.php
Normal file
30
app/Facades/FTP.php
Normal file
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Facades;
|
||||
|
||||
use App\Support\Testing\FTPFake;
|
||||
use FTP\Connection;
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
|
||||
/**
|
||||
* @method static bool|Connection connect(string $host, string $port, bool $ssl = false)
|
||||
* @method static bool login(string $username, string $password, bool|Connection $connection)
|
||||
* @method static void close(bool|Connection $connection)
|
||||
* @method static bool passive(bool|Connection $connection, bool $passive)
|
||||
* @method static bool delete(bool|Connection $connection, string $path)
|
||||
* @method static void assertConnected(string $host)
|
||||
*/
|
||||
class FTP extends Facade
|
||||
{
|
||||
protected static function getFacadeAccessor(): string
|
||||
{
|
||||
return 'ftp';
|
||||
}
|
||||
|
||||
public static function fake(): FTPFake
|
||||
{
|
||||
static::swap($fake = new FTPFake());
|
||||
|
||||
return $fake;
|
||||
}
|
||||
}
|
37
app/Helpers/FTP.php
Normal file
37
app/Helpers/FTP.php
Normal file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Helpers;
|
||||
|
||||
use FTP\Connection;
|
||||
|
||||
class FTP
|
||||
{
|
||||
public function connect(string $host, string $port, bool $ssl = false): bool|Connection
|
||||
{
|
||||
if ($ssl) {
|
||||
return ftp_ssl_connect($host, $port, 5);
|
||||
}
|
||||
|
||||
return ftp_connect($host, $port, 5);
|
||||
}
|
||||
|
||||
public function login(string $username, string $password, bool|Connection $connection): bool
|
||||
{
|
||||
return ftp_login($connection, $username, $password);
|
||||
}
|
||||
|
||||
public function close(bool|Connection $connection): void
|
||||
{
|
||||
ftp_close($connection);
|
||||
}
|
||||
|
||||
public function passive(bool|Connection $connection, bool $passive): bool
|
||||
{
|
||||
return ftp_pasv($connection, $passive);
|
||||
}
|
||||
|
||||
public function delete(bool|Connection $connection, string $path): bool
|
||||
{
|
||||
return ftp_delete($connection, $path);
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Helpers\FTP;
|
||||
use App\Helpers\Notifier;
|
||||
use App\Helpers\SSH;
|
||||
use App\Helpers\Toast;
|
||||
@ -36,5 +37,8 @@ public function boot(): void
|
||||
$this->app->bind('toast', function () {
|
||||
return new Toast;
|
||||
});
|
||||
$this->app->bind('ftp', function () {
|
||||
return new FTP;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
);
|
||||
}
|
||||
}
|
||||
|
60
app/Support/Testing/FTPFake.php
Normal file
60
app/Support/Testing/FTPFake.php
Normal file
@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support\Testing;
|
||||
|
||||
use FTP\Connection;
|
||||
use PHPUnit\Framework\Assert;
|
||||
|
||||
class FTPFake
|
||||
{
|
||||
protected array $connections = [];
|
||||
|
||||
protected array $logins = [];
|
||||
|
||||
public function connect(string $host, string $port, bool $ssl = false): bool|Connection
|
||||
{
|
||||
$this->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);
|
||||
}
|
||||
}
|
@ -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,
|
||||
],
|
||||
|
||||
|
@ -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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user