Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Add syntax sugar to the Process::pipe method #46745

Merged
merged 3 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/Illuminate/Process/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,16 @@ public function pool(callable $callback)
/**
* Start defining a series of piped processes.
*
* @param callable $callback
* @param callable|array $callback
* @return \Illuminate\Process\Pipe
*/
public function pipe(callable $callback, ?callable $output = null)
public function pipe(callable|array $callback, ?callable $output = null)
{
return (new Pipe($this, $callback))->run(output: $output);
return is_array($callback)
taylorotwell marked this conversation as resolved.
Show resolved Hide resolved
? (new Pipe($this, fn ($pipe) => collect($callback)->each(
fn ($command) => $pipe->command($command)
)))->run(output: $output)
: (new Pipe($this, $callback))->run(output: $output);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Support/Facades/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* @method static \Illuminate\Process\Factory assertDidntRun(\Closure|string $callback)
* @method static \Illuminate\Process\Factory assertNothingRan()
* @method static \Illuminate\Process\Pool pool(callable $callback)
* @method static \Illuminate\Process\Pipe pipe(callable $callback, callable|null $output = null)
* @method static \Illuminate\Process\Pipe pipe(callable|array $callback, callable|null $output = null)
* @method static \Illuminate\Process\ProcessPoolResults concurrently(callable $callback, callable|null $output = null)
* @method static \Illuminate\Process\PendingProcess newPendingProcess()
* @method static void macro(string $name, object|callable $macro)
Expand Down
38 changes: 38 additions & 0 deletions tests/Process/ProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,44 @@ public function testProcessPipeFailed()
$this->assertTrue($pipe->failed());
}

public function testProcessSimplePipe()
{
if (windows_os()) {
$this->markTestSkipped('Requires Linux.');
}

$factory = new Factory;
$factory->fake([
'cat *' => "Hello, world\nfoo\nbar",
]);

$pipe = $factory->pipe([
'cat test',
'grep -i "foo"',
]);

$this->assertSame("foo\n", $pipe->output());
}

public function testProcessSimplePipeFailed()
{
if (windows_os()) {
$this->markTestSkipped('Requires Linux.');
}

$factory = new Factory;
$factory->fake([
'cat *' => $factory->result(exitCode: 1),
]);

$pipe = $factory->pipe([
'cat test',
'grep -i "foo"',
]);

$this->assertTrue($pipe->failed());
}

public function testFakeInvokedProcessOutputWithLatestOutput()
{
$factory = new Factory;
Expand Down