/** * @param PHPUnit_Runner_TestSuiteLoader $loader * @param PHP_CodeCoverage_Filter $filter * @since Method available since Release 3.4.0 */ public function __construct(PHPUnit_Runner_TestSuiteLoader $loader = null, PHP_CodeCoverage_Filter $filter = null) { if ($filter === null) { $filter = new PHP_CodeCoverage_Filter(); } $this->codeCoverageFilter = $filter; $this->loader = $loader; $runtime = new Runtime(); $this->canCollectCodeCoverage = $runtime->canCollectCodeCoverage(); }
/** * Constructor. * * @param PHP_CodeCoverage_Driver $driver * @param PHP_CodeCoverage_Filter $filter * @throws PHP_CodeCoverage_Exception */ public function __construct(PHP_CodeCoverage_Driver $driver = null, PHP_CodeCoverage_Filter $filter = null) { if ($driver === null) { $runtime = new Runtime(); if (!$runtime->hasXdebug()) { throw new PHP_CodeCoverage_Exception('No code coverage driver available'); } $driver = new PHP_CodeCoverage_Driver_Xdebug(); } if ($filter === null) { $filter = new PHP_CodeCoverage_Filter(); } $this->driver = $driver; $this->filter = $filter; }
/** * @param Job $job * @return bool */ public function startJob(Job $job) { $this->pool->add($job); try { $runtime = new Runtime(); $job->start($runtime->getBinary()); } catch (ForkException $e) { $this->pool->remove($job); $job->startTest(); $job->addError($e); $job->endTest(); return false; } $job->startTest(); return true; }
/** * Runs a single job (PHP code) using a separate PHP process. * * @param string $job * @param array $settings * * @return array * * @throws PHPUnit_Framework_Exception */ public function runJob($job, array $settings = []) { $runtime = new Runtime(); $process = proc_open($runtime->getBinary() . $this->settingsToParameters($settings), [0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes); if (!is_resource($process)) { throw new PHPUnit_Framework_Exception('Unable to spawn worker process'); } $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(); return ['stdout' => $stdout, 'stderr' => $stderr]; }
/** * Returns the command based into the configurations. * * @param array $settings * * @return string */ public function getCommand(array $settings) { $command = $this->runtime->getBinary(); $command .= $this->settingsToParameters($settings); if (true === $this->stderrRedirection) { $command .= ' 2>&1'; } return $command; }
/** * * {@inheritdoc} Reading from STDOUT or STDERR hangs forever on Windows if the output is * too large. * * @see https://bugs.php.net/bug.php?id=51800 */ public function runJob($job, array $settings = array()) { $runtime = new Runtime(); if (false === ($stdout_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')), $pipes); if (!is_resource($process)) { throw new PHPUnit_Framework_Exception('Unable to spawn worker process'); } $this->process($pipes[0], $job); fclose($pipes[0]); $stderr = stream_get_contents($pipes[2]); fclose($pipes[2]); proc_close($process); rewind($stdout_handle); $stdout = stream_get_contents($stdout_handle); fclose($stdout_handle); $this->cleanup(); return array('stdout' => $stdout, 'stderr' => $stderr); }
/** * Returns the command based into the configurations. * * @param array $settings * * @return string */ public function getCommand(array $settings) { $command = $this->runtime->getBinary(); $command .= $this->settingsToParameters($settings); if ('phpdbg' === PHP_SAPI) { $command .= ' -qrr ' . escapeshellarg(__DIR__ . '/PHP/eval-stdin.php'); } if (true === $this->stderrRedirection) { $command .= ' 2>&1'; } return $command; }
/** * @param array $arguments * @since Method available since Release 3.2.1 */ protected function handleConfiguration(array &$arguments) { if (isset($arguments['configuration']) && !$arguments['configuration'] instanceof PHPUnit_Util_Configuration) { $arguments['configuration'] = PHPUnit_Util_Configuration::getInstance($arguments['configuration']); } $arguments['debug'] = isset($arguments['debug']) ? $arguments['debug'] : false; $arguments['filter'] = isset($arguments['filter']) ? $arguments['filter'] : false; $arguments['listeners'] = isset($arguments['listeners']) ? $arguments['listeners'] : array(); if (isset($arguments['configuration'])) { $arguments['configuration']->handlePHPConfiguration(); $phpunitConfiguration = $arguments['configuration']->getPHPUnitConfiguration(); if (isset($phpunitConfiguration['deprecatedStrictModeSetting'])) { $arguments['deprecatedStrictModeSetting'] = true; } if (isset($phpunitConfiguration['backupGlobals']) && !isset($arguments['backupGlobals'])) { $arguments['backupGlobals'] = $phpunitConfiguration['backupGlobals']; } if (isset($phpunitConfiguration['backupStaticAttributes']) && !isset($arguments['backupStaticAttributes'])) { $arguments['backupStaticAttributes'] = $phpunitConfiguration['backupStaticAttributes']; } if (isset($phpunitConfiguration['disallowChangesToGlobalState']) && !isset($arguments['disallowChangesToGlobalState'])) { $arguments['disallowChangesToGlobalState'] = $phpunitConfiguration['disallowChangesToGlobalState']; } if (isset($phpunitConfiguration['bootstrap']) && !isset($arguments['bootstrap'])) { $arguments['bootstrap'] = $phpunitConfiguration['bootstrap']; } if (isset($phpunitConfiguration['cacheTokens']) && !isset($arguments['cacheTokens'])) { $arguments['cacheTokens'] = $phpunitConfiguration['cacheTokens']; } if (isset($phpunitConfiguration['colors']) && !isset($arguments['colors'])) { $arguments['colors'] = $phpunitConfiguration['colors']; } if (isset($phpunitConfiguration['convertErrorsToExceptions']) && !isset($arguments['convertErrorsToExceptions'])) { $arguments['convertErrorsToExceptions'] = $phpunitConfiguration['convertErrorsToExceptions']; } if (isset($phpunitConfiguration['convertNoticesToExceptions']) && !isset($arguments['convertNoticesToExceptions'])) { $arguments['convertNoticesToExceptions'] = $phpunitConfiguration['convertNoticesToExceptions']; } if (isset($phpunitConfiguration['convertWarningsToExceptions']) && !isset($arguments['convertWarningsToExceptions'])) { $arguments['convertWarningsToExceptions'] = $phpunitConfiguration['convertWarningsToExceptions']; } if (isset($phpunitConfiguration['processIsolation']) && !isset($arguments['processIsolation'])) { $arguments['processIsolation'] = $phpunitConfiguration['processIsolation']; } if (isset($phpunitConfiguration['stopOnFailure']) && !isset($arguments['stopOnFailure'])) { $arguments['stopOnFailure'] = $phpunitConfiguration['stopOnFailure']; } if (isset($phpunitConfiguration['timeoutForSmallTests']) && !isset($arguments['timeoutForSmallTests'])) { $arguments['timeoutForSmallTests'] = $phpunitConfiguration['timeoutForSmallTests']; } if (isset($phpunitConfiguration['timeoutForMediumTests']) && !isset($arguments['timeoutForMediumTests'])) { $arguments['timeoutForMediumTests'] = $phpunitConfiguration['timeoutForMediumTests']; } if (isset($phpunitConfiguration['timeoutForLargeTests']) && !isset($arguments['timeoutForLargeTests'])) { $arguments['timeoutForLargeTests'] = $phpunitConfiguration['timeoutForLargeTests']; } if (isset($phpunitConfiguration['reportUselessTests']) && !isset($arguments['reportUselessTests'])) { $arguments['reportUselessTests'] = $phpunitConfiguration['reportUselessTests']; } if (isset($phpunitConfiguration['strictCoverage']) && !isset($arguments['strictCoverage'])) { $arguments['strictCoverage'] = $phpunitConfiguration['strictCoverage']; } if (isset($phpunitConfiguration['disallowTestOutput']) && !isset($arguments['disallowTestOutput'])) { $arguments['disallowTestOutput'] = $phpunitConfiguration['disallowTestOutput']; } if (isset($phpunitConfiguration['enforceTimeLimit']) && !isset($arguments['enforceTimeLimit'])) { $arguments['enforceTimeLimit'] = $phpunitConfiguration['enforceTimeLimit']; } if (isset($phpunitConfiguration['disallowTodoAnnotatedTests']) && !isset($arguments['disallowTodoAnnotatedTests'])) { $arguments['disallowTodoAnnotatedTests'] = $phpunitConfiguration['disallowTodoAnnotatedTests']; } if (isset($phpunitConfiguration['verbose']) && !isset($arguments['verbose'])) { $arguments['verbose'] = $phpunitConfiguration['verbose']; } if (isset($phpunitConfiguration['forceCoversAnnotation']) && !isset($arguments['forceCoversAnnotation'])) { $arguments['forceCoversAnnotation'] = $phpunitConfiguration['forceCoversAnnotation']; } if (isset($phpunitConfiguration['mapTestClassNameToCoveredClassName']) && !isset($arguments['mapTestClassNameToCoveredClassName'])) { $arguments['mapTestClassNameToCoveredClassName'] = $phpunitConfiguration['mapTestClassNameToCoveredClassName']; } $groupCliArgs = array(); if (!empty($arguments['groups'])) { $groupCliArgs = $arguments['groups']; } $groupConfiguration = $arguments['configuration']->getGroupConfiguration(); if (!empty($groupConfiguration['include']) && !isset($arguments['groups'])) { $arguments['groups'] = $groupConfiguration['include']; } if (!empty($groupConfiguration['exclude']) && !isset($arguments['excludeGroups'])) { $arguments['excludeGroups'] = array_diff($groupConfiguration['exclude'], $groupCliArgs); } foreach ($arguments['configuration']->getListenerConfiguration() as $listener) { if (!class_exists($listener['class'], false) && $listener['file'] !== '') { require_once $listener['file']; } if (class_exists($listener['class'])) { if (count($listener['arguments']) == 0) { $listener = new $listener['class'](); } else { $listenerClass = new ReflectionClass($listener['class']); $listener = $listenerClass->newInstanceArgs($listener['arguments']); } if ($listener instanceof PHPUnit_Framework_TestListener) { $arguments['listeners'][] = $listener; } } } $loggingConfiguration = $arguments['configuration']->getLoggingConfiguration(); if (isset($loggingConfiguration['coverage-clover']) && !isset($arguments['coverageClover'])) { $arguments['coverageClover'] = $loggingConfiguration['coverage-clover']; } if (isset($loggingConfiguration['coverage-crap4j']) && !isset($arguments['coverageCrap4J'])) { $arguments['coverageCrap4J'] = $loggingConfiguration['coverage-crap4j']; } if (isset($loggingConfiguration['coverage-html']) && !isset($arguments['coverageHtml'])) { if (isset($loggingConfiguration['lowUpperBound']) && !isset($arguments['reportLowUpperBound'])) { $arguments['reportLowUpperBound'] = $loggingConfiguration['lowUpperBound']; } if (isset($loggingConfiguration['highLowerBound']) && !isset($arguments['reportHighLowerBound'])) { $arguments['reportHighLowerBound'] = $loggingConfiguration['highLowerBound']; } $arguments['coverageHtml'] = $loggingConfiguration['coverage-html']; } if (isset($loggingConfiguration['coverage-php']) && !isset($arguments['coveragePHP'])) { $arguments['coveragePHP'] = $loggingConfiguration['coverage-php']; } if (isset($loggingConfiguration['coverage-text']) && !isset($arguments['coverageText'])) { $arguments['coverageText'] = $loggingConfiguration['coverage-text']; if (isset($loggingConfiguration['coverageTextShowUncoveredFiles'])) { $arguments['coverageTextShowUncoveredFiles'] = $loggingConfiguration['coverageTextShowUncoveredFiles']; } else { $arguments['coverageTextShowUncoveredFiles'] = false; } if (isset($loggingConfiguration['coverageTextShowOnlySummary'])) { $arguments['coverageTextShowOnlySummary'] = $loggingConfiguration['coverageTextShowOnlySummary']; } else { $arguments['coverageTextShowOnlySummary'] = false; } } if (isset($loggingConfiguration['coverage-xml']) && !isset($arguments['coverageXml'])) { $arguments['coverageXml'] = $loggingConfiguration['coverage-xml']; } if (isset($loggingConfiguration['json']) && !isset($arguments['jsonLogfile'])) { $arguments['jsonLogfile'] = $loggingConfiguration['json']; } if (isset($loggingConfiguration['plain'])) { $arguments['listeners'][] = new PHPUnit_TextUI_ResultPrinter($loggingConfiguration['plain'], true); } if (isset($loggingConfiguration['tap']) && !isset($arguments['tapLogfile'])) { $arguments['tapLogfile'] = $loggingConfiguration['tap']; } if (isset($loggingConfiguration['junit']) && !isset($arguments['junitLogfile'])) { $arguments['junitLogfile'] = $loggingConfiguration['junit']; if (isset($loggingConfiguration['logIncompleteSkipped']) && !isset($arguments['logIncompleteSkipped'])) { $arguments['logIncompleteSkipped'] = $loggingConfiguration['logIncompleteSkipped']; } } if (isset($loggingConfiguration['testdox-html']) && !isset($arguments['testdoxHTMLFile'])) { $arguments['testdoxHTMLFile'] = $loggingConfiguration['testdox-html']; } if (isset($loggingConfiguration['testdox-text']) && !isset($arguments['testdoxTextFile'])) { $arguments['testdoxTextFile'] = $loggingConfiguration['testdox-text']; } if ((isset($arguments['coverageClover']) || isset($arguments['coverageCrap4J']) || isset($arguments['coverageHtml']) || isset($arguments['coveragePHP']) || isset($arguments['coverageText']) || isset($arguments['coverageXml'])) && $this->runtime->canCollectCodeCoverage()) { $filterConfiguration = $arguments['configuration']->getFilterConfiguration(); $arguments['addUncoveredFilesFromWhitelist'] = $filterConfiguration['whitelist']['addUncoveredFilesFromWhitelist']; $arguments['processUncoveredFilesFromWhitelist'] = $filterConfiguration['whitelist']['processUncoveredFilesFromWhitelist']; if (empty($filterConfiguration['whitelist']['include']['directory']) && empty($filterConfiguration['whitelist']['include']['file'])) { foreach ($filterConfiguration['blacklist']['include']['directory'] as $dir) { $this->codeCoverageFilter->addDirectoryToBlacklist($dir['path'], $dir['suffix'], $dir['prefix'], $dir['group']); } foreach ($filterConfiguration['blacklist']['include']['file'] as $file) { $this->codeCoverageFilter->addFileToBlacklist($file); } foreach ($filterConfiguration['blacklist']['exclude']['directory'] as $dir) { $this->codeCoverageFilter->removeDirectoryFromBlacklist($dir['path'], $dir['suffix'], $dir['prefix'], $dir['group']); } foreach ($filterConfiguration['blacklist']['exclude']['file'] as $file) { $this->codeCoverageFilter->removeFileFromBlacklist($file); } } foreach ($filterConfiguration['whitelist']['include']['directory'] as $dir) { $this->codeCoverageFilter->addDirectoryToWhitelist($dir['path'], $dir['suffix'], $dir['prefix']); } foreach ($filterConfiguration['whitelist']['include']['file'] as $file) { $this->codeCoverageFilter->addFileToWhitelist($file); } foreach ($filterConfiguration['whitelist']['exclude']['directory'] as $dir) { $this->codeCoverageFilter->removeDirectoryFromWhitelist($dir['path'], $dir['suffix'], $dir['prefix']); } foreach ($filterConfiguration['whitelist']['exclude']['file'] as $file) { $this->codeCoverageFilter->removeFileFromWhitelist($file); } } } $arguments['addUncoveredFilesFromWhitelist'] = isset($arguments['addUncoveredFilesFromWhitelist']) ? $arguments['addUncoveredFilesFromWhitelist'] : true; $arguments['processUncoveredFilesFromWhitelist'] = isset($arguments['processUncoveredFilesFromWhitelist']) ? $arguments['processUncoveredFilesFromWhitelist'] : false; $arguments['backupGlobals'] = isset($arguments['backupGlobals']) ? $arguments['backupGlobals'] : null; $arguments['backupStaticAttributes'] = isset($arguments['backupStaticAttributes']) ? $arguments['backupStaticAttributes'] : null; $arguments['disallowChangesToGlobalState'] = isset($arguments['disallowChangesToGlobalState']) ? $arguments['disallowChangesToGlobalState'] : null; $arguments['cacheTokens'] = isset($arguments['cacheTokens']) ? $arguments['cacheTokens'] : false; $arguments['columns'] = isset($arguments['columns']) ? $arguments['columns'] : 80; $arguments['colors'] = isset($arguments['colors']) ? $arguments['colors'] : PHPUnit_TextUI_ResultPrinter::COLOR_DEFAULT; $arguments['convertErrorsToExceptions'] = isset($arguments['convertErrorsToExceptions']) ? $arguments['convertErrorsToExceptions'] : true; $arguments['convertNoticesToExceptions'] = isset($arguments['convertNoticesToExceptions']) ? $arguments['convertNoticesToExceptions'] : true; $arguments['convertWarningsToExceptions'] = isset($arguments['convertWarningsToExceptions']) ? $arguments['convertWarningsToExceptions'] : true; $arguments['excludeGroups'] = isset($arguments['excludeGroups']) ? $arguments['excludeGroups'] : array(); $arguments['groups'] = isset($arguments['groups']) ? $arguments['groups'] : array(); $arguments['logIncompleteSkipped'] = isset($arguments['logIncompleteSkipped']) ? $arguments['logIncompleteSkipped'] : false; $arguments['processIsolation'] = isset($arguments['processIsolation']) ? $arguments['processIsolation'] : false; $arguments['repeat'] = isset($arguments['repeat']) ? $arguments['repeat'] : false; $arguments['reportHighLowerBound'] = isset($arguments['reportHighLowerBound']) ? $arguments['reportHighLowerBound'] : 90; $arguments['reportLowUpperBound'] = isset($arguments['reportLowUpperBound']) ? $arguments['reportLowUpperBound'] : 50; $arguments['stopOnError'] = isset($arguments['stopOnError']) ? $arguments['stopOnError'] : false; $arguments['stopOnFailure'] = isset($arguments['stopOnFailure']) ? $arguments['stopOnFailure'] : false; $arguments['stopOnIncomplete'] = isset($arguments['stopOnIncomplete']) ? $arguments['stopOnIncomplete'] : false; $arguments['stopOnRisky'] = isset($arguments['stopOnRisky']) ? $arguments['stopOnRisky'] : false; $arguments['stopOnSkipped'] = isset($arguments['stopOnSkipped']) ? $arguments['stopOnSkipped'] : false; $arguments['timeoutForSmallTests'] = isset($arguments['timeoutForSmallTests']) ? $arguments['timeoutForSmallTests'] : 1; $arguments['timeoutForMediumTests'] = isset($arguments['timeoutForMediumTests']) ? $arguments['timeoutForMediumTests'] : 10; $arguments['timeoutForLargeTests'] = isset($arguments['timeoutForLargeTests']) ? $arguments['timeoutForLargeTests'] : 60; $arguments['reportUselessTests'] = isset($arguments['reportUselessTests']) ? $arguments['reportUselessTests'] : false; $arguments['strictCoverage'] = isset($arguments['strictCoverage']) ? $arguments['strictCoverage'] : false; $arguments['disallowTestOutput'] = isset($arguments['disallowTestOutput']) ? $arguments['disallowTestOutput'] : false; $arguments['enforceTimeLimit'] = isset($arguments['enforceTimeLimit']) ? $arguments['enforceTimeLimit'] : false; $arguments['disallowTodoAnnotatedTests'] = isset($arguments['disallowTodoAnnotatedTests']) ? $arguments['disallowTodoAnnotatedTests'] : false; $arguments['verbose'] = isset($arguments['verbose']) ? $arguments['verbose'] : false; }
/** * @covers \SebastianBergmann\Environment\Runtime::getVendorUrl * * @uses \SebastianBergmann\Environment\Runtime::isHHVM */ public function testVendorUrlCanBeRetrieved() { $this->assertInternalType('string', $this->env->getVendorUrl()); }
/** * @param Text_Template $template * @param PHP_CodeCoverage_Report_Node $node */ protected function setCommonTemplateVariables(Text_Template $template, PHP_CodeCoverage_Report_Node $node) { $runtime = new Runtime(); $template->setVar(array('id' => $node->getId(), 'full_path' => $node->getPath(), 'path_to_root' => $this->getPathToRoot($node), 'breadcrumbs' => $this->getBreadcrumbs($node), 'date' => $this->date, 'version' => $this->version, 'runtime_name' => $runtime->getName(), 'runtime_version' => $runtime->getVersion(), 'runtime_link' => $runtime->getVendorUrl(), 'generator' => $this->generator, 'low_upper_bound' => $this->lowUpperBound, 'high_lower_bound' => $this->highLowerBound)); }
/** * @return PHP_CodeCoverage_Driver * @throws PHP_CodeCoverage_RuntimeException */ private function selectDriver() { $runtime = new Runtime(); if (!$runtime->canCollectCodeCoverage()) { throw new PHP_CodeCoverage_RuntimeException('No code coverage driver available'); } if ($runtime->isHHVM()) { return new PHP_CodeCoverage_Driver_HHVM(); } elseif ($runtime->isPHPDBG()) { return new PHP_CodeCoverage_Driver_PHPDBG(); } else { return new PHP_CodeCoverage_Driver_Xdebug(); } }
public function doRun(PHPUnit_Framework_Test $suite, array $arguments = array()) { $this->handleConfiguration($arguments); $this->processSuiteFilters($suite, $arguments); // ----------------------- // get the tests that failed last time this same command (without rerun) was used // from file and rerun them if (isset($arguments['rerun'])) { $cache = new CacheUtil(); $key = $cache->generateKey($_SERVER['argv']); if ($cache->fileExists($key)) { $rerunnableTests = $cache->readCache($key); $suite = new PHPUnit_Framework_TestSuite(); foreach ($rerunnableTests as $testName) { $class = new ReflectionClass($testName['testClassName']); $methodName = $testName['testMethodName']; $test = $suite::createTest($class, $methodName); $suite->addTest($test); } } } // ------------------------------ if (isset($arguments['bootstrap'])) { $GLOBALS['__PHPUNIT_BOOTSTRAP'] = $arguments['bootstrap']; } if ($arguments['backupGlobals'] === false) { $suite->setBackupGlobals(false); } if ($arguments['backupStaticAttributes'] === true) { $suite->setBackupStaticAttributes(true); } if ($arguments['disallowChangesToGlobalState'] === true) { $suite->setDisallowChangesToGlobalState(true); } if (is_integer($arguments['repeat'])) { $test = new PHPUnit_Extensions_RepeatedTest($suite, $arguments['repeat'], $arguments['processIsolation']); $suite = new PHPUnit_Framework_TestSuite(); $suite->addTest($test); } $result = $this->createTestResult(); if (!$arguments['convertErrorsToExceptions']) { $result->convertErrorsToExceptions(false); } if (!$arguments['convertNoticesToExceptions']) { PHPUnit_Framework_Error_Notice::$enabled = false; } if (!$arguments['convertWarningsToExceptions']) { PHPUnit_Framework_Error_Warning::$enabled = false; } if ($arguments['stopOnError']) { $result->stopOnError(true); } if ($arguments['stopOnFailure']) { $result->stopOnFailure(true); } if ($arguments['stopOnIncomplete']) { $result->stopOnIncomplete(true); } if ($arguments['stopOnRisky']) { $result->stopOnRisky(true); } if ($arguments['stopOnSkipped']) { $result->stopOnSkipped(true); } if ($this->printer === null) { if (isset($arguments['printer']) && $arguments['printer'] instanceof PHPUnit_Util_Printer) { $this->printer = $arguments['printer']; } else { $printerClass = 'PHPUnit_TextUI_ResultPrinter'; if (isset($arguments['printer']) && is_string($arguments['printer']) && class_exists($arguments['printer'], false)) { $class = new ReflectionClass($arguments['printer']); if ($class->isSubclassOf('PHPUnit_TextUI_ResultPrinter')) { $printerClass = $arguments['printer']; } } $this->printer = new $printerClass(isset($arguments['stderr']) ? 'php://stderr' : null, $arguments['verbose'], $arguments['colors'], $arguments['debug'], $arguments['columns']); } } if (!$this->printer instanceof PHPUnit_Util_Log_TAP) { $this->printer->write(PHPUnit_Runner_Version::getVersionString() . "\n"); self::$versionStringPrinted = true; if ($arguments['verbose']) { $this->printer->write(sprintf("\nRuntime:\t%s", $this->runtime->getNameWithVersion())); if ($this->runtime->hasXdebug()) { $this->printer->write(sprintf(' with Xdebug %s', phpversion('xdebug'))); } if (isset($arguments['configuration'])) { $this->printer->write(sprintf("\nConfiguration:\t%s", $arguments['configuration']->getFilename())); } $this->printer->write("\n"); } if (isset($arguments['deprecatedStrictModeOption'])) { print "Warning:\tDeprecated option \"--strict\" used\n"; } elseif (isset($arguments['deprecatedStrictModeSetting'])) { print "Warning:\tDeprecated configuration setting \"strict\" used\n"; } if (isset($arguments['deprecatedSeleniumConfiguration'])) { print "Warning:\tDeprecated configuration setting \"selenium\" used\n"; } } foreach ($arguments['listeners'] as $listener) { $result->addListener($listener); } $result->addListener($this->printer); if (isset($arguments['testdoxHTMLFile'])) { $result->addListener(new PHPUnit_Util_TestDox_ResultPrinter_HTML($arguments['testdoxHTMLFile'])); } if (isset($arguments['testdoxTextFile'])) { $result->addListener(new PHPUnit_Util_TestDox_ResultPrinter_Text($arguments['testdoxTextFile'])); } $codeCoverageReports = 0; if (isset($arguments['coverageClover'])) { $codeCoverageReports++; } if (isset($arguments['coverageCrap4J'])) { $codeCoverageReports++; } if (isset($arguments['coverageHtml'])) { $codeCoverageReports++; } if (isset($arguments['coveragePHP'])) { $codeCoverageReports++; } if (isset($arguments['coverageText'])) { $codeCoverageReports++; } if (isset($arguments['coverageXml'])) { $codeCoverageReports++; } if (!$this->printer instanceof PHPUnit_Util_Log_TAP) { if ($codeCoverageReports > 0 && !$this->codeCoverageFilter->hasWhitelist()) { $this->printer->write("Warning:\tNo whitelist configured for code coverage\n"); } $this->printer->write("\n"); } if ($codeCoverageReports > 0 && (!extension_loaded('tokenizer') || !$this->runtime->canCollectCodeCoverage())) { if (!extension_loaded('tokenizer')) { $this->showExtensionNotLoadedMessage('tokenizer', 'No code coverage will be generated.'); } elseif (!extension_loaded('Xdebug')) { $this->showExtensionNotLoadedMessage('Xdebug', 'No code coverage will be generated.'); } $codeCoverageReports = 0; } if ($codeCoverageReports > 0) { $codeCoverage = new PHP_CodeCoverage(null, $this->codeCoverageFilter); $codeCoverage->setAddUncoveredFilesFromWhitelist($arguments['addUncoveredFilesFromWhitelist']); $codeCoverage->setCheckForUnintentionallyCoveredCode($arguments['strictCoverage']); $codeCoverage->setProcessUncoveredFilesFromWhitelist($arguments['processUncoveredFilesFromWhitelist']); if (isset($arguments['forceCoversAnnotation'])) { $codeCoverage->setForceCoversAnnotation($arguments['forceCoversAnnotation']); } if (isset($arguments['mapTestClassNameToCoveredClassName'])) { $codeCoverage->setMapTestClassNameToCoveredClassName($arguments['mapTestClassNameToCoveredClassName']); } $result->setCodeCoverage($codeCoverage); } if ($codeCoverageReports > 1) { if (isset($arguments['cacheTokens'])) { $codeCoverage->setCacheTokens($arguments['cacheTokens']); } } if (isset($arguments['jsonLogfile'])) { $result->addListener(new PHPUnit_Util_Log_JSON($arguments['jsonLogfile'])); } if (isset($arguments['tapLogfile'])) { $result->addListener(new PHPUnit_Util_Log_TAP($arguments['tapLogfile'])); } if (isset($arguments['junitLogfile'])) { $result->addListener(new PHPUnit_Util_Log_JUnit($arguments['junitLogfile'], $arguments['logIncompleteSkipped'])); } $result->beStrictAboutTestsThatDoNotTestAnything($arguments['reportUselessTests']); $result->beStrictAboutOutputDuringTests($arguments['disallowTestOutput']); $result->beStrictAboutTodoAnnotatedTests($arguments['disallowTodoAnnotatedTests']); $result->beStrictAboutTestSize($arguments['enforceTimeLimit']); $result->setTimeoutForSmallTests($arguments['timeoutForSmallTests']); $result->setTimeoutForMediumTests($arguments['timeoutForMediumTests']); $result->setTimeoutForLargeTests($arguments['timeoutForLargeTests']); if ($suite instanceof PHPUnit_Framework_TestSuite) { $suite->setRunTestInSeparateProcess($arguments['processIsolation']); } // -------------------------------------------- $result->addListener(new PHPUnit_Framework_FailureListener()); // ----------------------------------------------- $suite->run($result); unset($suite); $result->flushListeners(); if ($this->printer instanceof PHPUnit_TextUI_ResultPrinter) { $this->printer->printResult($result); } if (isset($codeCoverage)) { if (isset($arguments['coverageClover'])) { $this->printer->write("\nGenerating code coverage report in Clover XML format ..."); $writer = new PHP_CodeCoverage_Report_Clover(); $writer->process($codeCoverage, $arguments['coverageClover']); $this->printer->write(" done\n"); unset($writer); } if (isset($arguments['coverageCrap4J'])) { $this->printer->write("\nGenerating Crap4J report XML file ..."); $writer = new PHP_CodeCoverage_Report_Crap4j(); $writer->process($codeCoverage, $arguments['coverageCrap4J']); $this->printer->write(" done\n"); unset($writer); } if (isset($arguments['coverageHtml'])) { $this->printer->write("\nGenerating code coverage report in HTML format ..."); $writer = new PHP_CodeCoverage_Report_HTML($arguments['reportLowUpperBound'], $arguments['reportHighLowerBound'], sprintf(' and <a href="http://phpunit.de/">PHPUnit %s</a>', PHPUnit_Runner_Version::id())); $writer->process($codeCoverage, $arguments['coverageHtml']); $this->printer->write(" done\n"); unset($writer); } if (isset($arguments['coveragePHP'])) { $this->printer->write("\nGenerating code coverage report in PHP format ..."); $writer = new PHP_CodeCoverage_Report_PHP(); $writer->process($codeCoverage, $arguments['coveragePHP']); $this->printer->write(" done\n"); unset($writer); } if (isset($arguments['coverageText'])) { if ($arguments['coverageText'] == 'php://stdout') { $outputStream = $this->printer; $colors = $arguments['colors']; } else { $outputStream = new PHPUnit_Util_Printer($arguments['coverageText']); $colors = false; } $processor = new PHP_CodeCoverage_Report_Text($arguments['reportLowUpperBound'], $arguments['reportHighLowerBound'], $arguments['coverageTextShowUncoveredFiles'], $arguments['coverageTextShowOnlySummary']); $outputStream->write($processor->process($codeCoverage, $colors)); } if (isset($arguments['coverageXml'])) { $this->printer->write("\nGenerating code coverage report in PHPUnit XML format ..."); $writer = new PHP_CodeCoverage_Report_XML(); $writer->process($codeCoverage, $arguments['coverageXml']); $this->printer->write(" done\n"); unset($writer); } } return $result; }
/** * Constructor. * * @param PHP_CodeCoverage_Driver $driver * @param PHP_CodeCoverage_Filter $filter * @throws PHP_CodeCoverage_Exception */ public function __construct(PHP_CodeCoverage_Driver $driver = null, PHP_CodeCoverage_Filter $filter = null) { if ($filter === null) { $filter = new PHP_CodeCoverage_Filter(); } $parser = new PHP_CodeCoverage_Parser(); if ($driver === null) { $runtime = new Runtime(); if ($runtime->isHHVM()) { $driver = new PHP_CodeCoverage_Driver_HHVM($filter, $parser); } elseif ($runtime->hasXdebug()) { $driver = new PHP_CodeCoverage_Driver_Xdebug($filter, $parser); } else { throw new PHP_CodeCoverage_Exception('No code coverage driver available'); } } $this->driver = $driver; $this->filter = $filter; $this->parser = $parser; }
/** * Runs a TestCase. * * @param PHPUnit_Framework_Test $test */ public function run(PHPUnit_Framework_Test $test) { PHPUnit_Framework_Assert::resetCount(); $error = false; $failure = false; $incomplete = false; $risky = false; $skipped = false; $this->startTest($test); $errorHandlerSet = false; if ($this->convertErrorsToExceptions) { $oldErrorHandler = set_error_handler(array('PHPUnit_Util_ErrorHandler', 'handleError'), E_ALL | E_STRICT); if ($oldErrorHandler === null) { $errorHandlerSet = true; } else { restore_error_handler(); } } $runtime = new Runtime(); $canCollectCodeCoverage = $runtime->canCollectCodeCoverage(); $collectCodeCoverage = $canCollectCodeCoverage && $this->codeCoverage !== null && !$test instanceof PHPUnit_Extensions_SeleniumTestCase && !$test instanceof PHPUnit_Framework_Warning; if ($collectCodeCoverage) { // We need to blacklist test source files when no whitelist is used. if (!$this->codeCoverage->filter()->hasWhitelist()) { $classes = $this->getHierarchy(get_class($test), true); foreach ($classes as $class) { $this->codeCoverage->filter()->addFileToBlacklist($class->getFileName()); } } $this->codeCoverage->start($test); } PHP_Timer::start(); try { if (!$test instanceof PHPUnit_Framework_Warning && $this->beStrictAboutTestSize && extension_loaded('pcntl') && class_exists('PHP_Invoker')) { switch ($test->getSize()) { case PHPUnit_Util_Test::SMALL: $_timeout = $this->timeoutForSmallTests; break; case PHPUnit_Util_Test::MEDIUM: $_timeout = $this->timeoutForMediumTests; break; case PHPUnit_Util_Test::LARGE: $_timeout = $this->timeoutForLargeTests; break; } $invoker = new PHP_Invoker(); $invoker->invoke(array($test, 'runBare'), array(), $_timeout); } else { $test->runBare(); } } catch (PHPUnit_Framework_AssertionFailedError $e) { $failure = true; if ($e instanceof PHPUnit_Framework_RiskyTestError) { $risky = true; } elseif ($e instanceof PHPUnit_Framework_IncompleteTestError) { $incomplete = true; } elseif ($e instanceof PHPUnit_Framework_SkippedTestError) { $skipped = true; } } catch (Exception $e) { $error = true; } $time = PHP_Timer::stop(); $test->addToAssertionCount(PHPUnit_Framework_Assert::getCount()); if ($this->beStrictAboutTestsThatDoNotTestAnything && $test->getNumAssertions() == 0) { $risky = true; } if ($collectCodeCoverage) { $append = !$risky && !$incomplete && !$skipped; $linesToBeCovered = array(); $linesToBeUsed = array(); if ($append && $test instanceof PHPUnit_Framework_TestCase) { $linesToBeCovered = PHPUnit_Util_Test::getLinesToBeCovered(get_class($test), $test->getName()); $linesToBeUsed = PHPUnit_Util_Test::getLinesToBeUsed(get_class($test), $test->getName()); } try { $this->codeCoverage->stop($append, $linesToBeCovered, $linesToBeUsed); } catch (PHP_CodeCoverage_Exception_UnintentionallyCoveredCode $cce) { $this->addFailure($test, new PHPUnit_Framework_UnintentionallyCoveredCodeError('This test executed code that is not listed as code to be covered or used:' . PHP_EOL . $cce->getMessage()), $time); } catch (PHPUnit_Framework_InvalidCoversTargetException $cce) { $this->addFailure($test, new PHPUnit_Framework_InvalidCoversTargetError($cce->getMessage()), $time); } catch (PHP_CodeCoverage_Exception $cce) { $error = true; if (!isset($e)) { $e = $cce; } } } if ($errorHandlerSet === true) { restore_error_handler(); } if ($error === true) { $this->addError($test, $e, $time); } elseif ($failure === true) { $this->addFailure($test, $e, $time); } elseif ($this->beStrictAboutTestsThatDoNotTestAnything && $test->getNumAssertions() == 0) { $this->addFailure($test, new PHPUnit_Framework_RiskyTestError('This test did not perform any assertions'), $time); } elseif ($this->beStrictAboutOutputDuringTests && $test->hasOutput()) { $this->addFailure($test, new PHPUnit_Framework_OutputError(sprintf('This test printed output: %s', $test->getActualOutput())), $time); } $this->endTest($test, $time); }
/** * @return string */ private function getRuntimeString() { $runtime = new Runtime(); $buffer = sprintf('<a href="%s" target="_top">%s %s</a>', $runtime->getVendorUrl(), $runtime->getName(), $runtime->getVersion()); if ($runtime->hasXdebug() && !$runtime->hasPHPDBGCodeCoverage()) { $buffer .= sprintf(' with <a href="https://xdebug.org/">Xdebug %s</a>', phpversion('xdebug')); } return $buffer; }
private function getPhpBinary() { $runtime = new Runtime(); return $runtime->getBinary(); }