Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add result shorthands for Process fakes #52840

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Illuminate/Process/PendingProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,10 @@ protected function resolveSynchronousFake(string $command, Closure $fake)
{
$result = $fake($this);

if (is_int($result)) {
return (new FakeProcessResult(exitCode: $result))->withCommand($command);
}

if (is_string($result) || is_array($result)) {
return (new FakeProcessResult(output: $result))->withCommand($command);
}
Expand All @@ -373,6 +377,7 @@ protected function resolveSynchronousFake(string $command, Closure $fake)
$result instanceof FakeProcessResult => $result->withCommand($command),
$result instanceof FakeProcessDescription => $result->toProcessResult($command),
$result instanceof FakeProcessSequence => $this->resolveSynchronousFake($command, fn () => $result()),
$result instanceof \Throwable => throw $result,
default => throw new LogicException('Unsupported synchronous process fake result provided.'),
};
}
Expand Down
22 changes: 22 additions & 0 deletions tests/Process/ProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,16 @@ public function testProcessFakeExitCodes()
$this->assertFalse($result->successful());
}

public function testProcessFakeExitCodeShorthand()
{
$factory = new Factory;
$factory->fake(['ls -la' => 1]);

$result = $factory->run('ls -la');
$this->assertSame(1, $result->exitCode());
$this->assertFalse($result->successful());
}

public function testBasicProcessFakeWithCustomOutput()
{
$factory = new Factory;
Expand Down Expand Up @@ -389,6 +399,18 @@ public function testStrayProcessesActuallyRunByDefault()
$this->assertTrue(str_contains($result->output(), 'ProcessTest.php'));
}

public function testProcessFakeThrowShorthand()
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('fake exception message');

$factory = new Factory;

$factory->fake(['cat me' => new \RuntimeException('fake exception message')]);

$factory->run('cat me');
}

public function testFakeProcessesCanThrow()
{
$this->expectException(ProcessFailedException::class);
Expand Down