This commit is contained in:
2020-10-06 14:27:47 +07:00
commit 586be80cf6
16613 changed files with 3274099 additions and 0 deletions

View File

@@ -0,0 +1,421 @@
<?php
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Util\PHP;
use __PHP_Incomplete_Class;
use ErrorException;
use PHPUnit\Framework\Exception;
use PHPUnit\Framework\SyntheticError;
use PHPUnit\Framework\Test;
use PHPUnit\Framework\TestFailure;
use PHPUnit\Framework\TestResult;
use PHPUnit\Util\InvalidArgumentHelper;
use SebastianBergmann\Environment\Runtime;
/**
* Utility methods for PHP sub-processes.
*/
abstract class AbstractPhpProcess
{
/**
* @var Runtime
*/
protected $runtime;
/**
* @var bool
*/
protected $stderrRedirection = false;
/**
* @var string
*/
protected $stdin = '';
/**
* @var string
*/
protected $args = '';
/**
* @var array<string, string>
*/
protected $env = [];
/**
* @var int
*/
protected $timeout = 0;
/**
* Creates internal Runtime instance.
*/
public function __construct()
{
$this->runtime = new Runtime();
}
/**
* Defines if should use STDERR redirection or not.
*
* Then $stderrRedirection is TRUE, STDERR is redirected to STDOUT.
*
* @throws Exception
*
* @param bool $stderrRedirection
*/
public function setUseStderrRedirection($stderrRedirection)
{
if (!\is_bool($stderrRedirection)) {
throw InvalidArgumentHelper::factory(1, 'boolean');
}
$this->stderrRedirection = $stderrRedirection;
}
/**
* Returns TRUE if uses STDERR redirection or FALSE if not.
*
* @return bool
*/
public function useStderrRedirection()
{
return $this->stderrRedirection;
}
/**
* Sets the input string to be sent via STDIN
*
* @param string $stdin
*/
public function setStdin($stdin)
{
$this->stdin = (string) $stdin;
}
/**
* Returns the input string to be sent via STDIN
*
* @return string
*/
public function getStdin()
{
return $this->stdin;
}
/**
* Sets the string of arguments to pass to the php job
*
* @param string $args
*/
public function setArgs($args)
{
$this->args = (string) $args;
}
/**
* Returns the string of arguments to pass to the php job
*
* @retrun string
*/
public function getArgs()
{
return $this->args;
}
/**
* Sets the array of environment variables to start the child process with
*
* @param array<string, string> $env
*/
public function setEnv(array $env)
{
$this->env = $env;
}
/**
* Returns the array of environment variables to start the child process with
*
* @return array<string, string>
*/
public function getEnv()
{
return $this->env;
}
/**
* Sets the amount of seconds to wait before timing out
*
* @param int $timeout
*/
public function setTimeout($timeout)
{
$this->timeout = (int) $timeout;
}
/**
* Returns the amount of seconds to wait before timing out
*
* @return int
*/
public function getTimeout()
{
return $this->timeout;
}
/**
* @return AbstractPhpProcess
*/
public static function factory()
{
if (DIRECTORY_SEPARATOR == '\\') {
return new WindowsPhpProcess;
}
return new DefaultPhpProcess;
}
/**
* Runs a single test in a separate PHP process.
*
* @param string $job
* @param Test $test
* @param TestResult $result
*
* @throws Exception
*/
public function runTestJob($job, Test $test, TestResult $result)
{
$result->startTest($test);
$_result = $this->runJob($job);
$this->processChildResult(
$test,
$result,
$_result['stdout'],
$_result['stderr']
);
}
/**
* Returns the command based into the configurations.
*
* @param array $settings
* @param string|null $file
*
* @return string
*/
public function getCommand(array $settings, $file = null)
{
$command = $this->runtime->getBinary();
$command .= $this->settingsToParameters($settings);
if ('phpdbg' === PHP_SAPI) {
$command .= ' -qrr ';
if ($file) {
$command .= '-e ' . \escapeshellarg($file);
} else {
$command .= \escapeshellarg(__DIR__ . '/eval-stdin.php');
}
} elseif ($file) {
$command .= ' -f ' . \escapeshellarg($file);
}
if ($this->args) {
$command .= ' -- ' . $this->args;
}
if (true === $this->stderrRedirection) {
$command .= ' 2>&1';
}
return $command;
}
/**
* Runs a single job (PHP code) using a separate PHP process.
*
* @param string $job
* @param array $settings
*
* @return array
*
* @throws Exception
*/
abstract public function runJob($job, array $settings = []);
/**
* @param array $settings
*
* @return string
*/
protected function settingsToParameters(array $settings)
{
$buffer = '';
foreach ($settings as $setting) {
$buffer .= ' -d ' . \escapeshellarg($setting);
}
return $buffer;
}
/**
* Processes the TestResult object from an isolated process.
*
* @param Test $test
* @param TestResult $result
* @param string $stdout
* @param string $stderr
*/
private function processChildResult(Test $test, TestResult $result, $stdout, $stderr)
{
$time = 0;
if (!empty($stderr)) {
$result->addError(
$test,
new Exception(\trim($stderr)),
$time
);
} else {
\set_error_handler(function ($errno, $errstr, $errfile, $errline) {
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));
\restore_error_handler();
} catch (ErrorException $e) {
\restore_error_handler();
$childResult = false;
$result->addError(
$test,
new Exception(\trim($stdout), 0, $e),
$time
);
}
if ($childResult !== false) {
if (!empty($childResult['output'])) {
$output = $childResult['output'];
}
$test->setResult($childResult['testResult']);
$test->addToAssertionCount($childResult['numAssertions']);
/** @var TestResult $childResult */
$childResult = $childResult['result'];
if ($result->getCollectCodeCoverageInformation()) {
$result->getCodeCoverage()->merge(
$childResult->getCodeCoverage()
);
}
$time = $childResult->time();
$notImplemented = $childResult->notImplemented();
$risky = $childResult->risky();
$skipped = $childResult->skipped();
$errors = $childResult->errors();
$warnings = $childResult->warnings();
$failures = $childResult->failures();
if (!empty($notImplemented)) {
$result->addError(
$test,
$this->getException($notImplemented[0]),
$time
);
} elseif (!empty($risky)) {
$result->addError(
$test,
$this->getException($risky[0]),
$time
);
} elseif (!empty($skipped)) {
$result->addError(
$test,
$this->getException($skipped[0]),
$time
);
} elseif (!empty($errors)) {
$result->addError(
$test,
$this->getException($errors[0]),
$time
);
} elseif (!empty($warnings)) {
$result->addWarning(
$test,
$this->getException($warnings[0]),
$time
);
} elseif (!empty($failures)) {
$result->addFailure(
$test,
$this->getException($failures[0]),
$time
);
}
}
}
$result->endTest($test, $time);
if (!empty($output)) {
print $output;
}
}
/**
* Gets the thrown exception from a PHPUnit\Framework\TestFailure.
*
* @param TestFailure $error
*
* @return Exception
*
* @see https://github.com/sebastianbergmann/phpunit/issues/74
*/
private function getException(TestFailure $error)
{
$exception = $error->thrownException();
if ($exception instanceof __PHP_Incomplete_Class) {
$exceptionArray = [];
foreach ((array) $exception as $key => $value) {
$key = \substr($key, \strrpos($key, "\0") + 1);
$exceptionArray[$key] = $value;
}
$exception = new SyntheticError(
\sprintf(
'%s: %s',
$exceptionArray['_PHP_Incomplete_Class_Name'],
$exceptionArray['message']
),
$exceptionArray['code'],
$exceptionArray['file'],
$exceptionArray['line'],
$exceptionArray['trace']
);
}
return $exception;
}
}

View File

@@ -0,0 +1,232 @@
<?php
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Util\PHP;
use PHPUnit\Framework\Exception;
/**
* Default utility for PHP sub-processes.
*/
class DefaultPhpProcess extends AbstractPhpProcess
{
/**
* @var string
*/
protected $tempFile;
/**
* @var bool
*/
protected $useTempFile = false;
/**
* Runs a single job (PHP code) using a separate PHP process.
*
* @param string $job
* @param array $settings
*
* @return array<string, string>
*
* @throws Exception
*/
public function runJob($job, array $settings = [])
{
if ($this->useTempFile || $this->stdin) {
if (!($this->tempFile = \tempnam(\sys_get_temp_dir(), 'PHPUnit')) ||
\file_put_contents($this->tempFile, $job) === false) {
throw new Exception(
'Unable to write temporary file'
);
}
$job = $this->stdin;
}
return $this->runProcess($job, $settings);
}
/**
* Returns an array of file handles to be used in place of pipes
*
* @return array
*/
protected function getHandles()
{
return [];
}
/**
* Handles creating the child process and returning the STDOUT and STDERR
*
* @param string $job
* @param array $settings
*
* @return array<string, string>
*
* @throws Exception
*/
protected function runProcess($job, $settings)
{
$handles = $this->getHandles();
$env = null;
if ($this->env) {
$env = $_SERVER ?? [];
unset($env['argv'], $env['argc']);
$env = \array_merge($env, $this->env);
foreach ($env as $envKey => $envVar) {
if (\is_array($envVar)) {
unset($env[$envKey]);
}
}
}
$pipeSpec = [
0 => $handles[0] ?? ['pipe', 'r'],
1 => $handles[1] ?? ['pipe', 'w'],
2 => $handles[2] ?? ['pipe', 'w'],
];
$process = \proc_open(
$this->getCommand($settings, $this->tempFile),
$pipeSpec,
$pipes,
null,
$env
);
if (!\is_resource($process)) {
throw new Exception(
'Unable to spawn worker process'
);
}
if ($job) {
$this->process($pipes[0], $job);
}
\fclose($pipes[0]);
if ($this->timeout) {
$stderr = $stdout = '';
unset($pipes[0]);
while (true) {
$r = $pipes;
$w = null;
$e = null;
$n = @\stream_select($r, $w, $e, $this->timeout);
if ($n === false) {
break;
} elseif ($n === 0) {
\proc_terminate($process, 9);
throw new Exception(
\sprintf(
'Job execution aborted after %d seconds',
$this->timeout
)
);
} elseif ($n > 0) {
foreach ($r as $pipe) {
$pipeOffset = 0;
foreach ($pipes as $i => $origPipe) {
if ($pipe == $origPipe) {
$pipeOffset = $i;
break;
}
}
if (!$pipeOffset) {
break;
}
$line = \fread($pipe, 8192);
if (\strlen($line) == 0) {
\fclose($pipes[$pipeOffset]);
unset($pipes[$pipeOffset]);
} else {
if ($pipeOffset == 1) {
$stdout .= $line;
} else {
$stderr .= $line;
}
}
}
if (empty($pipes)) {
break;
}
}
}
} else {
if (isset($pipes[1])) {
$stdout = \stream_get_contents($pipes[1]);
\fclose($pipes[1]);
}
if (isset($pipes[2])) {
$stderr = \stream_get_contents($pipes[2]);
\fclose($pipes[2]);
}
}
if (isset($handles[1])) {
\rewind($handles[1]);
$stdout = \stream_get_contents($handles[1]);
\fclose($handles[1]);
}
if (isset($handles[2])) {
\rewind($handles[2]);
$stderr = \stream_get_contents($handles[2]);
\fclose($handles[2]);
}
\proc_close($process);
$this->cleanup();
return ['stdout' => $stdout, 'stderr' => $stderr];
}
/**
* @param resource $pipe
* @param string $job
*
* @throws Exception
*/
protected function process($pipe, $job)
{
\fwrite($pipe, $job);
}
protected function cleanup()
{
if ($this->tempFile) {
\unlink($this->tempFile);
}
}
}

View File

@@ -0,0 +1,46 @@
<?php
use SebastianBergmann\CodeCoverage\CodeCoverage;
$composerAutoload = {composerAutoload};
$phar = {phar};
$autoPrependFile = {autoPrependFile};
ob_start();
$GLOBALS['__PHPUNIT_ISOLATION_BLACKLIST'][] = '{job}';
if ($composerAutoload) {
require_once $composerAutoload;
define('PHPUNIT_COMPOSER_INSTALL', $composerAutoload);
} else if ($phar) {
require $phar;
}
{globals}
$coverage = null;
if (isset($GLOBALS['__PHPUNIT_BOOTSTRAP'])) {
require_once $GLOBALS['__PHPUNIT_BOOTSTRAP'];
}
if (class_exists('SebastianBergmann\CodeCoverage\CodeCoverage')) {
$coverage = new CodeCoverage(null);
$coverage->start(__FILE__);
}
register_shutdown_function(function() use ($coverage, $autoPrependFile) {
$output = null;
if ($coverage) {
$output = $coverage->stop();
}
file_put_contents('{coverageFile}', serialize($output));
});
ob_end_clean();
if ($autoPrependFile) {
require $autoPrependFile;
$includes = get_included_files();
$GLOBALS['__PHPUNIT_ISOLATION_BLACKLIST'][] = array_pop($includes);
unset($includes);
}

View File

@@ -0,0 +1,107 @@
<?php
use SebastianBergmann\CodeCoverage\CodeCoverage;
if (!defined('STDOUT')) {
// php://stdout does not obey output buffering. Any output would break
// unserialization of child process results in the parent process.
define('STDOUT', fopen('php://temp', 'w+b'));
define('STDERR', fopen('php://stderr', 'wb'));
}
{iniSettings}
ini_set('display_errors', 'stderr');
set_include_path('{include_path}');
$composerAutoload = {composerAutoload};
$phar = {phar};
ob_start();
if ($composerAutoload) {
require_once $composerAutoload;
define('PHPUNIT_COMPOSER_INSTALL', $composerAutoload);
} else if ($phar) {
require $phar;
}
function __phpunit_run_isolated_test()
{
if (!class_exists('{className}')) {
require_once '{filename}';
}
$result = new PHPUnit\Framework\TestResult;
if ({collectCodeCoverageInformation}) {
$result->setCodeCoverage(
new CodeCoverage(
null,
unserialize('{codeCoverageFilter}')
)
);
}
$result->beStrictAboutTestsThatDoNotTestAnything({isStrictAboutTestsThatDoNotTestAnything});
$result->beStrictAboutOutputDuringTests({isStrictAboutOutputDuringTests});
$result->enforceTimeLimit({enforcesTimeLimit});
$result->beStrictAboutTodoAnnotatedTests({isStrictAboutTodoAnnotatedTests});
$result->beStrictAboutResourceUsageDuringSmallTests({isStrictAboutResourceUsageDuringSmallTests});
$test = new {className}('{name}', unserialize('{data}'), '{dataName}');
$test->setDependencyInput(unserialize('{dependencyInput}'));
$test->setInIsolation(TRUE);
ob_end_clean();
$test->run($result);
$output = '';
if (!$test->hasExpectationOnOutput()) {
$output = $test->getActualOutput();
}
@rewind(STDOUT); /* @ as not every STDOUT target stream is rewindable */
if ($stdout = stream_get_contents(STDOUT)) {
$output = $stdout . $output;
$streamMetaData = stream_get_meta_data(STDOUT);
if (!empty($streamMetaData['stream_type']) && 'STDIO' === $streamMetaData['stream_type']) {
@ftruncate(STDOUT, 0);
@rewind(STDOUT);
}
}
print serialize(
array(
'testResult' => $test->getResult(),
'numAssertions' => $test->getNumAssertions(),
'result' => $result,
'output' => $output
)
);
}
$configurationFilePath = '{configurationFilePath}';
if ('' !== $configurationFilePath) {
$configuration = PHPUnit\Util\Configuration::getInstance($configurationFilePath);
$configuration->handlePHPConfiguration();
unset($configuration);
}
function __phpunit_error_handler($errno, $errstr, $errfile, $errline, $errcontext)
{
return true;
}
set_error_handler('__phpunit_error_handler');
{constants}
{included_files}
{globals}
restore_error_handler();
if (isset($GLOBALS['__PHPUNIT_BOOTSTRAP'])) {
require_once $GLOBALS['__PHPUNIT_BOOTSTRAP'];
unset($GLOBALS['__PHPUNIT_BOOTSTRAP']);
}
__phpunit_run_isolated_test();

View File

@@ -0,0 +1,109 @@
<?php
use PHPUnit\Framework\TestCase;
use SebastianBergmann\CodeCoverage\CodeCoverage;
if (!defined('STDOUT')) {
// php://stdout does not obey output buffering. Any output would break
// unserialization of child process results in the parent process.
define('STDOUT', fopen('php://temp', 'w+b'));
define('STDERR', fopen('php://stderr', 'wb'));
}
{iniSettings}
ini_set('display_errors', 'stderr');
set_include_path('{include_path}');
$composerAutoload = {composerAutoload};
$phar = {phar};
ob_start();
if ($composerAutoload) {
require_once $composerAutoload;
define('PHPUNIT_COMPOSER_INSTALL', $composerAutoload);
} else if ($phar) {
require $phar;
}
function __phpunit_run_isolated_test()
{
if (!class_exists('{className}')) {
require_once '{filename}';
}
$result = new PHPUnit\Framework\TestResult;
if ({collectCodeCoverageInformation}) {
$result->setCodeCoverage(
new CodeCoverage(
null,
unserialize('{codeCoverageFilter}')
)
);
}
$result->beStrictAboutTestsThatDoNotTestAnything({isStrictAboutTestsThatDoNotTestAnything});
$result->beStrictAboutOutputDuringTests({isStrictAboutOutputDuringTests});
$result->enforceTimeLimit({enforcesTimeLimit});
$result->beStrictAboutTodoAnnotatedTests({isStrictAboutTodoAnnotatedTests});
$result->beStrictAboutResourceUsageDuringSmallTests({isStrictAboutResourceUsageDuringSmallTests});
/** @var TestCase $test */
$test = new {className}('{methodName}', unserialize('{data}'), '{dataName}');
$test->setDependencyInput(unserialize('{dependencyInput}'));
$test->setInIsolation(TRUE);
ob_end_clean();
$test->run($result);
$output = '';
if (!$test->hasExpectationOnOutput()) {
$output = $test->getActualOutput();
}
@rewind(STDOUT); /* @ as not every STDOUT target stream is rewindable */
if ($stdout = stream_get_contents(STDOUT)) {
$output = $stdout . $output;
$streamMetaData = stream_get_meta_data(STDOUT);
if (!empty($streamMetaData['stream_type']) && 'STDIO' === $streamMetaData['stream_type']) {
@ftruncate(STDOUT, 0);
@rewind(STDOUT);
}
}
print serialize(
array(
'testResult' => $test->getResult(),
'numAssertions' => $test->getNumAssertions(),
'result' => $result,
'output' => $output
)
);
}
$configurationFilePath = '{configurationFilePath}';
if ('' !== $configurationFilePath) {
$configuration = PHPUnit\Util\Configuration::getInstance($configurationFilePath);
$configuration->handlePHPConfiguration();
unset($configuration);
}
function __phpunit_error_handler($errno, $errstr, $errfile, $errline, $errcontext)
{
return true;
}
set_error_handler('__phpunit_error_handler');
{constants}
{included_files}
{globals}
restore_error_handler();
if (isset($GLOBALS['__PHPUNIT_BOOTSTRAP'])) {
require_once $GLOBALS['__PHPUNIT_BOOTSTRAP'];
unset($GLOBALS['__PHPUNIT_BOOTSTRAP']);
}
__phpunit_run_isolated_test();

View File

@@ -0,0 +1,43 @@
<?php
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Util\PHP;
use PHPUnit\Framework\Exception;
/**
* Windows utility for PHP sub-processes.
*
* Reading from STDOUT or STDERR hangs forever on Windows if the output is
* too large.
*
* @see https://bugs.php.net/bug.php?id=51800
*/
class WindowsPhpProcess extends DefaultPhpProcess
{
protected $useTempFile = true;
protected function getHandles()
{
if (false === $stdout_handle = \tmpfile()) {
throw new Exception(
'A temporary file could not be created; verify that your TEMP environment variable is writable'
);
}
return [
1 => $stdout_handle
];
}
public function getCommand(array $settings, $file = null)
{
return '"' . parent::getCommand($settings, $file) . '"';
}
}

View File

@@ -0,0 +1,10 @@
<?php
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
eval('?>' . \file_get_contents('php://stdin'));