Skip to content

Commit

Permalink
Feat - Handle fake over ssh
Browse files Browse the repository at this point in the history
  • Loading branch information
lguichard committed Jan 7, 2025
1 parent 7dd15cf commit add8ba1
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/PendingProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
45 changes: 45 additions & 0 deletions tests/ProcessSshTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});

0 comments on commit add8ba1

Please sign in to comment.