Skip to content

child process - sanity tests and tweaks #392

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

Merged
Merged
Show file tree
Hide file tree
Changes from 10 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
20 changes: 17 additions & 3 deletions src/ChildProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,39 @@ public function all(): array

return $hydrated;
}

public function start(
string|array $cmd
string $alias,
array $cmd,
?string $cwd = null,
?array $env = null,
bool $persistent = false
): static {
$cwd = $cwd ?? base_path();

if (is_string($cmd)) {
// When a string is passed, explode it on the space
$cmd = array_values(array_filter(explode(' ', $cmd)));
}

$process = $this->client->post('child-process/start', [
'alias' => $alias,
'cmd' => $cmd,
'cwd' => base_path(),
'cwd' => $cwd,
'env' => $env,
'persistent' => $persistent,
])->json();

return $this->fromRuntimeProcess($process);
}

public function artisan(string|array $cmd, string $alias, ?array $env = null, ?bool $persistent = false): self
{
$cmd = [PHP_BINARY, 'artisan', ...(array) $cmd];

return $this->start($cmd, $alias, env: $env, persistent: $persistent);
}

public function stop(?string $alias = null): void
{
$this->client->post('child-process/stop', [
Expand Down
96 changes: 96 additions & 0 deletions tests/ChildProcess/ChildProcessTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
use Native\Laravel\Facades\ChildProcess;

beforeEach(function () {
Http::fake();
});

it('can start a child process', function () {
ChildProcess::start('foo bar', 'some-alias', 'path/to/dir', ['baz' => 'zah']);

Http::assertSent(function (Request $request) {
return $request->url() === 'http://localhost:4000/api/child-process/start' &&
$request['alias'] === 'some-alias' &&
$request['cmd'] === ['foo', 'bar'] &&
$request['cwd'] === 'path/to/dir' &&
$request['env'] === ['baz' => 'zah'];
});
});

it('can start a artisan command', function () {
ChildProcess::artisan('foo:bar', 'some-alias', ['baz' => 'zah']);

Http::assertSent(function (Request $request) {
return $request->url() === 'http://localhost:4000/api/child-process/start' &&
$request['alias'] === 'some-alias' &&
$request['cmd'] === [PHP_BINARY, 'artisan', 'foo:bar'] &&
$request['cwd'] === base_path() &&
$request['env'] === ['baz' => 'zah'];
});
});

it('accepts either a string or a array as start command argument', function () {
ChildProcess::start('foo bar', 'some-alias');
Http::assertSent(fn (Request $request) => $request['cmd'] === ['foo', 'bar']);

ChildProcess::start(['foo', 'baz'], 'some-alias');
Http::assertSent(fn (Request $request) => $request['cmd'] === ['foo', 'baz']);
});

it('accepts either a string or a array as artisan command argument', function () {
ChildProcess::artisan('foo:bar', 'some-alias');
Http::assertSent(fn (Request $request) => $request['cmd'] === [PHP_BINARY, 'artisan', 'foo:bar']);

ChildProcess::artisan(['foo:baz'], 'some-alias');
Http::assertSent(fn (Request $request) => $request['cmd'] === [PHP_BINARY, 'artisan', 'foo:baz']);
});

it('sets the cwd to the base path if none was given', function () {
ChildProcess::start(['foo', 'bar'], 'some-alias', cwd: 'path/to/dir');
Http::assertSent(fn (Request $request) => $request['cwd'] === 'path/to/dir');

ChildProcess::start(['foo', 'bar'], 'some-alias');
Http::assertSent(fn (Request $request) => $request['cwd'] === base_path());
});

it('filters double spaces when exploding a command string', function () {
ChildProcess::start('foo bar baz bak', 'some-alias');
Http::assertSent(fn (Request $request) => $request['cmd'] === ['foo', 'bar', 'baz', 'bak']);
});

it('can stop a child process', function () {
ChildProcess::stop('some-alias');

Http::assertSent(function (Request $request) {
return $request->url() === 'http://localhost:4000/api/child-process/stop' &&
$request['alias'] === 'some-alias';
});
});

it('can send messages to a child process', function () {
ChildProcess::message('some-message', 'some-alias');

Http::assertSent(function (Request $request) {
return $request->url() === 'http://localhost:4000/api/child-process/message' &&
$request['alias'] === 'some-alias' &&
$request['message'] === '"some-message"';
});
});

it('can mark a process as persistent', function () {
ChildProcess::start('foo bar', 'some-alias', persistent: true);
Http::assertSent(fn (Request $request) => $request['persistent'] === true);
});

it('can mark a artisan command as persistent', function () {
ChildProcess::artisan('foo:bar', 'some-alias', persistent: true);
Http::assertSent(fn (Request $request) => $request['persistent'] === true);
});

it('marks the process as non-persistent by default', function () {
ChildProcess::start('foo bar', 'some-alias');
Http::assertSent(fn (Request $request) => $request['persistent'] === false);
});