Skip to content

Commit 2b0babf

Browse files
committed
Fix path coverage
1 parent f71b52f commit 2b0babf

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

src/CodeCoverage.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -326,9 +326,9 @@ public function append(array $data, $id = null, $append = true, $linesToBeCovere
326326
continue;
327327
}
328328

329-
foreach ($fileData['lines'] as $line => $flag) {
330-
if ($flag === PHP_CodeCoverage_Driver::LINE_EXECUTED) {
331-
$lineData = &$this->data[$file]['lines'][$line];
329+
foreach ($fileData['lines'] as $function => $functionCoverage) {
330+
if ($functionCoverage === PHP_CodeCoverage_Driver::LINE_EXECUTED) {
331+
$lineData = &$this->data[$file]['lines'][$function];
332332
if ($lineData === null) {
333333
$lineData = [
334334
'pathCovered' => false,
@@ -339,6 +339,22 @@ public function append(array $data, $id = null, $append = true, $linesToBeCovere
339339
}
340340
}
341341
}
342+
343+
foreach ($fileData['functions'] as $function => $functionCoverage) {
344+
foreach ($functionCoverage['branches'] as $branch => $branchCoverage) {
345+
if ($branchCoverage['hit'] === 1){
346+
$this->data[$file]['branches'][$function][$branch]['hit'] = 1;
347+
if (!in_array($id, $this->data[$file]['branches'][$function][$branch]['tests'])) {
348+
$this->data[$file]['branches'][$function][$branch]['tests'][] = $id;
349+
}
350+
}
351+
}
352+
foreach ($functionCoverage['paths'] as $path => $pathCoverage) {
353+
if ($pathCoverage['hit'] === 1 && $this->data[$file]['paths'][$function][$path]['hit'] === 0){
354+
$this->data[$file]['paths'][$function][$path]['hit'] = 1;
355+
}
356+
}
357+
}
342358
}
343359
}
344360

@@ -591,8 +607,8 @@ private function initializeFilesThatAreSeenTheFirstTime(array $data)
591607
$this->data[$file]['branches'][$functionName] = [];
592608
$this->data[$file]['paths'][$functionName] = [];
593609

594-
foreach ($functionData['branches'] as $branch) {
595-
$this->data[$file]['branches'][$functionName][] = [
610+
foreach ($functionData['branches'] as $index => $branch) {
611+
$this->data[$file]['branches'][$functionName][$index] = [
596612
'hit' => $branch['hit'],
597613
'line_start' => $branch['line_start'],
598614
'line_end' => $branch['line_end'],

tests/PHP/CodeCoverageTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,14 @@ public function testMerge()
262262
$coverage = $this->getCoverageForBankAccountForFirstTwoTests();
263263
$coverage->merge($this->getCoverageForBankAccountForLastTwoTests());
264264

265+
$expectedData = $this->getExpectedDataArrayForBankAccount();
266+
$expectedData[TEST_FILES_PATH . 'BankAccount.php']['branches']['BankAccount->getBalance'][0]['tests'] = [
267+
'BankAccountTest::testBalanceIsInitiallyZero',
268+
'BankAccountTest::testBalanceCannotBecomeNegative',
269+
];
270+
265271
$this->assertEquals(
266-
$this->getExpectedDataArrayForBankAccount(),
272+
$expectedData,
267273
$coverage->getData()
268274
);
269275
}

tests/TestCase.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,12 @@ protected function getExpectedDataArrayForBankAccount()
461461
0 => [
462462
'line_start' => 6,
463463
'line_end' => 9,
464-
'tests' => [],
464+
'tests' => [
465+
'BankAccountTest::testBalanceIsInitiallyZero',
466+
'BankAccountTest::testBalanceCannotBecomeNegative',
467+
'BankAccountTest::testBalanceCannotBecomeNegative2',
468+
'BankAccountTest::testDepositWithdrawMoney',
469+
],
465470
'hit' => 1,
466471
],
467472
],
@@ -480,19 +485,19 @@ protected function getExpectedDataArrayForBankAccount()
480485
'tests' => [],
481486
'hit' => 0,
482487
],
483-
1 => [
488+
5 => [
484489
'line_start' => 14,
485490
'line_end' => 15,
486491
'tests' => [],
487492
'hit' => 0,
488493
],
489-
2 => [
494+
9 => [
490495
'line_start' => 16,
491496
'line_end' => 16,
492497
'tests' => [],
493498
'hit' => 0,
494499
],
495-
3 => [
500+
16 => [
496501
'line_start' => 18,
497502
'line_end' => 18,
498503
'tests' => [],

0 commit comments

Comments
 (0)