Skip to content

Commit

Permalink
CodeCoverage: use non-singleton local instances from PHPUnit 10.0.6 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Slamdunk authored Feb 8, 2023
1 parent 76baf4c commit 20fa234
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 52 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"phpunit/php-code-coverage": "^10.0",
"phpunit/php-file-iterator": "^4.0",
"phpunit/php-timer": "^6.0",
"phpunit/phpunit": "^10.0.5",
"phpunit/phpunit": "^10.0.6",
"sebastian/environment": "^6.0",
"symfony/console": "^6.2.5",
"symfony/process": "^6.2.5"
Expand Down
23 changes: 2 additions & 21 deletions src/Util/PhpstormHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
use function array_search;
use function array_unshift;
use function in_array;
use function strlen;
use function substr_compare;
use function str_ends_with;

/** @internal */
final class PhpstormHelper
Expand Down Expand Up @@ -50,32 +49,14 @@ public static function handleArgvFromPhpstorm(array &$argv, string $paratestBina
private static function getArgvKeyFor(array $argv, string $searchFor): int
{
foreach ($argv as $key => $arg) {
if (self::strEndsWith($arg, $searchFor)) {
if (str_ends_with($arg, $searchFor)) {
return $key;
}
}

throw new RuntimeException("Missing path to '$searchFor'");
}

/**
* Polyfill from PHP 8.0, drop when 7.4 support ends
*/
public static function strEndsWith(string $haystack, string $needle): bool
{
if ($needle === '' || $needle === $haystack) {
return true;
}

if ($haystack === '') {
return false;
}

$needleLength = strlen($needle);

return $needleLength <= strlen($haystack) && substr_compare($haystack, $needle, -$needleLength) === 0;
}

/**
* @param array<int, string> $argv
*
Expand Down
14 changes: 10 additions & 4 deletions src/WrapperRunner/ApplicationForWrapperWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use PHPUnit\Runner\TestSuiteSorter;
use PHPUnit\TestRunner\TestResult\Facade as TestResultFacade;
use PHPUnit\TextUI\Configuration\Builder;
use PHPUnit\TextUI\Configuration\CodeCoverageFilterRegistry;
use PHPUnit\TextUI\Configuration\Configuration;
use PHPUnit\TextUI\Configuration\PhpHandler;
use PHPUnit\TextUI\Output\Default\ProgressPrinter\ProgressPrinter;
Expand All @@ -32,7 +33,11 @@
use function mt_srand;
use function serialize;

/** @internal */
/**
* @internal
*
* @codeCoverageIgnore
*/
final class ApplicationForWrapperWorker
{
private bool $hasBeenBootstrapped = false;
Expand Down Expand Up @@ -109,7 +114,7 @@ private function bootstrap(): void
}
}

CodeCoverage::init($this->configuration);
CodeCoverage::instance()->init($this->configuration, CodeCoverageFilterRegistry::instance());

if ($this->configuration->hasLogfileJunit()) {
new JunitXmlLogger(DefaultPrinter::from($this->configuration->logfileJunit()));
Expand Down Expand Up @@ -145,15 +150,16 @@ public function end(): void
EventFacade::emitter()->testRunnerExecutionFinished();
EventFacade::emitter()->testRunnerFinished();

CodeCoverage::generateReports(new NullPrinter(), $this->configuration);
CodeCoverage::instance()->generateReports(new NullPrinter(), $this->configuration);

$result = TestResultFacade::result();
if (isset($this->testdoxResultCollector)) {
(new TestDoxResultPrinter(DefaultPrinter::from($this->testdoxFile), $this->testdoxColor))->print(
$this->testdoxResultCollector->testMethodsGroupedByClass(),
$result,
);
}

$result = TestResultFacade::result();
file_put_contents($this->testresultFile, serialize($result));

EventFacade::emitter()->applicationFinished(0);
Expand Down
12 changes: 0 additions & 12 deletions src/WrapperRunner/EmptyLogFileException.php

This file was deleted.

9 changes: 7 additions & 2 deletions src/WrapperRunner/SuiteLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHPUnit\Runner\TestSuiteSorter;
use PHPUnit\TextUI\Command\Result;
use PHPUnit\TextUI\Command\WarmCodeCoverageCacheCommand;
use PHPUnit\TextUI\Configuration\CodeCoverageFilterRegistry;
use PHPUnit\TextUI\Configuration\PhpHandler;
use PHPUnit\TextUI\Configuration\TestSuiteBuilder;
use PHPUnit\TextUI\TestSuiteFilterProcessor;
Expand Down Expand Up @@ -39,7 +40,8 @@ final class SuiteLoader

public function __construct(
private readonly Options $options,
OutputInterface $output
OutputInterface $output,
CodeCoverageFilterRegistry $codeCoverageFilterRegistry,
) {
(new PhpHandler())->handle($this->options->configuration->php());

Expand Down Expand Up @@ -79,7 +81,10 @@ public function __construct(
}

ob_start();
$result = (new WarmCodeCoverageCacheCommand($this->options->configuration))->execute();
$result = (new WarmCodeCoverageCacheCommand(
$this->options->configuration,
$codeCoverageFilterRegistry,
))->execute();
$output->write(ob_get_clean());
$output->write($result->output());
if ($result->shellExitCode() !== Result::SUCCESS) {
Expand Down
24 changes: 13 additions & 11 deletions src/WrapperRunner/WrapperRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PHPUnit\Runner\CodeCoverage;
use PHPUnit\TestRunner\TestResult\Facade as TestResultFacade;
use PHPUnit\TestRunner\TestResult\TestResult;
use PHPUnit\TextUI\Configuration\CodeCoverageFilterRegistry;
use PHPUnit\TextUI\ShellExitCodeCalculator;
use PHPUnit\Util\ExcludeList;
use SplFileInfo;
Expand Down Expand Up @@ -58,9 +59,9 @@ final class WrapperRunner implements RunnerInterface
private array $teamcityFiles = [];
/** @var list<SplFileInfo> */
private array $testdoxFiles = [];

/** @var non-empty-string[] */
private readonly array $parameters;
private CodeCoverageFilterRegistry $codeCoverageFilterRegistry;

public function __construct(
private readonly Options $options,
Expand All @@ -84,15 +85,20 @@ public function __construct(

$parameters[] = $wrapper;

$this->parameters = $parameters;
$this->parameters = $parameters;
$this->codeCoverageFilterRegistry = new CodeCoverageFilterRegistry();
}

public function run(): int
{
ExcludeList::addDirectory(dirname(__DIR__));
TestResultFacade::init();
EventFacade::seal();
$suiteLoader = new SuiteLoader($this->options, $this->output);
$suiteLoader = new SuiteLoader(
$this->options,
$this->output,
$this->codeCoverageFilterRegistry,
);
$result = TestResultFacade::result();

$this->pending = $suiteLoader->files;
Expand All @@ -105,11 +111,6 @@ public function run(): int
return $this->complete($result);
}

public function getExitCode(): int
{
return $this->exitcode;
}

private function startWorkers(): void
{
for ($token = 1; $token <= $this->options->processes; ++$token) {
Expand Down Expand Up @@ -300,13 +301,14 @@ protected function generateCodeCoverageReports(): void
return;
}

CodeCoverage::init($this->options->configuration);
$coverageMerger = new CoverageMerger(CodeCoverage::instance());
$coverageManager = new CodeCoverage();
$coverageManager->init($this->options->configuration, $this->codeCoverageFilterRegistry);
$coverageMerger = new CoverageMerger($coverageManager->codeCoverage());
foreach ($this->coverageFiles as $coverageFile) {
$coverageMerger->addCoverageFromFile($coverageFile);
}

CodeCoverage::generateReports(
$coverageManager->generateReports(
$this->printer->printer,
$this->options->configuration,
);
Expand Down
3 changes: 2 additions & 1 deletion test/Unit/WrapperRunner/SuiteLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use ParaTest\Tests\TestBase;
use ParaTest\WrapperRunner\SuiteLoader;
use PHPUnit\TextUI\Configuration\CodeCoverageFilterRegistry;
use Symfony\Component\Console\Output\BufferedOutput;

use function array_shift;
Expand Down Expand Up @@ -59,6 +60,6 @@ private function loadSuite(?string $cwd = null): SuiteLoader
{
$options = $this->createOptionsFromArgv($this->bareOptions, $cwd);

return new SuiteLoader($options, $this->output);
return new SuiteLoader($options, $this->output, new CodeCoverageFilterRegistry());
}
}

0 comments on commit 20fa234

Please sign in to comment.