Skip to content

Commit 4f7a13d

Browse files
Implement --force-coverage-cache CLI option
1 parent 9535c8c commit 4f7a13d

File tree

14 files changed

+72
-4
lines changed

14 files changed

+72
-4
lines changed

ChangeLog-9.4.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ All notable changes of the PHPUnit 9.4 release series are documented in this fil
88

99
* [#4464](https://github.com/sebastianbergmann/phpunit/issues/4464): Filter based on covered (`@covers`) / used (`@uses`) units of code
1010
* [#4467](https://github.com/sebastianbergmann/phpunit/issues/4467): Convenient custom comparison of objects
11+
* Added `--force-coverage-cache` CLI option for configuring the code coverage static analysis cache to not check whether cache files exist and are up-to-date (do not use this if you do not need it or cannot guarantee that the cache files exist and are up-to-date)
1112

1213
### Changed
1314

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"issues": "https://github.com/sebastianbergmann/phpunit/issues"
2121
},
2222
"prefer-stable": true,
23+
"minimum-stability": "dev",
2324
"require": {
2425
"php": ">=7.3",
2526
"ext-dom": "*",
@@ -33,7 +34,7 @@
3334
"phar-io/manifest": "^2.0.1",
3435
"phar-io/version": "^3.0.2",
3536
"phpspec/prophecy": "^1.11.1",
36-
"phpunit/php-code-coverage": "^9.1.11",
37+
"phpunit/php-code-coverage": "^9.2",
3738
"phpunit/php-file-iterator": "^3.0.4",
3839
"phpunit/php-invoker": "^3.1",
3940
"phpunit/php-text-template": "^2.0.2",

src/Framework/TestCase.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,7 @@ public function run(TestResult $result = null): TestResult
811811
$codeCoverage = $result->getCodeCoverage();
812812
$codeCoverageFilter = null;
813813
$cachesStaticAnalysis = 'false';
814+
$forcesStaticAnalysisCache = 'false';
814815
$codeCoverageCacheDirectory = null;
815816
$driverMethod = 'forLineCoverage';
816817

@@ -824,6 +825,10 @@ public function run(TestResult $result = null): TestResult
824825
if ($codeCoverage->cachesStaticAnalysis()) {
825826
$cachesStaticAnalysis = 'true';
826827
$codeCoverageCacheDirectory = $codeCoverage->cacheDirectory();
828+
829+
if (!$codeCoverage->validatesStaticAnalysisCache()) {
830+
$forcesStaticAnalysisCache = 'true';
831+
}
827832
}
828833
}
829834

@@ -851,6 +856,7 @@ public function run(TestResult $result = null): TestResult
851856
'className' => $class->getName(),
852857
'collectCodeCoverageInformation' => $coverage,
853858
'cachesStaticAnalysis' => $cachesStaticAnalysis,
859+
'forcesStaticAnalysisCache' => $forcesStaticAnalysisCache,
854860
'codeCoverageCacheDirectory' => $codeCoverageCacheDirectory,
855861
'driverMethod' => $driverMethod,
856862
'data' => $data,

src/Runner/PhptTestCase.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,19 +174,24 @@ public function run(TestResult $result = null): TestResult
174174

175175
if ($result->getCollectCodeCoverageInformation()) {
176176
$codeCoverageCacheDirectory = null;
177+
$forcesStaticAnalysisCache = false;
177178
$pathCoverage = false;
178179

179180
$codeCoverage = $result->getCodeCoverage();
180181

181182
if ($codeCoverage) {
182183
if ($codeCoverage->cachesStaticAnalysis()) {
183184
$codeCoverageCacheDirectory = $codeCoverage->cacheDirectory();
185+
186+
if (!$codeCoverage->validatesStaticAnalysisCache()) {
187+
$forcesStaticAnalysisCache = true;
188+
}
184189
}
185190

186191
$pathCoverage = $codeCoverage->collectsBranchAndPathCoverage();
187192
}
188193

189-
$this->renderForCoverage($code, $pathCoverage, $codeCoverageCacheDirectory);
194+
$this->renderForCoverage($code, $pathCoverage, $codeCoverageCacheDirectory, $forcesStaticAnalysisCache);
190195
}
191196

192197
$timer = new Timer;
@@ -595,7 +600,7 @@ private function getCoverageFiles(): array
595600
];
596601
}
597602

598-
private function renderForCoverage(string &$job, bool $pathCoverage, ?string $codeCoverageCacheDirectory): void
603+
private function renderForCoverage(string &$job, bool $pathCoverage, ?string $codeCoverageCacheDirectory, bool $forcesStaticAnalysisCache): void
599604
{
600605
$files = $this->getCoverageFiles();
601606

@@ -639,6 +644,7 @@ private function renderForCoverage(string &$job, bool $pathCoverage, ?string $co
639644
'coverageFile' => $files['coverage'],
640645
'driverMethod' => $pathCoverage ? 'forLineAndPathCoverage' : 'forLineCoverage',
641646
'codeCoverageCacheDirectory' => $codeCoverageCacheDirectory,
647+
'forcesStaticAnalysisCache' => $forcesStaticAnalysisCache ? 'true' : 'false',
642648
]
643649
);
644650

src/TextUI/CliArguments/Builder.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ final class Builder
4141
'configuration=',
4242
'coverage-cache=',
4343
'warm-coverage-cache',
44+
'force-coverage-cache',
4445
'coverage-filter=',
4546
'coverage-clover=',
4647
'coverage-crap4j=',
@@ -152,6 +153,7 @@ public function fromParameters(array $parameters, array $additionalLongOptions):
152153
$configuration = null;
153154
$coverageCacheDirectory = null;
154155
$warmCoverageCache = null;
156+
$forceCoverageCache = null;
155157
$coverageFilter = null;
156158
$coverageClover = null;
157159
$coverageCrap4J = null;
@@ -284,6 +286,11 @@ public function fromParameters(array $parameters, array $additionalLongOptions):
284286

285287
break;
286288

289+
case '--force-coverage-cache':
290+
$forceCoverageCache = true;
291+
292+
break;
293+
287294
case '--coverage-clover':
288295
$coverageClover = $option[1];
289296

@@ -806,6 +813,7 @@ public function fromParameters(array $parameters, array $additionalLongOptions):
806813
$pathCoverage,
807814
$coverageCacheDirectory,
808815
$warmCoverageCache,
816+
$forceCoverageCache,
809817
$debug,
810818
$defaultTimeLimit,
811819
$disableCodeCoverageIgnore,

src/TextUI/CliArguments/Configuration.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ final class Configuration
142142
*/
143143
private $warmCoverageCache;
144144

145+
/**
146+
* @var ?bool
147+
*/
148+
private $forceCoverageCache;
149+
145150
/**
146151
* @var ?bool
147152
*/
@@ -470,7 +475,7 @@ final class Configuration
470475
/**
471476
* @param null|int|string $columns
472477
*/
473-
public function __construct(?string $argument, ?string $atLeastVersion, ?bool $backupGlobals, ?bool $backupStaticAttributes, ?bool $beStrictAboutChangesToGlobalState, ?bool $beStrictAboutResourceUsageDuringSmallTests, ?string $bootstrap, ?bool $cacheResult, ?string $cacheResultFile, ?bool $checkVersion, ?string $colors, $columns, ?string $configuration, ?string $coverageClover, ?string $coverageCrap4J, ?string $coverageHtml, ?string $coveragePhp, ?string $coverageText, ?bool $coverageTextShowUncoveredFiles, ?bool $coverageTextShowOnlySummary, ?string $coverageXml, ?bool $pathCoverage, ?string $coverageCacheDirectory, ?bool $warmCoverageCache, ?bool $debug, ?int $defaultTimeLimit, ?bool $disableCodeCoverageIgnore, ?bool $disallowTestOutput, ?bool $disallowTodoAnnotatedTests, ?bool $enforceTimeLimit, ?array $excludeGroups, ?int $executionOrder, ?int $executionOrderDefects, ?array $extensions, ?array $unavailableExtensions, ?bool $failOnEmptyTestSuite, ?bool $failOnIncomplete, ?bool $failOnRisky, ?bool $failOnSkipped, ?bool $failOnWarning, ?string $filter, ?bool $generateConfiguration, ?bool $migrateConfiguration, ?array $groups, ?array $testsCovering, ?array $testsUsing, ?bool $help, ?string $includePath, ?array $iniSettings, ?string $junitLogfile, ?bool $listGroups, ?bool $listSuites, ?bool $listTests, ?string $listTestsXml, ?string $loader, ?bool $noCoverage, ?bool $noExtensions, ?bool $noInteraction, ?bool $noLogging, ?string $printer, ?bool $processIsolation, ?int $randomOrderSeed, ?int $repeat, ?bool $reportUselessTests, ?bool $resolveDependencies, ?bool $reverseList, ?bool $stderr, ?bool $strictCoverage, ?bool $stopOnDefect, ?bool $stopOnError, ?bool $stopOnFailure, ?bool $stopOnIncomplete, ?bool $stopOnRisky, ?bool $stopOnSkipped, ?bool $stopOnWarning, ?string $teamcityLogfile, ?array $testdoxExcludeGroups, ?array $testdoxGroups, ?string $testdoxHtmlFile, ?string $testdoxTextFile, ?string $testdoxXmlFile, ?array $testSuffixes, ?string $testSuite, array $unrecognizedOptions, ?string $unrecognizedOrderBy, ?bool $useDefaultConfiguration, ?bool $verbose, ?bool $version, ?array $coverageFilter, ?string $xdebugFilterFile)
478+
public function __construct(?string $argument, ?string $atLeastVersion, ?bool $backupGlobals, ?bool $backupStaticAttributes, ?bool $beStrictAboutChangesToGlobalState, ?bool $beStrictAboutResourceUsageDuringSmallTests, ?string $bootstrap, ?bool $cacheResult, ?string $cacheResultFile, ?bool $checkVersion, ?string $colors, $columns, ?string $configuration, ?string $coverageClover, ?string $coverageCrap4J, ?string $coverageHtml, ?string $coveragePhp, ?string $coverageText, ?bool $coverageTextShowUncoveredFiles, ?bool $coverageTextShowOnlySummary, ?string $coverageXml, ?bool $pathCoverage, ?string $coverageCacheDirectory, ?bool $warmCoverageCache, ?bool $forceCoverageCache, ?bool $debug, ?int $defaultTimeLimit, ?bool $disableCodeCoverageIgnore, ?bool $disallowTestOutput, ?bool $disallowTodoAnnotatedTests, ?bool $enforceTimeLimit, ?array $excludeGroups, ?int $executionOrder, ?int $executionOrderDefects, ?array $extensions, ?array $unavailableExtensions, ?bool $failOnEmptyTestSuite, ?bool $failOnIncomplete, ?bool $failOnRisky, ?bool $failOnSkipped, ?bool $failOnWarning, ?string $filter, ?bool $generateConfiguration, ?bool $migrateConfiguration, ?array $groups, ?array $testsCovering, ?array $testsUsing, ?bool $help, ?string $includePath, ?array $iniSettings, ?string $junitLogfile, ?bool $listGroups, ?bool $listSuites, ?bool $listTests, ?string $listTestsXml, ?string $loader, ?bool $noCoverage, ?bool $noExtensions, ?bool $noInteraction, ?bool $noLogging, ?string $printer, ?bool $processIsolation, ?int $randomOrderSeed, ?int $repeat, ?bool $reportUselessTests, ?bool $resolveDependencies, ?bool $reverseList, ?bool $stderr, ?bool $strictCoverage, ?bool $stopOnDefect, ?bool $stopOnError, ?bool $stopOnFailure, ?bool $stopOnIncomplete, ?bool $stopOnRisky, ?bool $stopOnSkipped, ?bool $stopOnWarning, ?string $teamcityLogfile, ?array $testdoxExcludeGroups, ?array $testdoxGroups, ?string $testdoxHtmlFile, ?string $testdoxTextFile, ?string $testdoxXmlFile, ?array $testSuffixes, ?string $testSuite, array $unrecognizedOptions, ?string $unrecognizedOrderBy, ?bool $useDefaultConfiguration, ?bool $verbose, ?bool $version, ?array $coverageFilter, ?string $xdebugFilterFile)
474479
{
475480
$this->argument = $argument;
476481
$this->atLeastVersion = $atLeastVersion;
@@ -497,6 +502,7 @@ public function __construct(?string $argument, ?string $atLeastVersion, ?bool $b
497502
$this->pathCoverage = $pathCoverage;
498503
$this->coverageCacheDirectory = $coverageCacheDirectory;
499504
$this->warmCoverageCache = $warmCoverageCache;
505+
$this->forceCoverageCache = $forceCoverageCache;
500506
$this->debug = $debug;
501507
$this->defaultTimeLimit = $defaultTimeLimit;
502508
$this->disableCodeCoverageIgnore = $disableCodeCoverageIgnore;
@@ -989,6 +995,23 @@ public function warmCoverageCache(): bool
989995
return $this->warmCoverageCache;
990996
}
991997

998+
public function hasForceCoverageCache(): bool
999+
{
1000+
return $this->forceCoverageCache !== null;
1001+
}
1002+
1003+
/**
1004+
* @throws Exception
1005+
*/
1006+
public function forceCoverageCache(): bool
1007+
{
1008+
if ($this->forceCoverageCache === null) {
1009+
throw new Exception;
1010+
}
1011+
1012+
return $this->forceCoverageCache;
1013+
}
1014+
9921015
public function hasDebug(): bool
9931016
{
9941017
return $this->debug !== null;

src/TextUI/CliArguments/Mapper.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ public function mapToLegacyArray(Configuration $arguments): array
6464
$result['warmCoverageCache'] = $arguments->warmCoverageCache();
6565
}
6666

67+
if ($arguments->hasForceCoverageCache()) {
68+
$result['forceCoverageCache'] = $arguments->forceCoverageCache();
69+
}
70+
6771
if ($arguments->hasCoverageClover()) {
6872
$result['coverageClover'] = $arguments->coverageClover();
6973
}

src/TextUI/Help.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ final class Help
4343
['arg' => '--coverage-xml <dir>', 'desc' => 'Generate code coverage report in PHPUnit XML format'],
4444
['arg' => '--coverage-cache <dir>', 'desc' => 'Cache static analysis results'],
4545
['arg' => '--warm-coverage-cache', 'desc' => 'Warm static analysis cache'],
46+
['arg' => '--force-coverage-cache', 'desc' => 'Do not validate static analysis cache'],
4647
['arg' => '--coverage-filter <dir>', 'desc' => 'Include <dir> in code coverage analysis'],
4748
['arg' => '--path-coverage', 'desc' => 'Perform path coverage analysis'],
4849
['arg' => '--disable-coverage-ignore', 'desc' => 'Disable annotations for ignoring code coverage'],

src/TextUI/TestRunner.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,10 @@ public function run(TestSuite $suite, array $arguments = [], array $warnings = [
468468
$codeCoverage->cacheStaticAnalysis($arguments['coverageCacheDirectory']);
469469
}
470470

471+
if (isset($arguments['forceCoverageCache']) && $arguments['forceCoverageCache'] === true && $codeCoverage->cachesStaticAnalysis()) {
472+
$codeCoverage->doNotValidateStaticAnalysisCache();
473+
}
474+
471475
$codeCoverage->excludeSubclassesOfThisClassFromUnintentionallyCoveredCodeCheck(Comparator::class);
472476

473477
if ($arguments['strictCoverage']) {

src/Util/PHP/Template/PhptTestCase.tpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ if (class_exists('SebastianBergmann\CodeCoverage\CodeCoverage')) {
3535

3636
if ({codeCoverageCacheDirectory}) {
3737
$coverage->cacheStaticAnalysis({codeCoverageCacheDirectory});
38+
39+
if ({forcesStaticAnalysisCache}) {
40+
$coverage->doNotValidateStaticAnalysisCache();
41+
}
3842
}
3943

4044
$coverage->start(__FILE__);

0 commit comments

Comments
 (0)