Skip to content

Commit ee61e4f

Browse files
committed
Job & Runner: explicit env variables specification and their clean up
1 parent 1e2656b commit ee61e4f

File tree

7 files changed

+85
-8
lines changed

7 files changed

+85
-8
lines changed

src/Runner/CliTester.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public function run()
5959
}
6060

6161
$runner = $this->createRunner();
62+
$runner->setEnvironmentVariable(Environment::RUNNER, 1);
63+
$runner->setEnvironmentVariable(Environment::COLORS, (int) Environment::$useColors);
6264

6365
if ($this->options['-o'] !== NULL) {
6466
ob_clean();

src/Runner/Job.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ class Job
3636
/** @var string[] test arguments */
3737
private $args;
3838

39+
/** @var string[] environment variables for test */
40+
private $envVars;
41+
3942
/** @var string test output */
4043
private $output;
4144

@@ -65,11 +68,12 @@ class Job
6568
* @param string test file name
6669
* @return void
6770
*/
68-
public function __construct($testFile, PhpInterpreter $interpreter, array $args = NULL)
71+
public function __construct($testFile, PhpInterpreter $interpreter, array $args = NULL, array $envVars = NULL)
6972
{
7073
$this->file = (string) $testFile;
7174
$this->interpreter = $interpreter;
7275
$this->args = (array) $args;
76+
$this->envVars = (array) $envVars;
7377
}
7478

7579

@@ -80,8 +84,10 @@ public function __construct($testFile, PhpInterpreter $interpreter, array $args
8084
*/
8185
public function run($flags = NULL)
8286
{
83-
putenv(Environment::RUNNER . '=1');
84-
putenv(Environment::COLORS . '=' . (int) Environment::$useColors);
87+
foreach ($this->envVars as $name => $value) {
88+
putenv("$name=$value");
89+
}
90+
8591
$this->proc = proc_open(
8692
$this->interpreter->getCommandLine()
8793
. ' -d register_argc_argv=on ' . Helpers::escapeArg($this->file) . ' ' . implode(' ', $this->args),
@@ -96,6 +102,10 @@ public function run($flags = NULL)
96102
['bypass_shell' => TRUE]
97103
);
98104

105+
foreach (array_keys($this->envVars) as $name) {
106+
putenv("$name");
107+
}
108+
99109
list($stdin, $this->stdout, $stderr) = $pipes;
100110
fclose($stdin);
101111
if ($flags & self::RUN_COLLECT_ERRORS) {

src/Runner/Runner.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ class Runner
4040
/** @var PhpInterpreter */
4141
private $interpreter;
4242

43+
/** @var array */
44+
private $envVars = [];
45+
4346
/** @var Job[] */
4447
private $jobs;
4548

@@ -60,6 +63,26 @@ public function __construct(PhpInterpreter $interpreter)
6063
}
6164

6265

66+
/**
67+
* @param string
68+
* @param string
69+
* @return void
70+
*/
71+
public function setEnvironmentVariable($name, $value)
72+
{
73+
$this->envVars[$name] = $value;
74+
}
75+
76+
77+
/**
78+
* @return array
79+
*/
80+
public function getEnvironmentVariables()
81+
{
82+
return $this->envVars;
83+
}
84+
85+
6386
/**
6487
* Runs all tests.
6588
* @return bool

src/Runner/TestHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function initiate($file)
6161
}
6262

6363
foreach ($jobsArgs as $args) {
64-
$this->runner->addJob(new Job($file, $php, $args));
64+
$this->runner->addJob(new Job($file, $php, $args, $this->runner->getEnvironmentVariables()));
6565
}
6666
}
6767

tests/Runner/Runner.misc.phpt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
use Tester\Assert;
4+
use Tester\Runner\Runner;
5+
6+
require __DIR__ . '/../bootstrap.php';
7+
require __DIR__ . '/../../src/Runner/OutputHandler.php';
8+
require __DIR__ . '/../../src/Runner/TestHandler.php';
9+
require __DIR__ . '/../../src/Runner/Runner.php';
10+
11+
12+
class Logger implements Tester\Runner\OutputHandler
13+
{
14+
public $results = [];
15+
16+
function result($testName, $result, $message)
17+
{
18+
$this->results[basename($testName)] = $result;
19+
}
20+
21+
function begin() {}
22+
function end() {}
23+
}
24+
25+
Assert::false(getenv('TesterEnvVar'));
26+
27+
$runner = new Tester\Runner\Runner(createInterpreter());
28+
$runner->paths[] = __DIR__ . '/misc/*.phptx';
29+
$runner->outputHandlers[] = $logger = new Logger;
30+
$runner->setEnvironmentVariable('TesterEnvVar', 'Is here!');
31+
$runner->run();
32+
33+
Assert::false(getenv('TesterEnvVar'));
34+
35+
Assert::same(Runner::PASSED, $logger->results['env-vars.phptx']);

tests/Runner/misc/env-vars.phptx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
use Tester\Assert;
4+
5+
require __DIR__ . '/../../bootstrap.php';
6+
7+
Assert::same('Is here!', getenv('TesterEnvVar'));

tests/RunnerOutput/JUnitPrinter.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use Tester\Assert;
44
use Tester\Environment;
5+
use Tester\Runner\Runner;
56
use Tester\Runner\Output\JUnitPrinter;
67

78
require __DIR__ . '/../bootstrap.php';
@@ -11,11 +12,10 @@ require __DIR__ . '/../../src/Runner/OutputHandler.php';
1112
require __DIR__ . '/../../src/Runner/Output/JUnitPrinter.php';
1213

1314

14-
Environment::$useColors = FALSE;
15-
$runner = new Tester\Runner\Runner(createInterpreter());
16-
$printer = new JUnitPrinter($runner);
15+
$runner = new Runner(createInterpreter());
16+
$runner->setEnvironmentVariable(Environment::COLORS, 0);
17+
$runner->outputHandlers[] = new JUnitPrinter($runner);
1718
$runner->paths[] = __DIR__ . '/cases/*.phptx';
18-
$runner->outputHandlers[] = $printer;
1919
ob_start();
2020
$runner->run();
2121
$output = ob_get_clean();

0 commit comments

Comments
 (0)