diff --git a/src/PendingProcess.php b/src/PendingProcess.php index 5a43226..0b7d342 100644 --- a/src/PendingProcess.php +++ b/src/PendingProcess.php @@ -3,6 +3,8 @@ namespace Bagel\ProcessSsh; use Illuminate\Process\PendingProcess as BasePendingProcess; +use Illuminate\Support\Collection; +use Illuminate\Support\Str; /** * PendingProcess handles the construction and execution of SSH commands. @@ -170,6 +172,15 @@ public function start(array|string|null $command = null, ?callable $output = nul return parent::start($command, $output); } + /** + * Get the fake handler for the given command, if applicable. + */ + protected function fakeFor(string $command) + { + return (new Collection($this->fakeHandlers)) + ->first(fn ($handler, $pattern): bool => $pattern === '*' || Str::contains($command, $pattern)); + } + /** * Convert the command to a Symfony Process object. */ diff --git a/tests/ProcessSshTest.php b/tests/ProcessSshTest.php index ebe5e26..7c3762a 100644 --- a/tests/ProcessSshTest.php +++ b/tests/ProcessSshTest.php @@ -294,3 +294,31 @@ 'whoami', ]); })->throws(InvalidArgumentException::class, 'Cannot pipe processes with SSH enabled.'); + +it('fake result Process::run', function (): void { + Process::fake([ + 'ls -al' => Process::result( + output: 'test', + errorOutput: '', + exitCode: 1, + ), + ]); + + $process = Process::ssh($this->basicSshConfig)->run('ls -al'); + + expect($process->output())->toBe("test\n"); +}); + +it('fake result Process::start', function (): void { + Process::fake([ + 'ls -al' => Process::result( + output: 'test', + errorOutput: '', + exitCode: 1, + ), + ]); + + $process = Process::ssh($this->basicSshConfig)->start('ls -al'); + + expect($process->wait()->output())->toBe("test\n"); +});