Skip to content

Commit dd3bb4b

Browse files
Use a directory in the operating system's path used for temporary files for caching information from static analysis needed for code coverage when no cache directory is explicitly configured
1 parent b451fe5 commit dd3bb4b

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

ChangeLog-12.1.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ All notable changes of the PHPUnit 12.1 release series are documented in this fi
1212

1313
### Changed
1414

15-
* The static analysis of first-party source files required for the code coverage functionality is now performed before the first test is run, if code coverage processing is requested (via the XML configuration file and/or CLI options) and all first-party source files are configured to be processed (which is the default). This has the same effect as running `phpunit --warm-coverage-cache` before running tests.
15+
* When code coverage processing is requested and no static analysis cache directory has been configured then a cache directory in the operating system's path used for temporary files is automatically created and used
16+
* The static analysis of first-party source files required for the code coverage functionality is now performed before the first test is run, if code coverage processing is requested (via the XML configuration file and/or CLI options), all first-party source files are configured to be processed (which is the default), and a static analysis cache directory is available (either explicitly configured or automatically determined, see above). This has the same effect as running `phpunit --warm-coverage-cache` before running tests.
1617

1718
### Deprecated
1819

src/Runner/CodeCoverage.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
use function assert;
1313
use function file_put_contents;
1414
use function sprintf;
15+
use function sys_get_temp_dir;
1516
use PHPUnit\Event\Facade as EventFacade;
1617
use PHPUnit\Framework\TestCase;
1718
use PHPUnit\TextUI\Configuration\CodeCoverageFilterRegistry;
1819
use PHPUnit\TextUI\Configuration\Configuration;
1920
use PHPUnit\TextUI\Output\Printer;
21+
use PHPUnit\Util\Filesystem;
2022
use SebastianBergmann\CodeCoverage\Driver\Driver;
2123
use SebastianBergmann\CodeCoverage\Driver\Selector;
2224
use SebastianBergmann\CodeCoverage\Exception as CodeCoverageException;
@@ -80,7 +82,17 @@ public function init(Configuration $configuration, CodeCoverageFilterRegistry $c
8082
}
8183

8284
if ($configuration->hasCoverageCacheDirectory()) {
83-
$this->codeCoverage()->cacheStaticAnalysis($configuration->coverageCacheDirectory());
85+
$coverageCacheDirectory = $configuration->coverageCacheDirectory();
86+
} else {
87+
$candidate = sys_get_temp_dir() . '/phpunit-code-coverage-cache';
88+
89+
if (Filesystem::createDirectory($candidate)) {
90+
$coverageCacheDirectory = $candidate;
91+
}
92+
}
93+
94+
if (isset($coverageCacheDirectory)) {
95+
$this->codeCoverage()->cacheStaticAnalysis($coverageCacheDirectory);
8496
}
8597

8698
$this->codeCoverage()->excludeSubclassesOfThisClassFromUnintentionallyCoveredCodeCheck(Comparator::class);
@@ -121,11 +133,11 @@ public function init(Configuration $configuration, CodeCoverageFilterRegistry $c
121133
$this->deactivate();
122134
}
123135

124-
if ($configuration->hasCoverageCacheDirectory() && $configuration->includeUncoveredFiles()) {
136+
if (isset($coverageCacheDirectory) && $configuration->includeUncoveredFiles()) {
125137
EventFacade::emitter()->testRunnerStartedStaticAnalysisForCodeCoverage();
126138

127139
$statistics = (new CacheWarmer)->warmCache(
128-
$configuration->coverageCacheDirectory(),
140+
$coverageCacheDirectory,
129141
!$configuration->disableCodeCoverageIgnore(),
130142
$configuration->ignoreDeprecatedCodeUnitsFromCodeCoverage(),
131143
$codeCoverageFilterRegistry->get(),

tests/end-to-end/event/invalid-coverage-metadata.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ PHPUnit Started (PHPUnit %s using %s)
2121
Test Runner Configured
2222
Event Facade Sealed
2323
Test Suite Loaded (1 test)
24+
Static Analysis for Code Coverage Started
25+
Static Analysis for Code Coverage Finished (0 cache hits, 1 cache misses)
2426
Test Runner Started
2527
Test Suite Sorted
2628
Test Runner Execution Started (1 test)

0 commit comments

Comments
 (0)