Skip to content

Commit 5816d79

Browse files
Wire up separate branch coverage configuration
1 parent cdaa1b9 commit 5816d79

6 files changed

Lines changed: 79 additions & 21 deletions

File tree

src/Runner/CodeCoverage.php

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ final class CodeCoverage
6262
private ?Timer $timer = null;
6363
private bool $requireCoverageContribution = false;
6464
private bool $lastTestContributedToCoverage = false;
65-
private bool $collectsBranchAndPathCoverage = false;
65+
private bool $collectsBranchCoverage = false;
66+
private bool $collectsPathCoverage = false;
6667

6768
public static function instance(): self
6869
{
@@ -81,7 +82,11 @@ public function init(Configuration $configuration, CodeCoverageFilterRegistry $c
8182
return CodeCoverageInitializationStatus::NOT_REQUESTED;
8283
}
8384

84-
$this->activate($codeCoverageFilterRegistry->get(), $configuration->pathCoverage());
85+
$this->activate(
86+
$codeCoverageFilterRegistry->get(),
87+
$configuration->branchCoverage(),
88+
$configuration->pathCoverage(),
89+
);
8590

8691
if (!$this->isActive()) {
8792
return CodeCoverageInitializationStatus::FAILED;
@@ -274,15 +279,21 @@ public function lastTestContributedToCoverage(): bool
274279

275280
public function deactivate(): void
276281
{
277-
$this->driver = null;
278-
$this->codeCoverage = null;
279-
$this->test = null;
280-
$this->collectsBranchAndPathCoverage = false;
282+
$this->driver = null;
283+
$this->codeCoverage = null;
284+
$this->test = null;
285+
$this->collectsBranchCoverage = false;
286+
$this->collectsPathCoverage = false;
281287
}
282288

283-
public function collectsBranchAndPathCoverage(): bool
289+
public function collectsBranchCoverage(): bool
284290
{
285-
return $this->collectsBranchAndPathCoverage;
291+
return $this->collectsBranchCoverage;
292+
}
293+
294+
public function collectsPathCoverage(): bool
295+
{
296+
return $this->collectsPathCoverage;
286297
}
287298

288299
public function generateReports(Printer $printer, Configuration $configuration): void
@@ -486,13 +497,26 @@ public function warnIfFilterIsNotConfigured(CodeCoverageFilterRegistry $codeCove
486497
$this->deactivate();
487498
}
488499

489-
private function activate(Filter $filter, bool $pathCoverage): void
500+
private function activate(Filter $filter, bool $branchCoverage, bool $pathCoverage): void
490501
{
491502
try {
503+
$granularity = Granularity::Line;
504+
505+
if ($branchCoverage) {
506+
$granularity = Granularity::LineAndBranch;
507+
}
508+
492509
if ($pathCoverage) {
493510
$granularity = Granularity::LineBranchAndPath;
494-
} else {
495-
$granularity = Granularity::Line;
511+
}
512+
513+
/**
514+
* @todo This needs to be removed once code coverage drivers are supported that can collect branch coverage without path coverage
515+
*/
516+
if ($branchCoverage || $pathCoverage) {
517+
$branchCoverage = true;
518+
$pathCoverage = true;
519+
$granularity = Granularity::LineBranchAndPath;
496520
}
497521

498522
$this->driver = (new Selector)->select($filter, $granularity);
@@ -502,7 +526,8 @@ private function activate(Filter $filter, bool $pathCoverage): void
502526
$filter,
503527
);
504528

505-
$this->collectsBranchAndPathCoverage = $pathCoverage;
529+
$this->collectsBranchCoverage = $branchCoverage;
530+
$this->collectsPathCoverage = $pathCoverage;
506531
} catch (CodeCoverageException $e) {
507532
EventFacade::emitter()->testRunnerTriggeredPhpunitWarning(
508533
$e->getMessage(),

src/Runner/Phpt/CodeCoverageBootstrapper.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,18 @@
2222
/**
2323
* @param ?non-empty-string $codeCoverageCacheDirectory
2424
*/
25-
public static function bootstrap(?string $codeCoverageCacheDirectory, bool $pathCoverage): CodeCoverage
25+
public static function bootstrap(?string $codeCoverageCacheDirectory, bool $branchCoverage, bool $pathCoverage): CodeCoverage
2626
{
2727
$filter = new Filter;
2828

29+
$granularity = Granularity::Line;
30+
31+
if ($branchCoverage) {
32+
$granularity = Granularity::LineAndBranch;
33+
}
34+
2935
if ($pathCoverage) {
3036
$granularity = Granularity::LineBranchAndPath;
31-
} else {
32-
$granularity = Granularity::Line;
3337
}
3438

3539
$coverage = new CodeCoverage(

src/Runner/Phpt/Renderer.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function render(string $phptFile, string $code): string
5858
*
5959
* @throws InvalidArgumentException
6060
*/
61-
public function renderForCoverage(string &$job, bool $pathCoverage, ?string $codeCoverageCacheDirectory, string $bootstrap, array $files): void
61+
public function renderForCoverage(string &$job, bool $branchCoverage, bool $pathCoverage, ?string $codeCoverageCacheDirectory, string $bootstrap, array $files): void
6262
{
6363
$template = new Template(
6464
__DIR__ . '/templates/phpt.tpl',
@@ -84,6 +84,12 @@ public function renderForCoverage(string &$job, bool $pathCoverage, ?string $cod
8484
$codeCoverageCacheDirectory = "'" . $codeCoverageCacheDirectory . "'";
8585
}
8686

87+
if ($branchCoverage) {
88+
$branchCoverageValue = 'true';
89+
} else {
90+
$branchCoverageValue = 'false';
91+
}
92+
8793
if ($pathCoverage) {
8894
$pathCoverageValue = 'true';
8995
} else {
@@ -97,6 +103,7 @@ public function renderForCoverage(string &$job, bool $pathCoverage, ?string $cod
97103
'phar' => $phar,
98104
'job' => $files['job'],
99105
'coverageFile' => $files['coverage'],
106+
'branchCoverage' => $branchCoverageValue,
100107
'pathCoverage' => $pathCoverageValue,
101108
'codeCoverageCacheDirectory' => $codeCoverageCacheDirectory,
102109
],

src/Runner/Phpt/TestCase.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,8 @@ public function run(): void
195195

196196
(new Renderer)->renderForCoverage(
197197
$code,
198-
CodeCoverage::instance()->collectsBranchAndPathCoverage(),
198+
CodeCoverage::instance()->collectsBranchCoverage(),
199+
CodeCoverage::instance()->collectsPathCoverage(),
199200
$codeCoverageCacheDirectory,
200201
$bootstrap,
201202
$this->coverageFiles(),

src/Runner/Phpt/templates/phpt.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ if ('{bootstrap}' !== '') {
2020
require_once '{bootstrap}';
2121
}
2222

23-
$__phpunit_coverage = CodeCoverageBootstrapper::bootstrap({codeCoverageCacheDirectory}, {pathCoverage});
23+
$__phpunit_coverage = CodeCoverageBootstrapper::bootstrap({codeCoverageCacheDirectory}, {branchCoverage}, {pathCoverage});
2424

2525
if ($__phpunit_coverage !== null) {
2626
$__phpunit_coverage->start(__FILE__);

tests/unit/Runner/Phpt/RendererTest.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,30 +58,49 @@ public function testRenderForCoverageWritesJobFileAndUpdatesJobVariable(): void
5858
(new Renderer)->renderForCoverage(
5959
$job,
6060
false,
61+
false,
6162
null,
6263
'',
6364
$files,
6465
);
6566

6667
$this->assertStringEqualsFile($files['job'], '<?php echo 1;');
67-
$this->assertStringContainsString('CodeCoverageBootstrapper::bootstrap(null, false)', $job);
68+
$this->assertStringContainsString('CodeCoverageBootstrapper::bootstrap(null, false, false)', $job);
6869
$this->assertStringContainsString($files['coverage'], $job);
6970
}
7071

72+
public function testRenderForCoverageWithBranchCoverage(): void
73+
{
74+
$files = $this->createTempFiles();
75+
$job = '<?php echo 1;';
76+
77+
(new Renderer)->renderForCoverage(
78+
$job,
79+
true,
80+
false,
81+
null,
82+
'',
83+
$files,
84+
);
85+
86+
$this->assertStringContainsString('CodeCoverageBootstrapper::bootstrap(null, true, false)', $job);
87+
}
88+
7189
public function testRenderForCoverageWithPathCoverage(): void
7290
{
7391
$files = $this->createTempFiles();
7492
$job = '<?php echo 1;';
7593

7694
(new Renderer)->renderForCoverage(
7795
$job,
96+
false,
7897
true,
7998
null,
8099
'',
81100
$files,
82101
);
83102

84-
$this->assertStringContainsString('CodeCoverageBootstrapper::bootstrap(null, true)', $job);
103+
$this->assertStringContainsString('CodeCoverageBootstrapper::bootstrap(null, false, true)', $job);
85104
}
86105

87106
public function testRenderForCoverageWithCacheDirectory(): void
@@ -92,12 +111,13 @@ public function testRenderForCoverageWithCacheDirectory(): void
92111
(new Renderer)->renderForCoverage(
93112
$job,
94113
false,
114+
false,
95115
'/tmp/cache',
96116
'',
97117
$files,
98118
);
99119

100-
$this->assertStringContainsString("CodeCoverageBootstrapper::bootstrap('/tmp/cache', false)", $job);
120+
$this->assertStringContainsString("CodeCoverageBootstrapper::bootstrap('/tmp/cache', false, false)", $job);
101121
}
102122

103123
public function testRenderForCoverageWithBootstrap(): void
@@ -108,6 +128,7 @@ public function testRenderForCoverageWithBootstrap(): void
108128
(new Renderer)->renderForCoverage(
109129
$job,
110130
false,
131+
false,
111132
null,
112133
'/path/to/bootstrap.php',
113134
$files,

0 commit comments

Comments
 (0)