|
| 1 | +<?php |
| 2 | + |
| 3 | +use Illuminate\Http\Client\Request; |
| 4 | +use Illuminate\Support\Facades\Http; |
| 5 | +use Mockery; |
| 6 | +use Native\Laravel\ChildProcess as ChildProcessImplement; |
| 7 | +use Native\Laravel\Client\Client; |
| 8 | +use Native\Laravel\Facades\ChildProcess; |
| 9 | + |
| 10 | +beforeEach(function () { |
| 11 | + Http::fake(); |
| 12 | + |
| 13 | + $mock = Mockery::mock(ChildProcessImplement::class, [resolve(Client::class)]) |
| 14 | + ->makePartial() |
| 15 | + ->shouldAllowMockingProtectedMethods(); |
| 16 | + |
| 17 | + $this->instance(ChildProcessImplement::class, $mock->allows([ |
| 18 | + 'fromRuntimeProcess' => $mock, |
| 19 | + ])); |
| 20 | +}); |
| 21 | + |
| 22 | +it('can start a child process', function () { |
| 23 | + ChildProcess::start('foo bar', 'some-alias', 'path/to/dir', ['baz' => 'zah']); |
| 24 | + |
| 25 | + Http::assertSent(function (Request $request) { |
| 26 | + return $request->url() === 'http://localhost:4000/api/child-process/start' && |
| 27 | + $request['alias'] === 'some-alias' && |
| 28 | + $request['cmd'] === ['foo bar'] && |
| 29 | + $request['cwd'] === 'path/to/dir' && |
| 30 | + $request['env'] === ['baz' => 'zah']; |
| 31 | + }); |
| 32 | +}); |
| 33 | + |
| 34 | +it('can start a php command', function () { |
| 35 | + ChildProcess::php("-r 'sleep(5);'", 'some-alias', ['baz' => 'zah']); |
| 36 | + |
| 37 | + Http::assertSent(function (Request $request) { |
| 38 | + return $request->url() === 'http://localhost:4000/api/child-process/start' && |
| 39 | + $request['alias'] === 'some-alias' && |
| 40 | + $request['cmd'] === [PHP_BINARY, "-r 'sleep(5);'"] && |
| 41 | + $request['cwd'] === base_path() && |
| 42 | + $request['env'] === ['baz' => 'zah']; |
| 43 | + }); |
| 44 | +}); |
| 45 | + |
| 46 | +it('can start a artisan command', function () { |
| 47 | + ChildProcess::artisan('foo:bar --verbose', 'some-alias', ['baz' => 'zah']); |
| 48 | + |
| 49 | + Http::assertSent(function (Request $request) { |
| 50 | + return $request->url() === 'http://localhost:4000/api/child-process/start' && |
| 51 | + $request['alias'] === 'some-alias' && |
| 52 | + $request['cmd'] === [PHP_BINARY, 'artisan', 'foo:bar --verbose'] && |
| 53 | + $request['cwd'] === base_path() && |
| 54 | + $request['env'] === ['baz' => 'zah']; |
| 55 | + }); |
| 56 | +}); |
| 57 | + |
| 58 | +it('accepts either a string or a array as start command argument', function () { |
| 59 | + ChildProcess::start('foo bar', 'some-alias'); |
| 60 | + Http::assertSent(fn (Request $request) => $request['cmd'] === ['foo bar']); |
| 61 | + |
| 62 | + ChildProcess::start(['foo', 'baz'], 'some-alias'); |
| 63 | + Http::assertSent(fn (Request $request) => $request['cmd'] === ['foo', 'baz']); |
| 64 | +}); |
| 65 | + |
| 66 | +it('accepts either a string or a array as php command argument', function () { |
| 67 | + ChildProcess::php("-r 'sleep(5);'", 'some-alias'); |
| 68 | + Http::assertSent(fn (Request $request) => $request['cmd'] === [PHP_BINARY, "-r 'sleep(5);'"]); |
| 69 | + |
| 70 | + ChildProcess::php(['-r', "'sleep(5);'"], 'some-alias'); |
| 71 | + Http::assertSent(fn (Request $request) => $request['cmd'] === [PHP_BINARY, '-r', "'sleep(5);'"]); |
| 72 | +}); |
| 73 | + |
| 74 | +it('accepts either a string or a array as artisan command argument', function () { |
| 75 | + ChildProcess::artisan('foo:bar', 'some-alias'); |
| 76 | + Http::assertSent(fn (Request $request) => $request['cmd'] === [PHP_BINARY, 'artisan', 'foo:bar']); |
| 77 | + |
| 78 | + ChildProcess::artisan(['foo:baz'], 'some-alias'); |
| 79 | + Http::assertSent(fn (Request $request) => $request['cmd'] === [PHP_BINARY, 'artisan', 'foo:baz']); |
| 80 | +}); |
| 81 | + |
| 82 | +it('sets the cwd to the base path if none was given', function () { |
| 83 | + ChildProcess::start(['foo', 'bar'], 'some-alias', cwd: 'path/to/dir'); |
| 84 | + Http::assertSent(fn (Request $request) => $request['cwd'] === 'path/to/dir'); |
| 85 | + |
| 86 | + ChildProcess::start(['foo', 'bar'], 'some-alias'); |
| 87 | + Http::assertSent(fn (Request $request) => $request['cwd'] === base_path()); |
| 88 | +}); |
| 89 | + |
| 90 | +it('can stop a child process', function () { |
| 91 | + ChildProcess::stop('some-alias'); |
| 92 | + |
| 93 | + Http::assertSent(function (Request $request) { |
| 94 | + return $request->url() === 'http://localhost:4000/api/child-process/stop' && |
| 95 | + $request['alias'] === 'some-alias'; |
| 96 | + }); |
| 97 | +}); |
| 98 | + |
| 99 | +it('can send messages to a child process', function () { |
| 100 | + ChildProcess::message('some-message', 'some-alias'); |
| 101 | + |
| 102 | + Http::assertSent(function (Request $request) { |
| 103 | + return $request->url() === 'http://localhost:4000/api/child-process/message' && |
| 104 | + $request['alias'] === 'some-alias' && |
| 105 | + $request['message'] === 'some-message'; |
| 106 | + }); |
| 107 | +}); |
| 108 | + |
| 109 | +it('can mark a process as persistent', function () { |
| 110 | + ChildProcess::start('foo bar', 'some-alias', persistent: true); |
| 111 | + Http::assertSent(fn (Request $request) => $request['persistent'] === true); |
| 112 | +}); |
| 113 | + |
| 114 | +it('can mark a php command as persistent', function () { |
| 115 | + ChildProcess::php("-r 'sleep(5);'", 'some-alias', persistent: true); |
| 116 | + Http::assertSent(fn (Request $request) => $request['persistent'] === true); |
| 117 | +}); |
| 118 | + |
| 119 | +it('can mark a artisan command as persistent', function () { |
| 120 | + ChildProcess::artisan('foo:bar', 'some-alias', persistent: true); |
| 121 | + Http::assertSent(fn (Request $request) => $request['persistent'] === true); |
| 122 | +}); |
| 123 | + |
| 124 | +it('marks the process as non-persistent by default', function () { |
| 125 | + ChildProcess::start('foo bar', 'some-alias'); |
| 126 | + Http::assertSent(fn (Request $request) => $request['persistent'] === false); |
| 127 | +}); |
0 commit comments