From 8048c9837363468b846385adae2d1cca93adbd15 Mon Sep 17 00:00:00 2001 From: Riley19280 Date: Tue, 15 Oct 2024 09:59:37 -0400 Subject: [PATCH] [11.x] Add successful and failed methods to `ProcessPoolResults` (#53160) * Add successful and failed * formatting * formatting * formatting * formatting --------- Co-authored-by: Riley Aven Co-authored-by: Mior Muhammad Zaki Co-authored-by: Taylor Otwell --- src/Illuminate/Process/ProcessPoolResults.php | 20 +++++++++++++++ tests/Process/ProcessTest.php | 25 +++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/src/Illuminate/Process/ProcessPoolResults.php b/src/Illuminate/Process/ProcessPoolResults.php index 840a109315d7..debd3e09db6e 100644 --- a/src/Illuminate/Process/ProcessPoolResults.php +++ b/src/Illuminate/Process/ProcessPoolResults.php @@ -25,6 +25,26 @@ public function __construct(array $results) $this->results = $results; } + /** + * Determine if all of the processes in the pool were successful. + * + * @return bool + */ + public function successful() + { + return $this->collect()->every(fn ($p) => $p->successful()); + } + + /** + * Determine if any of the processes in the pool failed. + * + * @return bool + */ + public function failed() + { + return ! $this->successful(); + } + /** * Get the results as a collection. * diff --git a/tests/Process/ProcessTest.php b/tests/Process/ProcessTest.php index 841860cacd26..8b51dad64f9a 100644 --- a/tests/Process/ProcessTest.php +++ b/tests/Process/ProcessTest.php @@ -47,6 +47,31 @@ public function testProcessPool() $this->assertTrue(str_contains($results[0]->output(), 'ProcessTest.php')); $this->assertTrue(str_contains($results[1]->output(), 'ProcessTest.php')); + + $this->assertTrue($results->successful()); + } + + public function testProcessPoolFailed() + { + $factory = new Factory; + + $factory->fake([ + 'cat *' => $factory->result(exitCode: 1), + ]); + + $pool = $factory->pool(function ($pool) { + return [ + $pool->path(__DIR__)->command($this->ls()), + $pool->path(__DIR__)->command('cat test'), + ]; + }); + + $results = $pool->start()->wait(); + + $this->assertTrue($results[0]->successful()); + $this->assertTrue($results[1]->failed()); + + $this->assertTrue($results->failed()); } public function testInvokedProcessPoolCount()