From add8ba15fcdf7aed60a299f6fb9e337e0b9ec191 Mon Sep 17 00:00:00 2001 From: lguichard Date: Tue, 7 Jan 2025 09:42:24 +0100 Subject: [PATCH] Feat - Handle fake over ssh --- src/PendingProcess.php | 19 +++++++++++++++++ tests/ProcessSshTest.php | 45 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/src/PendingProcess.php b/src/PendingProcess.php index 5a43226..acb886b 100644 --- a/src/PendingProcess.php +++ b/src/PendingProcess.php @@ -170,6 +170,25 @@ public function start(array|string|null $command = null, ?callable $output = nul return parent::start($command, $output); } + /** + * Specify the fake process result handlers for the pending process. + */ + public function withFakeHandlers(array $fakeHandlers) + { + if (! $this->handleSsh) { + $this->fakeHandlers = $fakeHandlers; + } else { + $updatedArray = []; + foreach ($fakeHandlers as $key => $value) { + $newKey = $this->buildCommand($key); + $updatedArray[$newKey] = $value; + } + $this->fakeHandlers = $updatedArray; + } + + return $this; + } + /** * Convert the command to a Symfony Process object. */ diff --git a/tests/ProcessSshTest.php b/tests/ProcessSshTest.php index ebe5e26..507fe80 100644 --- a/tests/ProcessSshTest.php +++ b/tests/ProcessSshTest.php @@ -294,3 +294,48 @@ '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"); +}); + +it('fake result Process::concurrently', function (): void { + Process::fake([ + '*' => Process::result( + output: 'test', + errorOutput: '', + exitCode: 1, + ), + ]); + + $process = Process::ssh($this->basicSshConfig)->concurrently(function (Pool $pool): void { + $pool->command('ls -al'); + $pool->command('whoami'); + }); + + expect($process[0]->output())->toBe("test\n"); +});