Skip to content

Commit

Permalink
Add testing shorthands for fakes (#52840)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmccreary authored Sep 18, 2024
1 parent f859f7b commit f243db7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
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

0 comments on commit f243db7

Please sign in to comment.