Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. PhptTestCase.php

function PhptTestCase::run

Runs a test and collects its result in a TestResult instance.

@noinspection RepetitiveMethodCallsInspection

Throws

\PHPUnit\Framework\Exception

\SebastianBergmann\Template\InvalidArgumentException

Exception

InvalidArgumentException

NoPreviousThrowableException

ReflectionException

StaticAnalysisCacheNotConfiguredException

TestIdMissingException

UnintentionallyCoveredCodeException

Overrides Test::run

File

vendor/phpunit/phpunit/src/Runner/PhptTestCase.php, line 123

Class

PhptTestCase
@no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit

Namespace

PHPUnit\Runner

Code

public function run() : void {
    $emitter = EventFacade::emitter();
    $emitter->testPreparationStarted($this->valueObjectForEvents());
    try {
        $sections = $this->parse();
    } catch (Exception $e) {
        $emitter->testPrepared($this->valueObjectForEvents());
        $emitter->testErrored($this->valueObjectForEvents(), ThrowableBuilder::from($e));
        $emitter->testFinished($this->valueObjectForEvents(), 0);
        return;
    }
    $code = $this->render($sections['FILE']);
    $xfail = false;
    $settings = $this->parseIniSection($this->settings(CodeCoverage::instance()->isActive()));
    $emitter->testPrepared($this->valueObjectForEvents());
    if (isset($sections['INI'])) {
        $settings = $this->parseIniSection($sections['INI'], $settings);
    }
    if (isset($sections['ENV'])) {
        $env = $this->parseEnvSection($sections['ENV']);
        $this->phpUtil
            ->setEnv($env);
    }
    $this->phpUtil
        ->setUseStderrRedirection(true);
    if ($this->shouldTestBeSkipped($sections, $settings)) {
        return;
    }
    if (isset($sections['XFAIL'])) {
        $xfail = trim($sections['XFAIL']);
    }
    if (isset($sections['STDIN'])) {
        $this->phpUtil
            ->setStdin($sections['STDIN']);
    }
    if (isset($sections['ARGS'])) {
        $this->phpUtil
            ->setArgs($sections['ARGS']);
    }
    if (CodeCoverage::instance()->isActive()) {
        $codeCoverageCacheDirectory = null;
        if (CodeCoverage::instance()->codeCoverage()
            ->cachesStaticAnalysis()) {
            $codeCoverageCacheDirectory = CodeCoverage::instance()->codeCoverage()
                ->cacheDirectory();
        }
        $this->renderForCoverage($code, CodeCoverage::instance()->codeCoverage()
            ->collectsBranchAndPathCoverage(), $codeCoverageCacheDirectory);
    }
    $jobResult = $this->phpUtil
        ->runJob($code, $this->stringifyIni($settings));
    $this->output = $jobResult['stdout'] ?? '';
    if (CodeCoverage::instance()->isActive()) {
        $coverage = $this->cleanupForCoverage();
        CodeCoverage::instance()->codeCoverage()
            ->start($this->filename, TestSize::large());
        CodeCoverage::instance()->codeCoverage()
            ->append($coverage, $this->filename, true, TestStatus::unknown());
    }
    $passed = true;
    try {
        $this->assertPhptExpectation($sections, $this->output);
    } catch (AssertionFailedError $e) {
        $failure = $e;
        if ($xfail !== false) {
            $failure = new IncompleteTestError($xfail, 0, $e);
        }
        elseif ($e instanceof ExpectationFailedException) {
            $comparisonFailure = $e->getComparisonFailure();
            if ($comparisonFailure) {
                $diff = $comparisonFailure->getDiff();
            }
            else {
                $diff = $e->getMessage();
            }
            $hint = $this->getLocationHintFromDiff($diff, $sections);
            $trace = array_merge($hint, debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS));
            $failure = new PhptAssertionFailedError($e->getMessage(), 0, (string) $trace[0]['file'], (int) $trace[0]['line'], $trace, $comparisonFailure ? $diff : '');
        }
        if ($failure instanceof IncompleteTestError) {
            $emitter->testMarkedAsIncomplete($this->valueObjectForEvents(), ThrowableBuilder::from($failure));
        }
        else {
            $emitter->testFailed($this->valueObjectForEvents(), ThrowableBuilder::from($failure), null);
        }
        $passed = false;
    } catch (Throwable $t) {
        $emitter->testErrored($this->valueObjectForEvents(), ThrowableBuilder::from($t));
        $passed = false;
    }
    if ($passed) {
        $emitter->testPassed($this->valueObjectForEvents());
    }
    $this->runClean($sections, CodeCoverage::instance()->isActive());
    $emitter->testFinished($this->valueObjectForEvents(), 1);
}
RSS feed
Powered by Drupal