diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 3d20ec9..89e89c6 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -9,8 +9,8 @@ jobs: fail-fast: true matrix: os: [ubuntu-latest] - php: ['8.3'] - dependency-version: [prefer-lowest, prefer-stable] + php: ['8.2', '8.3', '8.4'] + dependency-version: [prefer-stable] name: Tests P${{ matrix.php }} - ${{ matrix.os }} - ${{ matrix.dependency-version }} diff --git a/README.md b/README.md index a4431ee..5983aea 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,8 @@ To execute a command over SSH, use the `Process` facade: ### Basic Usage ```php +use Illuminate\Support\Facades\Process; + $result = Process::ssh([ 'host' => '192.168.1.10', 'user' => 'username', diff --git a/composer.json b/composer.json index 4107933..8c4da15 100644 --- a/composer.json +++ b/composer.json @@ -11,14 +11,13 @@ } ], "require": { - "php": "^8.0", + "php": "^8.1", "illuminate/support": "^10.0 || ^11.0" }, "require-dev": { "laravel/pint": "^1.18.1", "orchestra/testbench": "^9.9", "pestphp/pest": "^3.5.1", - "pestphp/pest-plugin-type-coverage": "^3.1", "pestphp/pest-plugin-watch": "^3.0", "phpstan/phpstan": "^1.12.7", "rector/rector": "^1.2.8", @@ -58,6 +57,7 @@ "test:types": "phpstan analyse --ansi", "test:unit": "pest --colors=always --parallel", "test": [ + "rector", "composer lint", "@test:types", "@test:unit" diff --git a/src/PendingProcess.php b/src/PendingProcess.php index 2f0982f..5a43226 100644 --- a/src/PendingProcess.php +++ b/src/PendingProcess.php @@ -36,11 +36,11 @@ public function command(array|string $command) /** * Set configuration for the SSH connection. */ - public function setConfig(array $config, bool $handleSsh) + public function setConfig(array $config, bool $handleSsh): static { $this->handleSsh = $handleSsh; - if (! $this->handleSsh && empty($config)) { + if (! $this->handleSsh && $config === []) { return $this; } @@ -72,7 +72,7 @@ public function setConfig(array $config, bool $handleSsh) */ public function buildCommand(array|string|null $command): array|string|null { - if (! $command) { + if ($command === '' || $command === '0' || $command === [] || $command === null) { return $command; } @@ -141,7 +141,7 @@ protected function buildTarget(): string * @param mixed $command The command to check. * @return bool True if the command is invalid, otherwise false. */ - protected function exceptionCondition($command): bool + protected function exceptionCondition(mixed $command): bool { return is_array($command) && $this->handleSsh; } @@ -177,8 +177,6 @@ protected function toSymfonyProcess(array|string|null $command) { $command = $this->buildCommand($command); - $process = parent::toSymfonyProcess($command); - - return $process; + return parent::toSymfonyProcess($command); } } diff --git a/src/Providers/ProcessSshServiceProvider.php b/src/Providers/ProcessSshServiceProvider.php index 89a3edb..f8b0937 100644 --- a/src/Providers/ProcessSshServiceProvider.php +++ b/src/Providers/ProcessSshServiceProvider.php @@ -10,8 +10,6 @@ class ProcessSshServiceProvider extends ServiceProvider { public function register(): void { - $this->app->bind(Factory::class, function () { - return new ProcessSsh; - }); + $this->app->bind(Factory::class, fn (): \Bagel\ProcessSsh\ProcessSsh => new ProcessSsh); } } diff --git a/tests/ProcessSshTest.php b/tests/ProcessSshTest.php index 00b9e84..ebe5e26 100644 --- a/tests/ProcessSshTest.php +++ b/tests/ProcessSshTest.php @@ -7,7 +7,7 @@ use Illuminate\Support\Facades\Process; use Orchestra\Testbench\TestCase; -uses(TestCase::class)->beforeEach(function () { +uses(TestCase::class)->beforeEach(function (): void { $this->app->register(ProcessSshServiceProvider::class); $this->basicSshConfig = [ @@ -17,7 +17,7 @@ ]; }); -it('process config set', function () { +it('process config set', function (): void { $process = Process::ssh([ 'host' => '192.178.0.1', 'user' => 'ubuntu', @@ -39,7 +39,7 @@ expect($process->sshConfig()['private_key'])->toBe('/path/to/key'); }); -it('process add extra options', function () { +it('process add extra options', function (): void { $process = Process::ssh([ 'host' => '192.178.0.1', 'user' => 'ubuntu', @@ -51,7 +51,7 @@ ]); }); -it('process disableStrictHostKeyChecking', function () { +it('process disableStrictHostKeyChecking', function (): void { $process = Process::ssh([ 'host' => '192.178.0.1', 'user' => 'ubuntu', @@ -63,7 +63,7 @@ ]); }); -it('process useMultiplexing', function () { +it('process useMultiplexing', function (): void { $process = Process::ssh([ 'host' => '192.168.85.5', 'user' => 'ubuntu', @@ -86,11 +86,11 @@ ]); }); -it('exception thrown when host is not set', function () { +it('exception thrown when host is not set', function (): void { Process::ssh([])->run('ls'); })->throws(InvalidArgumentException::class, 'Host is required for SSH connections.'); -it('Process run without user / password not set', function () { +it('Process run without user / password not set', function (): void { Process::fake(); $process = Process::ssh([ @@ -104,7 +104,7 @@ Process::assertRan('ls -al'); }); -it('Process run all parameters', function () { +it('Process run all parameters', function (): void { Process::fake(); $process = Process::ssh([ @@ -125,7 +125,7 @@ Process::assertRan('ls -al'); }); -it('Process run with private key', function () { +it('Process run with private key', function (): void { Process::fake(); $process = Process::ssh([ @@ -142,33 +142,29 @@ Process::assertRan('ls -al'); }); -it('Process can run normaly', function () { +it('Process can run normaly', function (): void { Process::fake(); Process::run('ls'); Process::assertRan('ls'); - Process::assertRan(function (PendingProcess $process, FakeProcessResult $result) { - return $process->command === 'ls' && - $process->timeout === 60; - }); + Process::assertRan(fn (PendingProcess $process, FakeProcessResult $result): bool => $process->command === 'ls' && + $process->timeout === 60); }); -it('Process can start normaly', function () { +it('Process can start normaly', function (): void { Process::fake(); Process::start('ls'); Process::assertRan('ls'); - Process::assertRan(function (PendingProcess $process, FakeProcessResult $result) { - return $process->command === 'ls' && - $process->timeout === 60; - }); + Process::assertRan(fn (PendingProcess $process, FakeProcessResult $result): bool => $process->command === 'ls' && + $process->timeout === 60); }); -it('Process ssh can run', function () { +it('Process ssh can run', function (): void { Process::fake(); Process::ssh([ @@ -181,13 +177,11 @@ Process::assertRan('ls'); - Process::assertRan(function (PendingProcess $process, FakeProcessResult $result) { - return $process->command === 'ls' && - $process->timeout === 60; - }); + Process::assertRan(fn (PendingProcess $process, FakeProcessResult $result): bool => $process->command === 'ls' && + $process->timeout === 60); }); -it('Process ssh can start', function () { +it('Process ssh can start', function (): void { Process::fake(); Process::ssh([ @@ -200,16 +194,14 @@ Process::assertRan('ls'); - Process::assertRan(function (PendingProcess $process, FakeProcessResult $result) { - return $process->command === 'ls' && - $process->timeout === 60; - }); + Process::assertRan(fn (PendingProcess $process, FakeProcessResult $result): bool => $process->command === 'ls' && + $process->timeout === 60); }); -it('invoke process::concurrently', function () { +it('invoke process::concurrently', function (): void { Process::fake(); - $process = Process::concurrently(function (Pool $pool) { + $process = Process::concurrently(function (Pool $pool): void { $pool->command('ls -al'); $pool->command('whoami'); }); @@ -221,10 +213,10 @@ Process::assertRan('whoami'); }); -it('invoke process::concurrently ssh', function () { +it('invoke process::concurrently ssh', function (): void { Process::fake(); - $process = Process::ssh($this->basicSshConfig)->concurrently(function (Pool $pool) { + $process = Process::ssh($this->basicSshConfig)->concurrently(function (Pool $pool): void { $pool->command('ls -al'); $pool->command('whoami'); }); @@ -234,10 +226,10 @@ }); -it('invoke process::pool', function () { +it('invoke process::pool', function (): void { Process::fake(); - $process = Process::pool(function (Pool $pool) { + $process = Process::pool(function (Pool $pool): void { $pool->command('ls -al'); $pool->command('whoami'); }); @@ -250,10 +242,10 @@ Process::assertRan('whoami'); }); -it('invoke process::pool ssh', function () { +it('invoke process::pool ssh', function (): void { Process::fake(); - $process = Process::ssh($this->basicSshConfig)->pool(function (Pool $pool) { + $process = Process::ssh($this->basicSshConfig)->pool(function (Pool $pool): void { $pool->command('ls -al'); $pool->command('whoami'); }); @@ -264,16 +256,28 @@ }); -it('exception thrown process run with array', function () { +it('exception thrown process run with array', function (): void { Process::fake(); Process::ssh([ 'host' => '127.0.0.1', ])->run(['ls', '-al']); - })->throws(InvalidArgumentException::class, 'Array commands are not supported for SSH connections.'); -it('exception thrown process start with array', function () { +it('can\'t pipe processes with SSH enabled', function (): void { + Process::fake(); + + Process::ssh([ + 'host' => 'example.com', + 'user' => 'ubuntu', + 'password' => 'password', + 'port' => 22, + ])->pipe(function (): void { + // + }); +})->throws(InvalidArgumentException::class, 'Cannot pipe processes with SSH enabled.'); + +it('exception thrown process start with array', function (): void { Process::fake(); Process::ssh([ @@ -282,7 +286,7 @@ })->throws(InvalidArgumentException::class, 'Array commands are not supported for SSH connections.'); -it('invoke process::pipe', function () { +it('invoke process::pipe', function (): void { Process::fake(); $process = Process::ssh($this->basicSshConfig)->pipe([ diff --git a/tests/Providers/ProviderTest.php b/tests/Providers/ProviderTest.php index 5572c3c..46dc388 100644 --- a/tests/Providers/ProviderTest.php +++ b/tests/Providers/ProviderTest.php @@ -5,11 +5,11 @@ use Illuminate\Process\Factory; use Orchestra\Testbench\TestCase; -uses(TestCase::class)->beforeEach(function () { +uses(TestCase::class)->beforeEach(function (): void { $this->app->register(ProcessSshServiceProvider::class); }); -it('binds ProcessSsh to Factory', function () { +it('binds ProcessSsh to Factory', function (): void { $factory = app(Factory::class); expect($factory)->toBeInstanceOf(ProcessSsh::class);