Skip to content

Commit 294626b

Browse files
committed
Add missing waitUntil method to FakeInvokedProcess
1 parent 7b5e185 commit 294626b

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

src/Illuminate/Contracts/Process/InvokedProcess.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,12 @@ public function latestErrorOutput();
6161
* @return \Illuminate\Process\ProcessResult
6262
*/
6363
public function wait(?callable $output = null);
64+
65+
/**
66+
* Wait until the given callback returns true.
67+
*
68+
* @param callable|null $output
69+
* @return \Illuminate\Process\ProcessResult
70+
*/
71+
public function waitUntil(?callable $output = null);
6472
}

src/Illuminate/Process/FakeInvokedProcess.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,31 @@ public function wait(?callable $output = null)
286286
return $this->process->toProcessResult($this->command);
287287
}
288288

289+
/**
290+
* Wait until the given callback returns true.
291+
*
292+
* @param callable|null $output
293+
* @return \Illuminate\Contracts\Process\ProcessResult
294+
*/
295+
public function waitUntil(?callable $output = null)
296+
{
297+
$this->outputHandler = $output ?: $this->outputHandler;
298+
299+
if (! $this->outputHandler) {
300+
$this->remainingRunIterations = 0;
301+
302+
return $this->predictProcessResult();
303+
}
304+
305+
while ($this->running() && ! call_user_func($this->outputHandler, 'out', $this->latestOutput())) {
306+
//
307+
}
308+
309+
$this->remainingRunIterations = 0;
310+
311+
return $this->process->toProcessResult($this->command);
312+
}
313+
289314
/**
290315
* Get the ultimate process result that will be returned by this "process".
291316
*

tests/Process/ProcessTest.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,77 @@ public function testFakeInvokedProcessOutputWithLatestOutput()
765765
$this->assertEquals("ONE\nTWO\nTHREE\n", $output[2]);
766766
}
767767

768+
public function testFakeInvokedProcessWaitUntil()
769+
{
770+
$factory = new Factory;
771+
772+
$factory->fake(function () use ($factory) {
773+
return $factory->describe()
774+
->output('WAITING')
775+
->output('READY')
776+
->output('DONE')
777+
->runsFor(iterations: 3);
778+
});
779+
780+
$process = $factory->start('echo "WAITING"; sleep 1; echo "READY"; sleep 1; echo "DONE";');
781+
782+
$callbackInvoked = [];
783+
784+
$result = $process->waitUntil(function ($type, $buffer) use (&$callbackInvoked) {
785+
$callbackInvoked[] = $buffer;
786+
return str_contains($buffer, 'READY');
787+
});
788+
789+
$this->assertInstanceOf(ProcessResult::class, $result);
790+
$this->assertTrue($result->successful());
791+
$this->assertContains("WAITING\n", $callbackInvoked);
792+
$this->assertContains("READY\n", $callbackInvoked);
793+
}
794+
795+
public function testFakeInvokedProcessWaitUntilWithNoCallback()
796+
{
797+
$factory = new Factory;
798+
799+
$factory->fake(function () use ($factory) {
800+
return $factory->describe()
801+
->output('OUTPUT');
802+
});
803+
804+
$process = $factory->start('echo "OUTPUT"');
805+
806+
$result = $process->waitUntil();
807+
808+
$this->assertInstanceOf(ProcessResult::class, $result);
809+
$this->assertTrue($result->successful());
810+
$this->assertEquals("OUTPUT\n", $result->output());
811+
}
812+
813+
public function testFakeInvokedProcessWaitUntilStopsWhenConditionMet()
814+
{
815+
$factory = new Factory;
816+
817+
$factory->fake(function () use ($factory) {
818+
return $factory->describe()
819+
->output('STEP1')
820+
->output('TARGET')
821+
->output('STEP3')
822+
->runsFor(iterations: 3);
823+
});
824+
825+
$process = $factory->start('echo "STEP1"; sleep 1; echo "TARGET"; sleep 1; echo "STEP3";');
826+
827+
$callbackCount = 0;
828+
829+
$result = $process->waitUntil(function ($type, $buffer) use (&$callbackCount) {
830+
$callbackCount++;
831+
return str_contains($buffer, 'TARGET');
832+
});
833+
834+
$this->assertInstanceOf(ProcessResult::class, $result);
835+
$this->assertTrue($result->successful());
836+
$this->assertEquals(2, $callbackCount);
837+
}
838+
768839
public function testBasicFakeAssertions()
769840
{
770841
$factory = new Factory;

0 commit comments

Comments
 (0)