Skip to content

Commit

Permalink
Added new method to ProcessFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
DumitracheAdrian committed Apr 19, 2024
1 parent 0c94b7f commit 66db3b7
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 11 deletions.
8 changes: 8 additions & 0 deletions packages/contracts/Process/ProcessFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
10 changes: 4 additions & 6 deletions packages/cron-job/CronJobProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);

Expand Down
10 changes: 5 additions & 5 deletions packages/cron-job/Tests/CronJobProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ public function testProcess(

$this->processFactory
->expects(static::once())
->method('create')
->method('createFromShellCommandLine')
->with(
[$expectedProcessCommand],
$expectedProcessCommand,
null,
null,
null,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -254,7 +254,7 @@ public function testProcessWithCancelledExecution(): void

$this->processFactory
->expects(static::never())
->method('create');
->method('createFromShellCommandLine');

$this->cronJobProcessor->process($execution);
}
Expand Down
10 changes: 10 additions & 0 deletions packages/process/ProcessFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
35 changes: 35 additions & 0 deletions packages/process/Tests/ProcessFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}

0 comments on commit 66db3b7

Please sign in to comment.