From 66db3b7442b807b7fb437b60afd18fbecd313902 Mon Sep 17 00:00:00 2001 From: Adrian Dumitrache Date: Fri, 19 Apr 2024 14:21:28 +0300 Subject: [PATCH] Added new method to ProcessFactory --- .../Process/ProcessFactoryInterface.php | 8 +++++ packages/cron-job/CronJobProcessor.php | 10 +++--- .../cron-job/Tests/CronJobProcessorTest.php | 10 +++--- packages/process/ProcessFactory.php | 10 ++++++ packages/process/Tests/ProcessFactoryTest.php | 35 +++++++++++++++++++ 5 files changed, 62 insertions(+), 11 deletions(-) diff --git a/packages/contracts/Process/ProcessFactoryInterface.php b/packages/contracts/Process/ProcessFactoryInterface.php index 8dad99472..5ce514277 100644 --- a/packages/contracts/Process/ProcessFactoryInterface.php +++ b/packages/contracts/Process/ProcessFactoryInterface.php @@ -7,4 +7,12 @@ interface ProcessFactoryInterface { public function create(array $command, ?string $cwd = null, ?array $env = null, $input = null, ?float $timeout = 60): Process; + + public function createFromShellCommandLine( + string $command, + ?string $cwd = null, + ?array $env = null, + $input = null, + ?float $timeout = 60 + ): Process; } diff --git a/packages/cron-job/CronJobProcessor.php b/packages/cron-job/CronJobProcessor.php index b21c1757d..35f3fba2b 100644 --- a/packages/cron-job/CronJobProcessor.php +++ b/packages/cron-job/CronJobProcessor.php @@ -49,12 +49,10 @@ public function process(CronJobExecution $execution): void $execution->start(); $manager->flush(); - $process = $this->processFactory->create( - [ - $this->parameterBag->resolveValue( - $event->getCommand() - ), - ], + $process = $this->processFactory->createFromShellCommandLine( + $this->parameterBag->resolveValue( + $event->getCommand() + ), timeout: 1800 ); diff --git a/packages/cron-job/Tests/CronJobProcessorTest.php b/packages/cron-job/Tests/CronJobProcessorTest.php index e8285747d..817e5d2fe 100644 --- a/packages/cron-job/Tests/CronJobProcessorTest.php +++ b/packages/cron-job/Tests/CronJobProcessorTest.php @@ -136,9 +136,9 @@ public function testProcess( $this->processFactory ->expects(static::once()) - ->method('create') + ->method('createFromShellCommandLine') ->with( - [$expectedProcessCommand], + $expectedProcessCommand, null, null, null, @@ -217,9 +217,9 @@ public function testProcessWithError(): void $this->processFactory ->expects(static::once()) - ->method('create') + ->method('createFromShellCommandLine') ->with( - ['echo 12345 > /var/cache/crontab.out'], + 'echo 12345 > /var/cache/crontab.out', null, null, null, @@ -254,7 +254,7 @@ public function testProcessWithCancelledExecution(): void $this->processFactory ->expects(static::never()) - ->method('create'); + ->method('createFromShellCommandLine'); $this->cronJobProcessor->process($execution); } diff --git a/packages/process/ProcessFactory.php b/packages/process/ProcessFactory.php index cbd29c654..6614285c5 100644 --- a/packages/process/ProcessFactory.php +++ b/packages/process/ProcessFactory.php @@ -24,4 +24,14 @@ public function create(array $command, ?string $cwd = null, ?array $env = null, { return new Process(...\func_get_args()); } + + public function createFromShellCommandLine( + string $command, + ?string $cwd = null, + ?array $env = null, + $input = null, + ?float $timeout = 60 + ): Process { + return Process::fromShellCommandline(...\func_get_args()); + } } diff --git a/packages/process/Tests/ProcessFactoryTest.php b/packages/process/Tests/ProcessFactoryTest.php index 947cbeddc..d591ab5dc 100644 --- a/packages/process/Tests/ProcessFactoryTest.php +++ b/packages/process/Tests/ProcessFactoryTest.php @@ -4,9 +4,14 @@ use Draw\Component\Process\ProcessFactory; use Draw\Contracts\Process\ProcessFactoryInterface; +use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; use Symfony\Component\Process\Process; +#[ + CoversClass(ProcessFactory::class), + CoversClass(ProcessFactoryInterface::class), +] class ProcessFactoryTest extends TestCase { private ProcessFactory $service; @@ -91,4 +96,34 @@ public function testCreateWithArguments(): void $process->getTimeout() ); } + + public function testCreateFromShellCommandLineDefault(): void + { + $process = $this->service->createFromShellCommandLine('ls -lah | grep test'); + + static::assertInstanceOf(Process::class, $process); + static::assertSame('ls -lah | grep test', $process->getCommandLine()); + static::assertSame(getcwd(), $process->getWorkingDirectory()); + static::assertEmpty($process->getEnv()); + static::assertNull($process->getInput()); + static::assertSame(60.0, $process->getTimeout()); + } + + public function testCreateFromShellCommandLineWithArguments(): void + { + $process = $this->service->createFromShellCommandLine( + 'ls -lah | grep test', + $workingDirectory = __DIR__, + $env = ['key' => 'value'], + $input = 'input', + $timeout = 5.0 + ); + + static::assertInstanceOf(Process::class, $process); + static::assertSame('ls -lah | grep test', $process->getCommandLine()); + static::assertSame($workingDirectory, $process->getWorkingDirectory()); + static::assertSame($env, $process->getEnv()); + static::assertSame($input, $process->getInput()); + static::assertSame($timeout, $process->getTimeout()); + } }