Skip to content

Commit

Permalink
Job & Runner: explicit env variables specification and their clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
milo committed May 19, 2016
1 parent 1e2656b commit a1e696b
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/Runner/CliTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public function run()
}

$runner = $this->createRunner();
$runner->setEnvironmentVariable(Environment::RUNNER, 1);
$runner->setEnvironmentVariable(Environment::COLORS, (int) Environment::$useColors);

if ($this->options['-o'] !== NULL) {
ob_clean();
Expand Down
17 changes: 13 additions & 4 deletions src/Runner/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace Tester\Runner;

use Tester\Environment;
use Tester\Helpers;


Expand Down Expand Up @@ -36,6 +35,9 @@ class Job
/** @var string[] test arguments */
private $args;

/** @var string[] environment variables for test */
private $envVars;

/** @var string test output */
private $output;

Expand Down Expand Up @@ -65,11 +67,12 @@ class Job
* @param string test file name
* @return void
*/
public function __construct($testFile, PhpInterpreter $interpreter, array $args = NULL)
public function __construct($testFile, PhpInterpreter $interpreter, array $args = NULL, array $envVars = NULL)
{
$this->file = (string) $testFile;
$this->interpreter = $interpreter;
$this->args = (array) $args;
$this->envVars = (array) $envVars;
}


Expand All @@ -80,8 +83,10 @@ public function __construct($testFile, PhpInterpreter $interpreter, array $args
*/
public function run($flags = NULL)
{
putenv(Environment::RUNNER . '=1');
putenv(Environment::COLORS . '=' . (int) Environment::$useColors);
foreach ($this->envVars as $name => $value) {
putenv("$name=$value");
}

$this->proc = proc_open(
$this->interpreter->getCommandLine()
. ' -d register_argc_argv=on ' . Helpers::escapeArg($this->file) . ' ' . implode(' ', $this->args),
Expand All @@ -96,6 +101,10 @@ public function run($flags = NULL)
['bypass_shell' => TRUE]
);

foreach (array_keys($this->envVars) as $name) {
putenv($name);
}

list($stdin, $this->stdout, $stderr) = $pipes;
fclose($stdin);
if ($flags & self::RUN_COLLECT_ERRORS) {
Expand Down
23 changes: 23 additions & 0 deletions src/Runner/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class Runner
/** @var PhpInterpreter */
private $interpreter;

/** @var array */
private $envVars = [];

/** @var Job[] */
private $jobs;

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


/**
* @param string
* @param string
* @return void
*/
public function setEnvironmentVariable($name, $value)
{
$this->envVars[$name] = $value;
}


/**
* @return array
*/
public function getEnvironmentVariables()
{
return $this->envVars;
}


/**
* Runs all tests.
* @return bool
Expand Down
2 changes: 1 addition & 1 deletion src/Runner/TestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function initiate($file)
}

foreach ($jobsArgs as $args) {
$this->runner->addJob(new Job($file, $php, $args));
$this->runner->addJob(new Job($file, $php, $args, $this->runner->getEnvironmentVariables()));
}
}

Expand Down
35 changes: 35 additions & 0 deletions tests/Runner/Runner.misc.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Tester\Assert;
use Tester\Runner\Runner;

require __DIR__ . '/../bootstrap.php';
require __DIR__ . '/../../src/Runner/OutputHandler.php';
require __DIR__ . '/../../src/Runner/TestHandler.php';
require __DIR__ . '/../../src/Runner/Runner.php';


class Logger implements Tester\Runner\OutputHandler
{
public $results = [];

function result($testName, $result, $message)
{
$this->results[basename($testName)] = $result;
}

function begin() {}
function end() {}
}

Assert::false(getenv('TesterEnvVar'));

$runner = new Tester\Runner\Runner(createInterpreter());
$runner->paths[] = __DIR__ . '/misc/*.phptx';
$runner->outputHandlers[] = $logger = new Logger;
$runner->setEnvironmentVariable('TesterEnvVar', 'Is here!');
$runner->run();

Assert::false(getenv('TesterEnvVar'));

Assert::same(Runner::PASSED, $logger->results['env-vars.phptx']);
7 changes: 7 additions & 0 deletions tests/Runner/misc/env-vars.phptx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

use Tester\Assert;

require __DIR__ . '/../../bootstrap.php';

Assert::same('Is here!', getenv('TesterEnvVar'));
8 changes: 4 additions & 4 deletions tests/RunnerOutput/JUnitPrinter.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Tester\Assert;
use Tester\Environment;
use Tester\Runner\Runner;
use Tester\Runner\Output\JUnitPrinter;

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


Environment::$useColors = FALSE;
$runner = new Tester\Runner\Runner(createInterpreter());
$printer = new JUnitPrinter($runner);
$runner = new Runner(createInterpreter());
$runner->setEnvironmentVariable(Environment::COLORS, 0);
$runner->outputHandlers[] = new JUnitPrinter($runner);
$runner->paths[] = __DIR__ . '/cases/*.phptx';
$runner->outputHandlers[] = $printer;
ob_start();
$runner->run();
$output = ob_get_clean();
Expand Down

0 comments on commit a1e696b

Please sign in to comment.