From d16d3c138526c2ef43a67b9229d8310f9adc6882 Mon Sep 17 00:00:00 2001 From: Saeed Vaziry Date: Mon, 25 Mar 2024 22:52:45 +0100 Subject: [PATCH] test --- app/Support/Testing/SSHFake.php | 22 ++++++++-------- tests/Feature/ConsoleTest.php | 45 +++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 12 deletions(-) create mode 100644 tests/Feature/ConsoleTest.php diff --git a/app/Support/Testing/SSHFake.php b/app/Support/Testing/SSHFake.php index f17417a..593b7e1 100644 --- a/app/Support/Testing/SSHFake.php +++ b/app/Support/Testing/SSHFake.php @@ -34,7 +34,7 @@ public function connect(bool $sftp = false): void } } - public function exec(string|array $commands, string $log = '', ?int $siteId = null, ?bool $stream = false): string + public function exec(string $command, string $log = '', ?int $siteId = null, ?bool $stream = false): string { if ($log) { $this->setLog($log, $siteId); @@ -42,21 +42,19 @@ public function exec(string|array $commands, string $log = '', ?int $siteId = nu $this->log = null; } - if (! is_array($commands)) { - $commands = [$commands]; - } - - foreach ($commands as $command) { - if (is_string($command)) { - $this->commands[] = $command; - } else { - $this->commands[] = get_class($command); - } - } + $this->commands[] = $command; $output = $this->output ?? 'fake output'; $this->log?->write($output); + if ($stream) { + echo $output; + ob_flush(); + flush(); + + return ''; + } + return $output; } diff --git a/tests/Feature/ConsoleTest.php b/tests/Feature/ConsoleTest.php new file mode 100644 index 0000000..5153bb5 --- /dev/null +++ b/tests/Feature/ConsoleTest.php @@ -0,0 +1,45 @@ +actingAs($this->user); + + $this->get(route('servers.console', $this->server)) + ->assertSeeText('Headless Console'); + } + + public function test_run(): void + { + SSH::fake('fake output'); + + $this->actingAs($this->user); + + $this->post(route('servers.console.run', $this->server), [ + 'user' => 'vito', + 'command' => 'ls -la', + ])->assertStreamedContent('fake output'); + } + + public function test_run_validation_error(): void + { + $this->actingAs($this->user); + + $this->post(route('servers.console.run', $this->server), [ + 'user' => 'vito', + ])->assertSessionHasErrors('command'); + + $this->post(route('servers.console.run', $this->server), [ + 'command' => 'ls -la', + ])->assertSessionHasErrors('user'); + } +}