Skip to content

Instantly share code, notes, and snippets.

@sun
Created July 28, 2014 20:52
Show Gist options
  • Save sun/5180d1c982e33cdeec02 to your computer and use it in GitHub Desktop.
Save sun/5180d1c982e33cdeec02 to your computer and use it in GitHub Desktop.
PHPUnit: Use php://fd/3 (custom file descriptor) to transfer child process results — FAILS on Windows
diff --git a/src/Util/PHP.php b/src/Util/PHP.php
index d3d36a0..e173775 100644
--- a/src/Util/PHP.php
+++ b/src/Util/PHP.php
@@ -84,7 +84,7 @@ public function runTestJob($job, PHPUnit_Framework_Test $test, PHPUnit_Framework
$_result = $this->runJob($job);
$this->processChildResult(
- $test, $result, $_result['stdout'], $_result['stderr']
+ $test, $result, $_result['result'], $_result['stdout'], $_result['stderr']
);
}
@@ -119,11 +119,12 @@ protected function settingsToParameters(array $settings)
*
* @param PHPUnit_Framework_Test $test
* @param PHPUnit_Framework_TestResult $result
+ * @param string $raw_result
* @param string $stdout
* @param string $stderr
* @since Method available since Release 3.5.0
*/
- private function processChildResult(PHPUnit_Framework_Test $test, PHPUnit_Framework_TestResult $result, $stdout, $stderr)
+ private function processChildResult(PHPUnit_Framework_Test $test, PHPUnit_Framework_TestResult $result, $raw_result, $stdout, $stderr)
{
$time = 0;
@@ -137,18 +138,14 @@ private function processChildResult(PHPUnit_Framework_Test $test, PHPUnit_Framew
throw new ErrorException($errstr, $errno, $errno, $errfile, $errline);
});
try {
- if (strpos($stdout, "#!/usr/bin/env php\n") === 0) {
- $stdout = substr($stdout, 19);
- }
-
- $childResult = unserialize(str_replace("#!/usr/bin/env php\n", '', $stdout));
+ $childResult = unserialize($raw_result);
restore_error_handler();
} catch (ErrorException $e) {
restore_error_handler();
$childResult = false;
$result->addError(
- $test, new PHPUnit_Framework_Exception(trim($stdout), 0, $e), $time
+ $test, new PHPUnit_Framework_Exception($raw_result, 0, $e), $time
);
}
diff --git a/src/Util/PHP/Template/TestCaseMethod.tpl.dist b/src/Util/PHP/Template/TestCaseMethod.tpl.dist
index b81e9bb..1844fc1 100644
--- a/src/Util/PHP/Template/TestCaseMethod.tpl.dist
+++ b/src/Util/PHP/Template/TestCaseMethod.tpl.dist
@@ -39,14 +39,14 @@ function __phpunit_run_isolated_test()
$test->run($result);
$output = $test->getActualOutput();
- print serialize(
+ file_put_contents('php://fd/3', serialize(
array(
'testResult' => $test->getResult(),
'numAssertions' => $test->getNumAssertions(),
'result' => $result,
'output' => $output
)
- );
+ ));
ob_start();
}
diff --git a/src/Util/PHP/Windows.php b/src/Util/PHP/Windows.php
index d9b6ffc..2baec97 100644
--- a/src/Util/PHP/Windows.php
+++ b/src/Util/PHP/Windows.php
@@ -80,13 +80,20 @@ public function runJob($job, array $settings = array())
'A temporary file could not be created; verify that your TEMP environment variable is writable'
);
}
+ if (false === $result_handle = tmpfile()) {
+ throw new PHPUnit_Framework_Exception(
+ 'A temporary file could not be created; verify that your TEMP environment variable is writable'
+ );
+ }
$process = proc_open(
$runtime->getBinary() . $this->settingsToParameters($settings),
array(
0 => array('pipe', 'r'),
1 => $stdout_handle,
- 2 => array('pipe', 'w')
+ 2 => array('pipe', 'w'),
+ 3 => $result_handle,
+// 3 => array('pipe', 'wb')
),
$pipes
);
@@ -103,15 +110,22 @@ public function runJob($job, array $settings = array())
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[2]);
+// $result = stream_get_contents($pipes[3]);
+// fclose($pipes[3]);
+
proc_close($process);
rewind($stdout_handle);
$stdout = stream_get_contents($stdout_handle);
fclose($stdout_handle);
+ rewind($result_handle);
+ $result = stream_get_contents($result_handle);
+ fclose($result_handle);
+
$this->cleanup();
- return array('stdout' => $stdout, 'stderr' => $stderr);
+ return array('result' => $result, 'stdout' => $stdout, 'stderr' => $stderr);
}
/**
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment