Skip to content

Commit d2ad210

Browse files
dvdougsebastianbergmann
authored andcommitted
Make @Covers annotations work on branch/path coverage
1 parent bfe4a52 commit d2ad210

File tree

2 files changed

+46
-24
lines changed

2 files changed

+46
-24
lines changed

src/RawCodeCoverageData.php

+16
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,22 @@ public function keepCoverageDataOnlyForLines(string $filename, array $lines): vo
209209
$this->lineCoverage[$filename],
210210
\array_flip($lines)
211211
);
212+
213+
if (isset($this->functionCoverage[$filename])) {
214+
foreach ($this->functionCoverage[$filename] as $functionName => $functionData) {
215+
foreach ($functionData['branches'] as $branchId => $branch) {
216+
if (\count(\array_diff(\range($branch['line_start'], $branch['line_end']), $lines)) > 0) {
217+
unset($this->functionCoverage[$filename][$functionName]['branches'][$branchId]);
218+
219+
foreach ($functionData['paths'] as $pathId => $path) {
220+
if (\in_array($branchId, $path['path'], true)) {
221+
unset($this->functionCoverage[$filename][$functionName]['paths'][$pathId]);
222+
}
223+
}
224+
}
225+
}
226+
}
227+
}
212228
}
213229

214230
/**

tests/TestCase.php

+30-24
Original file line numberDiff line numberDiff line change
@@ -908,31 +908,41 @@ protected function getPathCoverageForBankAccount(): CodeCoverage
908908
);
909909

910910
$coverage->stop(
911-
true
911+
true,
912+
[TEST_FILES_PATH . 'BankAccount.php' => \range(6, 9)]
912913
);
913914

914915
$coverage->start(
915916
new \BankAccountTest('testBalanceCannotBecomeNegative')
916917
);
917918

918919
$coverage->stop(
919-
true
920+
true,
921+
[TEST_FILES_PATH . 'BankAccount.php' => \range(27, 32)]
920922
);
921923

922924
$coverage->start(
923925
new \BankAccountTest('testBalanceCannotBecomeNegative2')
924926
);
925927

926928
$coverage->stop(
927-
true
929+
true,
930+
[TEST_FILES_PATH . 'BankAccount.php' => \range(20, 25)]
928931
);
929932

930933
$coverage->start(
931934
new \BankAccountTest('testDepositWithdrawMoney')
932935
);
933936

934937
$coverage->stop(
935-
true
938+
true,
939+
[
940+
TEST_FILES_PATH . 'BankAccount.php' => \array_merge(
941+
\range(6, 9),
942+
\range(20, 25),
943+
\range(27, 32)
944+
),
945+
]
936946
);
937947

938948
return $coverage;
@@ -1233,15 +1243,17 @@ protected function getPathCoverageForBankAccountForFirstTwoTests(): CodeCoverage
12331243
);
12341244

12351245
$coverage->stop(
1236-
true
1246+
true,
1247+
[TEST_FILES_PATH . 'BankAccount.php' => \range(6, 9)]
12371248
);
12381249

12391250
$coverage->start(
12401251
new \BankAccountTest('testBalanceCannotBecomeNegative')
12411252
);
12421253

12431254
$coverage->stop(
1244-
true
1255+
true,
1256+
[TEST_FILES_PATH . 'BankAccount.php' => \range(27, 32)]
12451257
);
12461258

12471259
return $coverage;
@@ -1270,15 +1282,23 @@ protected function getPathCoverageForBankAccountForLastTwoTests(): CodeCoverage
12701282
);
12711283

12721284
$coverage->stop(
1273-
true
1285+
true,
1286+
[TEST_FILES_PATH . 'BankAccount.php' => \range(20, 25)]
12741287
);
12751288

12761289
$coverage->start(
12771290
new \BankAccountTest('testDepositWithdrawMoney')
12781291
);
12791292

12801293
$coverage->stop(
1281-
true
1294+
true,
1295+
[
1296+
TEST_FILES_PATH . 'BankAccount.php' => \array_merge(
1297+
\range(6, 9),
1298+
\range(20, 25),
1299+
\range(27, 32)
1300+
),
1301+
]
12821302
);
12831303

12841304
return $coverage;
@@ -1326,9 +1346,7 @@ protected function getExpectedPathCoverageDataArrayForBankAccount(): array
13261346
'line_end' => 9,
13271347
'hit' => [
13281348
0 => 'BankAccountTest::testBalanceIsInitiallyZero',
1329-
1 => 'BankAccountTest::testBalanceCannotBecomeNegative',
1330-
2 => 'BankAccountTest::testBalanceCannotBecomeNegative2',
1331-
3 => 'BankAccountTest::testDepositWithdrawMoney',
1349+
1 => 'BankAccountTest::testDepositWithdrawMoney',
13321350
],
13331351
'out' => [
13341352
],
@@ -1343,9 +1361,7 @@ protected function getExpectedPathCoverageDataArrayForBankAccount(): array
13431361
],
13441362
'hit' => [
13451363
0 => 'BankAccountTest::testBalanceIsInitiallyZero',
1346-
1 => 'BankAccountTest::testBalanceCannotBecomeNegative',
1347-
2 => 'BankAccountTest::testBalanceCannotBecomeNegative2',
1348-
3 => 'BankAccountTest::testDepositWithdrawMoney',
1364+
1 => 'BankAccountTest::testDepositWithdrawMoney',
13491365
],
13501366
],
13511367
],
@@ -1414,9 +1430,6 @@ protected function getExpectedPathCoverageDataArrayForBankAccount(): array
14141430
'line_start' => 11,
14151431
'line_end' => 13,
14161432
'hit' => [
1417-
0 => 'BankAccountTest::testBalanceCannotBecomeNegative',
1418-
1 => 'BankAccountTest::testBalanceCannotBecomeNegative2',
1419-
2 => 'BankAccountTest::testDepositWithdrawMoney',
14201433
],
14211434
'out' => [
14221435
0 => 5,
@@ -1433,7 +1446,6 @@ protected function getExpectedPathCoverageDataArrayForBankAccount(): array
14331446
'line_start' => 14,
14341447
'line_end' => 14,
14351448
'hit' => [
1436-
0 => 'BankAccountTest::testDepositWithdrawMoney',
14371449
],
14381450
'out' => [
14391451
0 => 13,
@@ -1448,8 +1460,6 @@ protected function getExpectedPathCoverageDataArrayForBankAccount(): array
14481460
'line_start' => 16,
14491461
'line_end' => 16,
14501462
'hit' => [
1451-
0 => 'BankAccountTest::testBalanceCannotBecomeNegative',
1452-
1 => 'BankAccountTest::testBalanceCannotBecomeNegative2',
14531463
],
14541464
'out' => [
14551465
0 => 2147483645,
@@ -1464,7 +1474,6 @@ protected function getExpectedPathCoverageDataArrayForBankAccount(): array
14641474
'line_start' => 18,
14651475
'line_end' => 18,
14661476
'hit' => [
1467-
0 => 'BankAccountTest::testDepositWithdrawMoney',
14681477
],
14691478
'out' => [
14701479
0 => 2147483645,
@@ -1482,7 +1491,6 @@ protected function getExpectedPathCoverageDataArrayForBankAccount(): array
14821491
2 => 13,
14831492
],
14841493
'hit' => [
1485-
0 => 'BankAccountTest::testDepositWithdrawMoney',
14861494
],
14871495
],
14881496
1 => [
@@ -1491,8 +1499,6 @@ protected function getExpectedPathCoverageDataArrayForBankAccount(): array
14911499
1 => 9,
14921500
],
14931501
'hit' => [
1494-
0 => 'BankAccountTest::testBalanceCannotBecomeNegative',
1495-
1 => 'BankAccountTest::testBalanceCannotBecomeNegative2',
14961502
],
14971503
],
14981504
],

0 commit comments

Comments
 (0)