Skip to content

Commit

Permalink
Refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Feb 20, 2011
1 parent 353fe69 commit 3d3f631
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 77 deletions.
3 changes: 2 additions & 1 deletion PHPUnit/Framework/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,8 @@ public function run(PHPUnit_Framework_TestResult $result = NULL)

$this->prepareTemplate($template);

PHPUnit_Util_PHP::runJob($template->render(), $this, $result);
$php = PHPUnit_Util_PHP::factory();
$php->runJob($template->render(), $this, $result);
} else {
$result->run($this);
}
Expand Down
67 changes: 60 additions & 7 deletions PHPUnit/Util/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
* @link http://www.phpunit.de/
* @since Class available since Release 3.4.0
*/
class PHPUnit_Util_PHP
abstract class PHPUnit_Util_PHP
{
/**
* Path to the PHP interpreter that is to be used.
Expand Down Expand Up @@ -127,6 +127,19 @@ public static function getPhpBinary()
return self::$phpBinary;
}

/**
* @return PHPUnit_Util_PHP
* @since Method available since Release 3.5.12
*/
public static function factory()
{
if (DIRECTORY_SEPARATOR == '\\') {
return new PHPUnit_Util_PHP_Windows;
}

return new PHPUnit_Util_PHP_Default;
}

/**
* Runs a single job (PHP code) using a separate PHP process.
*
Expand All @@ -136,25 +149,65 @@ public static function getPhpBinary()
* @return array|null
* @throws PHPUnit_Framework_Exception
*/
public static function runJob($job, PHPUnit_Framework_Test $test = NULL, PHPUnit_Framework_TestResult $result = NULL)
public function runJob($job, PHPUnit_Framework_Test $test = NULL, PHPUnit_Framework_TestResult $result = NULL)
{
if (DIRECTORY_SEPARATOR == '\\') {
return PHPUnit_Util_PHP_Windows::runJob($job, $test, $result);
$process = proc_open(
self::getPhpBinary(), self::$descriptorSpec, $pipes
);

if (!is_resource($process)) {
throw new PHPUnit_Framework_Exception(
'Unable to create process for process isolation.'
);
}

if ($result !== NULL) {
$result->startTest($test);
}

$this->process($pipes[0], $job);
fclose($pipes[0]);

$stdout = stream_get_contents($pipes[1]);
fclose($pipes[1]);

$stderr = stream_get_contents($pipes[2]);
fclose($pipes[2]);

proc_close($process);
$this->cleanup();

if ($result !== NULL) {
$this->processChildResult($test, $result, $stdout, $stderr);
} else {
return array('stdout' => $stdout, 'stderr' => $stderr);
}
}

return PHPUnit_Util_PHP_Default::runJob($job, $test, $result);
/**
* @param resource $pipe
* @param string $job
* @since Method available since Release 3.5.12
*/
abstract protected function process($pipe, $job);

/**
* @since Method available since Release 3.5.12
*/
protected function cleanup()
{
}

/**
* Runs a single job (PHP code) using a separate PHP process.
* Processes the TestResult object from an isolated process.
*
* @param PHPUnit_Framework_TestCase $test
* @param PHPUnit_Framework_TestResult $result
* @param string $stdout
* @param string $stderr
* @since Method available since Release 3.5.0
*/
protected static function processChildResult(PHPUnit_Framework_Test $test, PHPUnit_Framework_TestResult $result, $stdout, $stderr)
protected function processChildResult(PHPUnit_Framework_Test $test, PHPUnit_Framework_TestResult $result, $stdout, $stderr)
{
if (!empty($stderr)) {
$time = 0;
Expand Down
37 changes: 4 additions & 33 deletions PHPUnit/Util/PHP/Default.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,40 +58,11 @@
class PHPUnit_Util_PHP_Default extends PHPUnit_Util_PHP
{
/**
* Runs a single job (PHP code) using a separate PHP process.
*
* @param string $job
* @param PHPUnit_Framework_TestCase $test
* @param PHPUnit_Framework_TestResult $result
* @return array|null
* @param resource $pipe
* @since Method available since Release 3.5.12
*/
public static function runJob($job, PHPUnit_Framework_Test $test = NULL, PHPUnit_Framework_TestResult $result = NULL)
protected function process($pipe, $job)
{
$process = proc_open(
self::getPhpBinary(), self::$descriptorSpec, $pipes
);

if (is_resource($process)) {
if ($result !== NULL) {
$result->startTest($test);
}

fwrite($pipes[0], $job);
fclose($pipes[0]);

$stdout = stream_get_contents($pipes[1]);
fclose($pipes[1]);

$stderr = stream_get_contents($pipes[2]);
fclose($pipes[2]);

proc_close($process);

if ($result !== NULL) {
self::processChildResult($test, $result, $stdout, $stderr);
} else {
return array('stdout' => $stdout, 'stderr' => $stderr);
}
}
fwrite($pipe, $job);
}
}
56 changes: 20 additions & 36 deletions PHPUnit/Util/PHP/Windows.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,50 +58,34 @@
class PHPUnit_Util_PHP_Windows extends PHPUnit_Util_PHP
{
/**
* Runs a single job (PHP code) using a separate PHP process.
*
* @param string $job
* @param PHPUnit_Framework_TestCase $test
* @param PHPUnit_Framework_TestResult $result
* @return array|null
* @throws PHPUnit_Framework_Exception
* @var string
*/
public static function runJob($job, PHPUnit_Framework_Test $test = NULL, PHPUnit_Framework_TestResult $result = NULL)
protected $tempFile;

/**
* @param resource $pipe
* @since Method available since Release 3.5.12
*/
protected function process($pipe, $job)
{
if(!($file = tempnam(sys_get_temp_dir(), 'PHPUnit')) || file_put_contents($file, $job) === FALSE) {
if (!($this->tempFile = tempnam(sys_get_temp_dir(), 'PHPUnit')) ||
file_put_contents($this->tempFile, $job) === FALSE) {
throw new PHPUnit_Framework_Exception(
'Unable to write temporary files for process isolation.'
);
}

$process = proc_open(
self::getPhpBinary(), self::$descriptorSpec, $pipes
fwrite(
$pipe,
"<?php require_once '" . addcslashes($this->tempFile, "'") . "'; ?>"
);
}

if (is_resource($process)) {
if ($result !== NULL) {
$result->startTest($test);
}

fwrite($pipes[0], "<?php require_once '" . addcslashes($file, "'") . "'; ?>");
fclose($pipes[0]);

$stdout = stream_get_contents($pipes[1]);
fclose($pipes[1]);

$stderr = stream_get_contents($pipes[2]);
fclose($pipes[2]);

proc_close($process);
unlink($file);

if ($result !== NULL) {
self::processChildResult($test, $result, $stdout, $stderr);
} else {
return array('stdout' => $stdout, 'stderr' => $stderr);
}
} else {
unlink($file);
}
/**
* @since Method available since Release 3.5.12
*/
protected function cleanup()
{
unlink($this->tempFile);
}
}

0 comments on commit 3d3f631

Please sign in to comment.