Skip to content

fix: resolve ChildProcess dynamic property warnings #533

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

Closed
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
11 changes: 8 additions & 3 deletions src/ChildProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class ChildProcess implements ChildProcessContract

public readonly bool $persistent;

protected array $settings = [];

final public function __construct(protected Client $client) {}

public function get(?string $alias = null): ?self
Expand Down Expand Up @@ -146,10 +148,13 @@ protected function fromRuntimeProcess($process)
$this->pid = $process['pid'];
}

foreach ($process['settings'] as $key => $value) {
$this->{$key} = $value;
}
$this->settings = $process['settings'] ?? [];

return $this;
}

public function __get($name)
{
return $this->settings[$name] ?? null;
Copy link
Member

@PeteBishwhip PeteBishwhip Mar 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your first contribution! 🚀

This to me is quite ambiguous. Although quite an elegant way of ensuring fatal errors wouldn't occur, it's not clear and obvious that calling $childProcess->something is meant to be a setting value only.

Could we add an $iniSettings property explicitly instead? This is far less ambiguous and keeps that separation.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I see whtat you mean. I don't know much about the expected values, but I think we could either set defaults like this:

protected function fromRuntimeProcess($process)
{
    $this->pid = $process['pid'] ?? 0;
    $this->alias = $process['settings']['alias'] ?? '';
    $this->cmd = $process['settings']['cmd'] ?? [];
    $this->cwd = $process['settings']['cwd'] ?? null;
    $this->env = $process['settings']['env'] ?? null;
    $this->persistent = $process['settings']['persistent'] ?? false;
    $this->iniSettings = $process['settings']['iniSettings'] ?? [];
    // Could also make all the values nullable and set them to null.

    return $this;
}

or checking if each are set

protected function fromRuntimeProcess($process)
{
    if (isset($process['pid'])) {
        $this->pid = $process['pid'];
    }

    if (isset($process['settings']['alias'])) {
        $this->alias = $process['settings']['alias'];
    }

    if (isset($process['settings']['cmd'])) {
        $this->cmd = $process['settings']['cmd'];
    }

    if (isset($process['settings']['cwd'])) {
        $this->cwd = $process['settings']['cwd'];
    }

    if (isset($process['settings']['env'])) {
        $this->env = $process['settings']['env'];
    }

    if (isset($process['settings']['persistent'])) {
        $this->persistent = $process['settings']['persistent'];
    }

    if (isset($process['settings']['iniSettings'])) {
        $this->iniSettings = $process['settings']['iniSettings'];
    }

    return $this;
}

Another probably less desirable option is to just leave the code as it was and set a class attribute allowing dynamic properties to be set
https://www.php.net/manual/en/class.allowdynamicproperties.php

What do you think?

}
}
196 changes: 109 additions & 87 deletions tests/ChildProcess/ChildProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,121 +6,143 @@
use Native\Laravel\Client\Client;
use Native\Laravel\Facades\ChildProcess;

beforeEach(function () {
Http::fake();
describe('API', function () {
beforeEach(function () {
Http::fake();

$mock = Mockery::mock(ChildProcessImplement::class, [resolve(Client::class)])
->makePartial()
->shouldAllowMockingProtectedMethods();
$mock = Mockery::mock(ChildProcessImplement::class, [resolve(Client::class)])
->makePartial()
->shouldAllowMockingProtectedMethods();

$this->instance(ChildProcessImplement::class, $mock->allows([
'fromRuntimeProcess' => $mock,
]));
});
$this->instance(ChildProcessImplement::class, $mock->allows([
'fromRuntimeProcess' => $mock,
]));
});

it('can start a child process', function () {
ChildProcess::start('foo bar', 'some-alias', 'path/to/dir', ['baz' => 'zah']);
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'];
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 php command', function () {
ChildProcess::php("-r 'sleep(5);'", 'some-alias', ['baz' => 'zah']);
it('can start a php command', function () {
ChildProcess::php("-r 'sleep(5);'", 'some-alias', ['baz' => 'zah']);

Http::assertSent(function (Request $request) {
return $request->url() === 'http://localhost:4000/api/child-process/start-php' &&
$request['alias'] === 'some-alias' &&
$request['cmd'] === ["-r 'sleep(5);'"] &&
$request['cwd'] === base_path() &&
$request['env'] === ['baz' => 'zah'];
Http::assertSent(function (Request $request) {
return $request->url() === 'http://localhost:4000/api/child-process/start-php' &&
$request['alias'] === 'some-alias' &&
$request['cmd'] === ["-r 'sleep(5);'"] &&
$request['cwd'] === base_path() &&
$request['env'] === ['baz' => 'zah'];
});
});
});

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

Http::assertSent(function (Request $request) {
return $request->url() === 'http://localhost:4000/api/child-process/start-php' &&
$request['alias'] === 'some-alias' &&
$request['cmd'] === ['artisan', 'foo:bar --verbose'] &&
$request['cwd'] === base_path() &&
$request['env'] === ['baz' => 'zah'];
Http::assertSent(function (Request $request) {
return $request->url() === 'http://localhost:4000/api/child-process/start-php' &&
$request['alias'] === 'some-alias' &&
$request['cmd'] === ['artisan', 'foo:bar --verbose'] &&
$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']);
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']);
});
ChildProcess::start(['foo', 'baz'], 'some-alias');
Http::assertSent(fn(Request $request) => $request['cmd'] === ['foo', 'baz']);
});

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

ChildProcess::php(['-r', "'sleep(5);'"], 'some-alias');
Http::assertSent(fn (Request $request) => $request['cmd'] === ['-r', "'sleep(5);'"]);
});
ChildProcess::php(['-r', "'sleep(5);'"], 'some-alias');
Http::assertSent(fn(Request $request) => $request['cmd'] === ['-r', "'sleep(5);'"]);
});

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'] === ['artisan', 'foo:bar']);
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'] === ['artisan', 'foo:bar']);

ChildProcess::artisan(['foo:baz'], 'some-alias');
Http::assertSent(fn (Request $request) => $request['cmd'] === ['artisan', 'foo:baz']);
});
ChildProcess::artisan(['foo:baz'], 'some-alias');
Http::assertSent(fn(Request $request) => $request['cmd'] === ['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');
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());
});
ChildProcess::start(['foo', 'bar'], 'some-alias');
Http::assertSent(fn(Request $request) => $request['cwd'] === base_path());
});

it('can stop a child process', function () {
ChildProcess::stop('some-alias');
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';
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');
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';
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 process as persistent', function () {
ChildProcess::start('foo bar', 'some-alias', persistent: true);
Http::assertSent(fn(Request $request) => $request['persistent'] === true);
});

it('can mark a php command as persistent', function () {
ChildProcess::php("-r 'sleep(5);'", 'some-alias', persistent: true);
Http::assertSent(fn (Request $request) => $request['persistent'] === true);
});
it('can mark a php command as persistent', function () {
ChildProcess::php("-r 'sleep(5);'", '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('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);
});
});

it('marks the process as non-persistent by default', function () {
ChildProcess::start('foo bar', 'some-alias');
Http::assertSent(fn (Request $request) => $request['persistent'] === false);
describe('Hydration', function () {
it('hydrates runtime properties from API responses', function () {
$processData = [
'pid' => 1234,
'settings' => [
'iniSettings' => ['memory_limit' => '256M'],
],
];
Http::fake([
'http://localhost:4000/api/child-process/start-php' => Http::response($processData, 200),
]);

$childProcess = new ChildProcessImplement(resolve(Client::class));

$result = $childProcess->php("-r 'sleep(5);'", 'some-alias');
expect($result->pid)->toBe(1234);
expect($result->iniSettings)->toBe(['memory_limit' => '256M']);
});
});
Loading