From f243db71351433dd5050c1a3b445be7fcf156c52 Mon Sep 17 00:00:00 2001 From: Jason McCreary Date: Wed, 18 Sep 2024 10:56:49 -0400 Subject: [PATCH] Add testing shorthands for fakes (#52840) --- src/Illuminate/Process/PendingProcess.php | 5 +++++ tests/Process/ProcessTest.php | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/Illuminate/Process/PendingProcess.php b/src/Illuminate/Process/PendingProcess.php index 48a28f6b335a..3a4309f56d21 100644 --- a/src/Illuminate/Process/PendingProcess.php +++ b/src/Illuminate/Process/PendingProcess.php @@ -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); } @@ -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.'), }; } diff --git a/tests/Process/ProcessTest.php b/tests/Process/ProcessTest.php index 6e5790cfa590..841860cacd26 100644 --- a/tests/Process/ProcessTest.php +++ b/tests/Process/ProcessTest.php @@ -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; @@ -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);