Permalink
Browse files

Refactor.

  • Loading branch information...
1 parent 353fe69 commit 3d3f631823e8e48ba8028bdf0703500db1703462 @sebastianbergmann committed Feb 20, 2011
Showing with 86 additions and 77 deletions.
  1. +2 −1 PHPUnit/Framework/TestCase.php
  2. +60 −7 PHPUnit/Util/PHP.php
  3. +4 −33 PHPUnit/Util/PHP/Default.php
  4. +20 −36 PHPUnit/Util/PHP/Windows.php
@@ -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);
}
View
@@ -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.
@@ -128,6 +128,19 @@ public static function getPhpBinary()
}
/**
+ * @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.
*
* @param string $job
@@ -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;
@@ -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);
}
}
@@ -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.