Skip to content

Commit f243db7

Browse files
Add testing shorthands for fakes (#52840)
1 parent f859f7b commit f243db7

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/Illuminate/Process/PendingProcess.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,10 @@ protected function resolveSynchronousFake(string $command, Closure $fake)
364364
{
365365
$result = $fake($this);
366366

367+
if (is_int($result)) {
368+
return (new FakeProcessResult(exitCode: $result))->withCommand($command);
369+
}
370+
367371
if (is_string($result) || is_array($result)) {
368372
return (new FakeProcessResult(output: $result))->withCommand($command);
369373
}
@@ -373,6 +377,7 @@ protected function resolveSynchronousFake(string $command, Closure $fake)
373377
$result instanceof FakeProcessResult => $result->withCommand($command),
374378
$result instanceof FakeProcessDescription => $result->toProcessResult($command),
375379
$result instanceof FakeProcessSequence => $this->resolveSynchronousFake($command, fn () => $result()),
380+
$result instanceof \Throwable => throw $result,
376381
default => throw new LogicException('Unsupported synchronous process fake result provided.'),
377382
};
378383
}

tests/Process/ProcessTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,16 @@ public function testProcessFakeExitCodes()
178178
$this->assertFalse($result->successful());
179179
}
180180

181+
public function testProcessFakeExitCodeShorthand()
182+
{
183+
$factory = new Factory;
184+
$factory->fake(['ls -la' => 1]);
185+
186+
$result = $factory->run('ls -la');
187+
$this->assertSame(1, $result->exitCode());
188+
$this->assertFalse($result->successful());
189+
}
190+
181191
public function testBasicProcessFakeWithCustomOutput()
182192
{
183193
$factory = new Factory;
@@ -389,6 +399,18 @@ public function testStrayProcessesActuallyRunByDefault()
389399
$this->assertTrue(str_contains($result->output(), 'ProcessTest.php'));
390400
}
391401

402+
public function testProcessFakeThrowShorthand()
403+
{
404+
$this->expectException(\RuntimeException::class);
405+
$this->expectExceptionMessage('fake exception message');
406+
407+
$factory = new Factory;
408+
409+
$factory->fake(['cat me' => new \RuntimeException('fake exception message')]);
410+
411+
$factory->run('cat me');
412+
}
413+
392414
public function testFakeProcessesCanThrow()
393415
{
394416
$this->expectException(ProcessFailedException::class);

0 commit comments

Comments
 (0)