Skip to content

Commit

Permalink
refactor: Wip
Browse files Browse the repository at this point in the history
  • Loading branch information
lewislarsen committed Jul 23, 2024
1 parent 6e78509 commit 5b1e719
Show file tree
Hide file tree
Showing 6 changed files with 415 additions and 178 deletions.
32 changes: 24 additions & 8 deletions app/Providers/ServerConnectionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,39 @@
use Illuminate\Support\ServiceProvider;
use RuntimeException;

/**
* Service Provider for Server Connection.
*
* This provider is responsible for setting up the ServerConnectionManager
* and configuring its default values for private key and passphrase.
*/
class ServerConnectionServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* This method binds the ServerConnectionManager to the service container
* as a singleton instance.
*/
public function register(): void
{
$this->app->singleton('server.connection', function ($app): ServerConnectionManager {
$this->app->singleton('server.connection', function (): ServerConnectionManager {
return new ServerConnectionManager;
});
}

/**
* Bootstrap services.
*
* This method sets up the default private key and passphrase for
* the ServerConnectionManager. It throws a RuntimeException if
* the SSH passphrase is not set in the configuration.
*
* @throws RuntimeException If the SSH passphrase is not set in the configuration
*/
public function boot(): void
{
$manager = $this->app->make('server.connection');
$manager->defaultPrivateKey(Storage::disk('local')->path('app/ssh/id_rsa'));
$manager->defaultPassphrase(config('app.ssh.passphrase'));

if (! config('app.ssh.passphrase')) {
throw new RuntimeException('SSH passphrase is not set in the configuration.');
}
ServerConnectionManager::defaultPrivateKey(Storage::disk('local')->path('app/ssh/id_rsa'));
ServerConnectionManager::defaultPassphrase(config('app.ssh.passphrase'));
}
}
36 changes: 18 additions & 18 deletions app/Support/ServerConnection/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ public function __construct(
protected SSH2|SFTP|null $ssh2
) {}

/**
* Check if the connection is active.
*/
public function connected(): bool
{
return $this->ssh2 instanceof SSH2 && $this->ssh2->isConnected();
}

/**
* Disconnect from the server.
*/
public function disconnect(): void
{
if ($this->ssh2 instanceof SSH2) {
$this->ssh2->disconnect();
}
}

/**
* Run a command on the server.
*
Expand Down Expand Up @@ -84,22 +102,4 @@ public function download(string $remotePath, string $localPath): bool

return $result;
}

/**
* Check if the connection is active.
*/
public function connected(): bool
{
return $this->ssh2 instanceof SSH2 && $this->ssh2->isConnected();
}

/**
* Disconnect from the server.
*/
public function disconnect(): void
{
if ($this->ssh2 instanceof SSH2) {
$this->ssh2->disconnect();
}
}
}
20 changes: 10 additions & 10 deletions app/Support/ServerConnection/Fakes/ConnectionFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ public function __construct(protected ServerConnectionFake $serverConnectionFake
parent::__construct(null);
}

/**
* Check if the fake connection is active.
*
* @return bool True if the fake connection is active, false otherwise
*/
public function connected(): bool
{
return $this->serverConnectionFake->isConnected();
}

/**
* Simulate disconnecting from the server.
*/
Expand Down Expand Up @@ -84,16 +94,6 @@ public function download(string $remotePath, string $localPath): bool
return true;
}

/**
* Check if the fake connection is active.
*
* @return bool True if the fake connection is active, false otherwise
*/
public function connected(): bool
{
return $this->serverConnectionFake->isConnected();
}

/**
* Ensure that the fake connection is active before performing an operation.
*
Expand Down
90 changes: 49 additions & 41 deletions app/Support/ServerConnection/Fakes/ServerConnectionFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ class ServerConnectionFake extends PendingConnection
*/
protected string $output = '';

// Connection Simulation Methods

/**
* Simulate connecting from a RemoteServer model.
*
Expand Down Expand Up @@ -149,6 +151,42 @@ public function disconnect(): void
$this->isCurrentlyConnected = false;
}

// Action Recording Methods

/**
* Record a command that was run.
*
* @param string $command The command to record
*/
public function recordCommand(string $command): void
{
$this->commands[] = $command;
}

/**
* Record a file upload.
*
* @param string $localPath The local file path
* @param string $remotePath The remote file path
*/
public function recordUpload(string $localPath, string $remotePath): void
{
$this->uploads[] = ['localPath' => $localPath, 'remotePath' => $remotePath];
}

/**
* Record a file download.
*
* @param string $remotePath The remote file path
* @param string $localPath The local file path
*/
public function recordDownload(string $remotePath, string $localPath): void
{
$this->downloads[] = ['remotePath' => $remotePath, 'localPath' => $localPath];
}

// Assertion Methods

/**
* Assert that a connection was established.
*
Expand All @@ -169,6 +207,16 @@ public function assertNotConnected(): void
PHPUnit::assertFalse($this->isCurrentlyConnected, 'Failed asserting that a connection was not established.');
}

/**
* Assert that the connection was disconnected.
*
* @throws ExpectationFailedException
*/
public function assertDisconnected(): void
{
PHPUnit::assertFalse($this->isCurrentlyConnected, 'Failed asserting that the connection was disconnected.');
}

/**
* Assert that a connection was attempted with specific details.
*
Expand Down Expand Up @@ -241,37 +289,7 @@ public function assertOutput(string $output): void
PHPUnit::assertEquals($output, $this->output, 'The command output does not match.');
}

/**
* Record a command that was run.
*
* @param string $command The command to record
*/
public function recordCommand(string $command): void
{
$this->commands[] = $command;
}

/**
* Record a file upload.
*
* @param string $localPath The local file path
* @param string $remotePath The remote file path
*/
public function recordUpload(string $localPath, string $remotePath): void
{
$this->uploads[] = ['localPath' => $localPath, 'remotePath' => $remotePath];
}

/**
* Record a file download.
*
* @param string $remotePath The remote file path
* @param string $localPath The local file path
*/
public function recordDownload(string $remotePath, string $localPath): void
{
$this->downloads[] = ['remotePath' => $remotePath, 'localPath' => $localPath];
}
// Utility Methods

/**
* Set the simulated command output.
Expand Down Expand Up @@ -302,14 +320,4 @@ public function isConnected(): bool
{
return $this->isCurrentlyConnected;
}

/**
* Assert that the connection was disconnected.
*
* @throws ExpectationFailedException
*/
public function assertDisconnected(): void
{
PHPUnit::assertFalse($this->isCurrentlyConnected, 'Failed asserting that the connection was disconnected.');
}
}
Loading

0 comments on commit 5b1e719

Please sign in to comment.