Skip to content

Commit

Permalink
Set environment variables on sub-process (#49)
Browse files Browse the repository at this point in the history
* test: fix changed process execution

* test: upgrade phpunit

* test: implement mess detector and code sniffer

* ci: upgrade ci workflows

* ci: use pcntl

* ci: php 8.1 compatibility

* feature: set environment variables
closes #239

* test: assert multiple environment variables can be set
  • Loading branch information
g105b authored May 8, 2024
1 parent 3bbdf3e commit e2964f4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class Process {
/** @var array<string, mixed> */
protected array $status;
protected bool $isBlocking = false;
/** @var array<string, string> */
protected array $env = [];

public function __construct(string...$command) {
$this->command = $command;
Expand All @@ -31,6 +33,10 @@ public function setExecCwd(string $cwd):void {
$this->cwd = $cwd;
}

public function setEnv(string $key, string $value):void {
$this->env[$key] = $value;
}

/**
* Runs the command in a concurrent thread.
* Sets the input, output and errors streams.
Expand All @@ -54,7 +60,7 @@ public function exec():void {
$this->command,
$descriptor,
$this->pipes,
// env_vars: $this->env,
env_vars: $this->env,
);
if(!$this->process) {
throw new CommandNotFoundException($this->command[0]);
Expand Down
20 changes: 19 additions & 1 deletion test/phpunit/ProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,22 @@ public function testExecBlocking() {
$sut->exec();
self::assertFalse($sut->isRunning());
}
}

public function testSetEnv() {
$sut = new Process("printenv");
$sut->setEnv("NAME", "PHPUnit");
$sut->exec();
$output = $sut->getOutput();
self::assertStringContainsString("NAME=PHPUnit\n", $output);
}

public function testSetEnv_multiple() {
$sut = new Process("printenv");
$sut->setEnv("NAME", "PHPUnit");
$sut->setEnv("TEST", "setEnv");
$sut->exec();
$output = $sut->getOutput();
self::assertStringContainsString("TEST=setEnv\n", $output);
self::assertStringContainsString("NAME=PHPUnit\n", $output);
}
}

0 comments on commit e2964f4

Please sign in to comment.