From 62e53646ca68e1b37624c8240a21398b48edfdd6 Mon Sep 17 00:00:00 2001 From: Daniel Wehner Date: Fri, 3 Mar 2017 15:20:32 +0000 Subject: [PATCH 1/5] Remove file option and unify the php and abstract process --- src/Util/PHP/AbstractPhpProcess.php | 420 ---------------------------- src/Util/PHP/DefaultPhpProcess.php | 386 ++++++++++++++++++++++++- src/Util/PHP/WindowsPhpProcess.php | 4 +- src/Util/PHP/eval-stdin.php | 10 - 4 files changed, 385 insertions(+), 435 deletions(-) delete mode 100644 src/Util/PHP/AbstractPhpProcess.php delete mode 100644 src/Util/PHP/eval-stdin.php diff --git a/src/Util/PHP/AbstractPhpProcess.php b/src/Util/PHP/AbstractPhpProcess.php deleted file mode 100644 index 913e7a75d67..00000000000 --- a/src/Util/PHP/AbstractPhpProcess.php +++ /dev/null @@ -1,420 +0,0 @@ - - * - * 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\TestResult; -use PHPUnit\Framework\TestFailure; -use PHPUnit\Framework\Test; -use PHPUnit\Framework\SyntheticError; -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 - */ - 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 $env - */ - public function setEnv(array $env) - { - $this->env = $env; - } - - /** - * Returns the array of environment variables to start the child process with - * - * @return array - */ - 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__ . '/PHP/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 ' . $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']); - - $childResult = $childResult['result']; - /* @var $childResult TestResult */ - - 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; - } -} diff --git a/src/Util/PHP/DefaultPhpProcess.php b/src/Util/PHP/DefaultPhpProcess.php index f6485eccbaf..47f8c0f5e86 100644 --- a/src/Util/PHP/DefaultPhpProcess.php +++ b/src/Util/PHP/DefaultPhpProcess.php @@ -10,6 +10,16 @@ namespace PHPUnit\Util\PHP; use PHPUnit\Framework\Exception; +use SebastianBergmann\Environment\Runtime; +use __PHP_Incomplete_Class; +use ErrorException; +use PHPUnit\Framework\Exception; +use PHPUnit\Framework\TestResult; +use PHPUnit\Framework\TestFailure; +use PHPUnit\Framework\Test; +use PHPUnit\Framework\SyntheticError; +use PHPUnit\Util\InvalidArgumentHelper; +use SebastianBergmann\Environment\Runtime; /** * Default utility for PHP sub-processes. @@ -21,10 +31,382 @@ class DefaultPhpProcess extends AbstractPhpProcess */ protected $tempFile; + /** + * @var Runtime + */ + protected $runtime; + /** * @var bool */ - protected $useTempFile = false; + protected $stderrRedirection = false; + + /** + * @var string + */ + protected $stdin = ''; + + /** + * @var string + */ + protected $args = ''; + + /** + * @var array + */ + 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 $env + */ + public function setEnv(array $env) + { + $this->env = $env; + } + + /** + * Returns the array of environment variables to start the child process with + * + * @return array + */ + 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 $file + * + * @return string + */ + public function getCommand(array $settings, $file) + { + $command = $this->runtime->getBinary(); + $command .= $this->settingsToParameters($settings); + + if ('phpdbg' === PHP_SAPI) { + $command .= ' -qrr '; + + $command .= '-e ' . escapeshellarg($file); + } else { + $command .= ' -f ' . escapeshellarg($file); + } + + if ($this->args) { + $command .= ' -- ' . $this->args; + } + + if (true === $this->stderrRedirection) { + $command .= ' 2>&1'; + } + + return $command; + } + + /** + * @param array $settings + * + * @return string + */ + protected function settingsToParameters(array $settings) + { + $buffer = ''; + + foreach ($settings as $setting) { + $buffer .= ' -d ' . $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']); + + $childResult = $childResult['result']; + /* @var $childResult TestResult */ + + 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; + } /** * Runs a single job (PHP code) using a separate PHP process. @@ -38,7 +420,7 @@ class DefaultPhpProcess extends AbstractPhpProcess */ public function runJob($job, array $settings = []) { - if ($this->useTempFile || $this->stdin) { + if ($this->stdin) { if (!($this->tempFile = tempnam(sys_get_temp_dir(), 'PHPUnit')) || file_put_contents($this->tempFile, $job) === false ) { diff --git a/src/Util/PHP/WindowsPhpProcess.php b/src/Util/PHP/WindowsPhpProcess.php index 988dfe769df..71bbe4db331 100644 --- a/src/Util/PHP/WindowsPhpProcess.php +++ b/src/Util/PHP/WindowsPhpProcess.php @@ -21,8 +21,6 @@ */ class WindowsPhpProcess extends DefaultPhpProcess { - protected $useTempFile = true; - protected function getHandles() { if (false === $stdout_handle = tmpfile()) { @@ -36,7 +34,7 @@ protected function getHandles() ]; } - public function getCommand(array $settings, $file = null) + public function getCommand(array $settings, $file) { return '"' . parent::getCommand($settings, $file) . '"'; } diff --git a/src/Util/PHP/eval-stdin.php b/src/Util/PHP/eval-stdin.php deleted file mode 100644 index ccf82714538..00000000000 --- a/src/Util/PHP/eval-stdin.php +++ /dev/null @@ -1,10 +0,0 @@ - - * - * 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')); From 72b8a6ad267d436e76091f99467da05032bc510d Mon Sep 17 00:00:00 2001 From: Daniel Wehner Date: Fri, 3 Mar 2017 15:42:18 +0000 Subject: [PATCH 2/5] Rename the phpunit class to PHP --- src/Framework/TestCase.php | 3 +- src/Runner/PhptTestCase.php | 3 +- .../{PHP/DefaultPhpProcess.php => PHP.php} | 22 +++++----- src/Util/PHP/WindowsPhpProcess.php | 41 ------------------- 4 files changed, 14 insertions(+), 55 deletions(-) rename src/Util/{PHP/DefaultPhpProcess.php => PHP.php} (97%) delete mode 100644 src/Util/PHP/WindowsPhpProcess.php diff --git a/src/Framework/TestCase.php b/src/Framework/TestCase.php index 497f117f870..0e91fad2337 100644 --- a/src/Framework/TestCase.php +++ b/src/Framework/TestCase.php @@ -14,6 +14,7 @@ use PHPUnit\Framework\Constraint\ExceptionCode; use PHPUnit\Framework\Constraint\ExceptionMessage; use PHPUnit\Framework\Constraint\ExceptionMessageRegularExpression; +use PHPUnit\Util\PHP; use PHPUnit_Framework_MockObject_Generator; use PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount; use PHPUnit_Framework_MockObject_Matcher_InvokedAtIndex; @@ -861,7 +862,7 @@ public function run(TestResult $result = null) $this->prepareTemplate($template); - $php = AbstractPhpProcess::factory(); + $php = new PHP(); $php->runTestJob($template->render(), $this, $result); } else { $result->run($this); diff --git a/src/Runner/PhptTestCase.php b/src/Runner/PhptTestCase.php index 7fcb5497c9c..65dc1f16156 100644 --- a/src/Runner/PhptTestCase.php +++ b/src/Runner/PhptTestCase.php @@ -19,6 +19,7 @@ use PHPUnit\Framework\SelfDescribing; use PHPUnit\Util\InvalidArgumentHelper; use PHPUnit\Util\PHP\AbstractPhpProcess; +use PHPUnit\Util\PHP; use Throwable; /** @@ -86,7 +87,7 @@ public function __construct($filename, $phpUtil = null) } $this->filename = $filename; - $this->phpUtil = $phpUtil ?: AbstractPhpProcess::factory(); + $this->phpUtil = $phpUtil ?: new PHP(); } /** diff --git a/src/Util/PHP/DefaultPhpProcess.php b/src/Util/PHP.php similarity index 97% rename from src/Util/PHP/DefaultPhpProcess.php rename to src/Util/PHP.php index 47f8c0f5e86..3bf1c97e9a2 100644 --- a/src/Util/PHP/DefaultPhpProcess.php +++ b/src/Util/PHP.php @@ -7,24 +7,21 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ -namespace PHPUnit\Util\PHP; +namespace PHPUnit\Util; -use PHPUnit\Framework\Exception; -use SebastianBergmann\Environment\Runtime; use __PHP_Incomplete_Class; use ErrorException; +use SebastianBergmann\Environment\Runtime; use PHPUnit\Framework\Exception; use PHPUnit\Framework\TestResult; use PHPUnit\Framework\TestFailure; use PHPUnit\Framework\Test; use PHPUnit\Framework\SyntheticError; -use PHPUnit\Util\InvalidArgumentHelper; -use SebastianBergmann\Environment\Runtime; /** * Default utility for PHP sub-processes. */ -class DefaultPhpProcess extends AbstractPhpProcess +class PHP { /** * @var string @@ -178,15 +175,11 @@ public function getTimeout() } /** - * @return AbstractPhpProcess + * @return \PHPUnit\Util\PHP */ public static function factory() { - if (DIRECTORY_SEPARATOR == '\\') { - return new WindowsPhpProcess; - } - - return new DefaultPhpProcess; + return new static(); } /** @@ -241,6 +234,11 @@ public function getCommand(array $settings, $file) $command .= ' 2>&1'; } + // Special case windows. + if (DIRECTORY_SEPARATOR == '\\') { + $command = '"' . $command . '"'; + } + return $command; } diff --git a/src/Util/PHP/WindowsPhpProcess.php b/src/Util/PHP/WindowsPhpProcess.php deleted file mode 100644 index 71bbe4db331..00000000000 --- a/src/Util/PHP/WindowsPhpProcess.php +++ /dev/null @@ -1,41 +0,0 @@ - - * - * 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 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) - { - return '"' . parent::getCommand($settings, $file) . '"'; - } -} From 524337a6c8fc9748388cdb6d703020b4e7170cea Mon Sep 17 00:00:00 2001 From: Daniel Wehner Date: Fri, 3 Mar 2017 15:43:22 +0000 Subject: [PATCH 3/5] remove the PHP::factory method as its no longer needed --- src/Util/PHP.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/Util/PHP.php b/src/Util/PHP.php index 3bf1c97e9a2..c06e6d2e2bc 100644 --- a/src/Util/PHP.php +++ b/src/Util/PHP.php @@ -174,14 +174,6 @@ public function getTimeout() return $this->timeout; } - /** - * @return \PHPUnit\Util\PHP - */ - public static function factory() - { - return new static(); - } - /** * Runs a single test in a separate PHP process. * From c4a62746a0741fe348845a6a31b5b02ed03896e6 Mon Sep 17 00:00:00 2001 From: Daniel Wehner Date: Fri, 3 Mar 2017 16:49:30 +0000 Subject: [PATCH 4/5] fix a good amount of test failures --- src/Framework/TestCase.php | 1 - src/Runner/PhptTestCase.php | 5 ++--- src/Util/PHP.php | 16 +++++++-------- tests/Runner/PhptTestCaseTest.php | 4 ++-- tests/Util/PHPTest.php | 34 +++++++++++++++---------------- 5 files changed, 29 insertions(+), 31 deletions(-) diff --git a/src/Framework/TestCase.php b/src/Framework/TestCase.php index 0e91fad2337..25235a8a04f 100644 --- a/src/Framework/TestCase.php +++ b/src/Framework/TestCase.php @@ -35,7 +35,6 @@ use PHPUnit\Runner\PhptTestCase; use PHPUnit\Util\GlobalState; use PHPUnit\Util\InvalidArgumentHelper; -use PHPUnit\Util\PHP\AbstractPhpProcess; use Prophecy; use ReflectionClass; use ReflectionException; diff --git a/src/Runner/PhptTestCase.php b/src/Runner/PhptTestCase.php index 65dc1f16156..bcb403fe7fb 100644 --- a/src/Runner/PhptTestCase.php +++ b/src/Runner/PhptTestCase.php @@ -18,7 +18,6 @@ use PHPUnit\Framework\SkippedTestError; use PHPUnit\Framework\SelfDescribing; use PHPUnit\Util\InvalidArgumentHelper; -use PHPUnit\Util\PHP\AbstractPhpProcess; use PHPUnit\Util\PHP; use Throwable; @@ -33,7 +32,7 @@ class PhptTestCase implements Test, SelfDescribing private $filename; /** - * @var AbstractPhpProcess + * @var \PHPUnit\Util\PHP */ private $phpUtil; @@ -67,7 +66,7 @@ class PhptTestCase implements Test, SelfDescribing * Constructs a test case with the given filename. * * @param string $filename - * @param AbstractPhpProcess $phpUtil + * @param \PHPUnit\Util\PHP $phpUtil * * @throws Exception */ diff --git a/src/Util/PHP.php b/src/Util/PHP.php index c06e6d2e2bc..a0e7c0c6244 100644 --- a/src/Util/PHP.php +++ b/src/Util/PHP.php @@ -410,15 +410,15 @@ private function getException(TestFailure $error) */ public function runJob($job, array $settings = []) { - if ($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' - ); - } + if (!($this->tempFile = tempnam(sys_get_temp_dir(), 'PHPUnit')) || + file_put_contents($this->tempFile, $job) === false + ) { + throw new Exception( + 'Unable to write temporary file' + ); + } + if ($this->stdin) { $job = $this->stdin; } diff --git a/tests/Runner/PhptTestCaseTest.php b/tests/Runner/PhptTestCaseTest.php index 16c069c220b..82b7f104e1f 100644 --- a/tests/Runner/PhptTestCaseTest.php +++ b/tests/Runner/PhptTestCaseTest.php @@ -10,7 +10,7 @@ use PHPUnit\Framework\TestCase; use PHPUnit\Runner\PhptTestCase; -use PHPUnit\Util\PHP\AbstractPhpProcess; +use PHPUnit\Util\PHP; class Runner_PhptTestCaseTest extends TestCase { @@ -55,7 +55,7 @@ protected function setUp() $this->filename = sys_get_temp_dir() . '/phpunit.phpt'; touch($this->filename); - $this->phpUtil = $this->getMockForAbstractClass(AbstractPhpProcess::class, [], '', false); + $this->phpUtil = $this->createMock(PHP::class); $this->testCase = new PhptTestCase($this->filename, $this->phpUtil); } diff --git a/tests/Util/PHPTest.php b/tests/Util/PHPTest.php index a5ab24bf8b0..a0327ccc425 100644 --- a/tests/Util/PHPTest.php +++ b/tests/Util/PHPTest.php @@ -8,7 +8,7 @@ * file that was distributed with this source code. */ use PHPUnit\Framework\TestCase; -use PHPUnit\Util\PHP\AbstractPhpProcess; +use PHPUnit\Util\PHP; /** * @author Henrique Moody @@ -21,14 +21,14 @@ class PHPUnit_Util_PHPTest extends TestCase { public function testShouldNotUseStderrRedirectionByDefault() { - $phpMock = $this->getMockForAbstractClass(AbstractPhpProcess::class); + $phpMock = new PHP(); $this->assertFalse($phpMock->useStderrRedirection()); } public function testShouldDefinedIfUseStderrRedirection() { - $phpMock = $this->getMockForAbstractClass(AbstractPhpProcess::class); + $phpMock = new PHP(); $phpMock->setUseStderrRedirection(true); $this->assertTrue($phpMock->useStderrRedirection()); @@ -36,7 +36,7 @@ public function testShouldDefinedIfUseStderrRedirection() public function testShouldDefinedIfDoNotUseStderrRedirection() { - $phpMock = $this->getMockForAbstractClass(AbstractPhpProcess::class); + $phpMock = new PHP(); $phpMock->setUseStderrRedirection(false); $this->assertFalse($phpMock->useStderrRedirection()); @@ -44,7 +44,7 @@ public function testShouldDefinedIfDoNotUseStderrRedirection() public function testShouldThrowsExceptionWhenStderrRedirectionVariableIsNotABoolean() { - $phpMock = $this->getMockForAbstractClass(AbstractPhpProcess::class); + $phpMock = new PHP(); $this->expectException(PHPUnit\Framework\Exception::class); @@ -53,7 +53,7 @@ public function testShouldThrowsExceptionWhenStderrRedirectionVariableIsNotABool public function testShouldUseGivenSettingsToCreateCommand() { - $phpMock = $this->getMockForAbstractClass(AbstractPhpProcess::class); + $phpMock = new PHP(); $settings = [ 'allow_url_fopen=1', @@ -61,37 +61,37 @@ public function testShouldUseGivenSettingsToCreateCommand() 'display_errors=1', ]; - $expectedCommandFormat = '%s -d allow_url_fopen=1 -d auto_append_file= -d display_errors=1'; - $actualCommand = $phpMock->getCommand($settings); + $expectedCommandFormat = '%s -d allow_url_fopen=1 -d auto_append_file= -d display_errors=1 -f file.php'; + $actualCommand = $phpMock->getCommand($settings, 'file.php'); $this->assertStringMatchesFormat($expectedCommandFormat, $actualCommand); } public function testShouldRedirectStderrToStdoutWhenDefined() { - $phpMock = $this->getMockForAbstractClass(AbstractPhpProcess::class); + $phpMock = new PHP(); $phpMock->setUseStderrRedirection(true); $expectedCommandFormat = '%s 2>&1'; - $actualCommand = $phpMock->getCommand([]); + $actualCommand = $phpMock->getCommand([], 'file.php'); $this->assertStringMatchesFormat($expectedCommandFormat, $actualCommand); } public function testShouldUseArgsToCreateCommand() { - $phpMock = $this->getMockForAbstractClass(AbstractPhpProcess::class); + $phpMock = new PHP(); $phpMock->setArgs('foo=bar'); $expectedCommandFormat = '%s -- foo=bar'; - $actualCommand = $phpMock->getCommand([]); + $actualCommand = $phpMock->getCommand([], 'file.php'); $this->assertStringMatchesFormat($expectedCommandFormat, $actualCommand); } public function testShouldHaveFileToCreateCommand() { - $phpMock = $this->getMockForAbstractClass(AbstractPhpProcess::class); + $phpMock = new PHP(); $expectedCommandFormat = '%s -%c \'file.php\''; $actualCommand = $phpMock->getCommand([], 'file.php'); @@ -101,7 +101,7 @@ public function testShouldHaveFileToCreateCommand() public function testStdinGetterAndSetter() { - $phpMock = $this->getMockForAbstractClass(AbstractPhpProcess::class); + $phpMock = new PHP(); $phpMock->setStdin('foo'); $this->assertEquals('foo', $phpMock->getStdin()); @@ -109,7 +109,7 @@ public function testStdinGetterAndSetter() public function testArgsGetterAndSetter() { - $phpMock = $this->getMockForAbstractClass(AbstractPhpProcess::class); + $phpMock = new PHP(); $phpMock->setArgs('foo=bar'); $this->assertEquals('foo=bar', $phpMock->getArgs()); @@ -117,7 +117,7 @@ public function testArgsGetterAndSetter() public function testEnvGetterAndSetter() { - $phpMock = $this->getMockForAbstractClass(AbstractPhpProcess::class); + $phpMock = new PHP(); $phpMock->setEnv(['foo' => 'bar']); $this->assertEquals(['foo' => 'bar'], $phpMock->getEnv()); @@ -125,7 +125,7 @@ public function testEnvGetterAndSetter() public function testTimeoutGetterAndSetter() { - $phpMock = $this->getMockForAbstractClass(AbstractPhpProcess::class); + $phpMock = new PHP(); $phpMock->setTimeout(30); $this->assertEquals(30, $phpMock->getTimeout()); From 05dede01356f0263a662466b55b7a625e20e0dcb Mon Sep 17 00:00:00 2001 From: Daniel Wehner Date: Tue, 7 Mar 2017 10:19:09 +0100 Subject: [PATCH 5/5] fix indentation --- src/Util/PHP.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Util/PHP.php b/src/Util/PHP.php index a0e7c0c6244..1eccd207933 100644 --- a/src/Util/PHP.php +++ b/src/Util/PHP.php @@ -66,7 +66,7 @@ public function __construct() $this->runtime = new Runtime(); } - /** + /** * Defines if should use STDERR redirection or not. * * Then $stderrRedirection is TRUE, STDERR is redirected to STDOUT.