getOption(self::COVERAGE_OPTION)) { $this->coverage = true; $originals[] = '--coverage-php'; $originals[] = \Pest\Support\Coverage::getPath(); if (! \Pest\Support\Coverage::isAvailable()) { if (\Pest\Support\Coverage::usingXdebug()) { $this->output->writeln([ '', " ERROR Unable to get coverage using Xdebug. Did you set Xdebug's coverage mode?", '', ]); } else { $this->output->writeln([ '', ' ERROR No code coverage driver is available.', '', ]); } exit(1); } } if ($input->getOption(self::MIN_OPTION) !== null) { /** @var int|float $minOption */ $minOption = $input->getOption(self::MIN_OPTION); $this->coverageMin = (float) $minOption; } if ($input->getOption(self::EXACTLY_OPTION) !== null) { /** @var int|float $exactlyOption */ $exactlyOption = $input->getOption(self::EXACTLY_OPTION); $this->coverageExactly = (float) $exactlyOption; } if ($_SERVER['COLLISION_PRINTER_COMPACT'] ?? false) { $this->compact = true; } return $originals; } /** * {@inheritdoc} */ public function addOutput(int $exitCode): int { if (Parallel::isWorker()) { return $exitCode; } if ($exitCode === 0 && $this->coverage) { if (! \Pest\Support\Coverage::isAvailable()) { $this->output->writeln( "\n ERROR No code coverage driver is available.", ); exit(1); } $coverage = \Pest\Support\Coverage::report($this->output, $this->compact); $exitCode = (int) ($coverage < $this->coverageMin); if ($exitCode === 0 && $this->coverageExactly !== null) { $comparableCoverage = $this->computeComparableCoverage($coverage); $comparableCoverageExactly = $this->computeComparableCoverage($this->coverageExactly); $exitCode = $comparableCoverage === $comparableCoverageExactly ? 0 : 1; if ($exitCode === 1) { $this->output->writeln(sprintf( "\n FAIL Code coverage not exactly %s %%, currently %s %%.", number_format($this->coverageExactly, 1), number_format(floor($coverage * 10) / 10, 1), )); } } elseif ($exitCode === 1) { $this->output->writeln(sprintf( "\n FAIL Code coverage below expected %s %%, currently %s %%.", number_format($this->coverageMin, 1), number_format(floor($coverage * 10) / 10, 1) )); } $this->output->writeln(['']); } return $exitCode; } /** * Computes the comparable coverage to a percentage with one decimal. */ private function computeComparableCoverage(float $coverage): float { return floor($coverage * 10) / 10; } }