Author: Sebastian Bergmann (sebastian@phpunit.de)
Inheritance: extends PHPUnit_Framework_TestCase
示例#1
0
 /**
  * A warning occurred.
  *
  * @param PHPUnit_Framework_Test    $test
  * @param PHPUnit_Framework_Warning $e
  * @param float                     $time
  *
  * @since Method available since Release 5.1.0
  */
 public function addWarning(PHPUnit_Framework_Test $test, PHPUnit_Framework_Warning $e, $time)
 {
     $data = null;
     if ($e instanceof PHPUnit_Framework_ExpectationFailedException) {
         $cf = $e->getComparisonFailure();
         if ($cf !== null) {
             $data = ['got' => $cf->getActual(), 'expected' => $cf->getExpected()];
         }
     }
     $this->writeCase('warning', $time, PHPUnit_Util_Filter::getFilteredStacktrace($e, false), $e->getMessage(), $test, $data);
     $this->currentTestPass = false;
 }
 /**
  * Runs a TestCase.
  *
  * @param PHPUnit_Framework_Test $test
  */
 public function run(PHPUnit_Framework_Test $test)
 {
     PHPUnit_Framework_Assert::resetCount();
     $error = false;
     $failure = false;
     $warning = false;
     $incomplete = false;
     $risky = false;
     $skipped = false;
     $this->startTest($test);
     $errorHandlerSet = false;
     if ($this->convertErrorsToExceptions) {
         $oldErrorHandler = set_error_handler(['PHPUnit_Util_ErrorHandler', 'handleError'], E_ALL | E_STRICT);
         if ($oldErrorHandler === null) {
             $errorHandlerSet = true;
         } else {
             restore_error_handler();
         }
     }
     $collectCodeCoverage = $this->codeCoverage !== null && !$test instanceof PHPUnit_Framework_WarningTestCase;
     if ($collectCodeCoverage) {
         $this->codeCoverage->start($test);
     }
     $monitorFunctions = $this->beStrictAboutResourceUsageDuringSmallTests && !$test instanceof PHPUnit_Framework_WarningTestCase && $test->getSize() == PHPUnit_Util_Test::SMALL && function_exists('xdebug_start_function_monitor');
     if ($monitorFunctions) {
         xdebug_start_function_monitor(ResourceOperations::getFunctions());
     }
     PHP_Timer::start();
     try {
         if (!$test instanceof PHPUnit_Framework_WarningTestCase && $test->getSize() != PHPUnit_Util_Test::UNKNOWN && $this->enforceTimeLimit && 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([$test, 'runBare'], [], $_timeout);
         } else {
             $test->runBare();
         }
     } catch (PHPUnit_Framework_MockObject_Exception $e) {
         $e = new PHPUnit_Framework_Warning($e->getMessage());
         $warning = true;
     } 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 (PHPUnit_Framework_Warning $e) {
         $warning = true;
     } catch (PHPUnit_Framework_Exception $e) {
         $error = true;
     } catch (Throwable $e) {
         $e = new PHPUnit_Framework_ExceptionWrapper($e);
         $error = true;
     } catch (Exception $e) {
         $e = new PHPUnit_Framework_ExceptionWrapper($e);
         $error = true;
     }
     $time = PHP_Timer::stop();
     $test->addToAssertionCount(PHPUnit_Framework_Assert::getCount());
     if ($monitorFunctions) {
         $blacklist = new PHPUnit_Util_Blacklist();
         $functions = xdebug_get_monitored_functions();
         xdebug_stop_function_monitor();
         foreach ($functions as $function) {
             if (!$blacklist->isBlacklisted($function['filename'])) {
                 $this->addFailure($test, new PHPUnit_Framework_RiskyTestError(sprintf('%s() used in %s:%s', $function['function'], $function['filename'], $function['lineno'])), $time);
             }
         }
     }
     if ($this->beStrictAboutTestsThatDoNotTestAnything && $test->getNumAssertions() == 0) {
         $risky = true;
     }
     if ($collectCodeCoverage) {
         $append = !$risky && !$incomplete && !$skipped;
         $linesToBeCovered = [];
         $linesToBeUsed = [];
         if ($append && $test instanceof PHPUnit_Framework_TestCase) {
             try {
                 $linesToBeCovered = PHPUnit_Util_Test::getLinesToBeCovered(get_class($test), $test->getName(false));
                 $linesToBeUsed = PHPUnit_Util_Test::getLinesToBeUsed(get_class($test), $test->getName(false));
             } catch (PHPUnit_Framework_InvalidCoversTargetException $cce) {
                 $this->addWarning($test, new PHPUnit_Framework_Warning($cce->getMessage()), $time);
             }
         }
         try {
             $this->codeCoverage->stop($append, $linesToBeCovered, $linesToBeUsed);
         } catch (PHP_CodeCoverage_UnintentionallyCoveredCodeException $cce) {
             if (!$test->isMedium() && !$test->isLarge()) {
                 $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 (PHP_CodeCoverage_CoveredCodeNotExecutedException $cce) {
             $this->addFailure($test, new PHPUnit_Framework_CoveredCodeNotExecutedException('This test did not execute all the code that is listed as code to be covered:' . PHP_EOL . $cce->getMessage()), $time);
         } catch (PHP_CodeCoverage_MissingCoversAnnotationException $cce) {
             $this->addFailure($test, new PHPUnit_Framework_MissingCoversAnnotationException('This test does not have a @covers annotation but is expected to have one'), $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 ($warning === true) {
         $this->addWarning($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);
     } elseif ($this->beStrictAboutTodoAnnotatedTests && $test instanceof PHPUnit_Framework_TestCase) {
         $annotations = $test->getAnnotations();
         if (isset($annotations['method']['todo'])) {
             $this->addFailure($test, new PHPUnit_Framework_RiskyTestError('Test method is annotated with @todo'), $time);
         }
     }
     $this->endTest($test, $time);
 }
示例#3
0
 /**
  * A warning occurred.
  *
  * @param \PHPUnit_Framework_Test  $test
  * @param \PHPUnit_Framework_Warning $e
  * @param float $time
  * @return void
  */
 public function addWarning(\PHPUnit_Framework_Test $test, \PHPUnit_Framework_Warning $e, $time)
 {
     echo "\t[";
     echo Colorize::text($e->getMessage(), "red", null, true);
     echo "] ";
 }