Skip to content

Commit 708ef6e

Browse files
committed
Fix path coverage
1 parent aaedb4a commit 708ef6e

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
@@ -331,9 +331,9 @@ public function append(array $data, $id = null, $append = true, $linesToBeCovere
331331
continue;
332332
}
333333

334-
foreach ($fileData['lines'] as $line => $flag) {
335-
if ($flag === PHP_CodeCoverage_Driver::LINE_EXECUTED) {
336-
$lineData = &$this->data[$file]['lines'][$line];
334+
foreach ($fileData['lines'] as $function => $functionCoverage) {
335+
if ($functionCoverage === PHP_CodeCoverage_Driver::LINE_EXECUTED) {
336+
$lineData = &$this->data[$file]['lines'][$function];
337337
if ($lineData === null) {
338338
$lineData = [
339339
'pathCovered' => false,
@@ -344,6 +344,22 @@ public function append(array $data, $id = null, $append = true, $linesToBeCovere
344344
}
345345
}
346346
}
347+
348+
foreach ($fileData['functions'] as $function => $functionCoverage) {
349+
foreach ($functionCoverage['branches'] as $branch => $branchCoverage) {
350+
if ($branchCoverage['hit'] === 1){
351+
$this->data[$file]['branches'][$function][$branch]['hit'] = 1;
352+
if (!in_array($id, $this->data[$file]['branches'][$function][$branch]['tests'])) {
353+
$this->data[$file]['branches'][$function][$branch]['tests'][] = $id;
354+
}
355+
}
356+
}
357+
foreach ($functionCoverage['paths'] as $path => $pathCoverage) {
358+
if ($pathCoverage['hit'] === 1 && $this->data[$file]['paths'][$function][$path]['hit'] === 0){
359+
$this->data[$file]['paths'][$function][$path]['hit'] = 1;
360+
}
361+
}
362+
}
347363
}
348364
}
349365

@@ -603,8 +619,8 @@ private function initializeFilesThatAreSeenTheFirstTime(array $data)
603619
$this->data[$file]['branches'][$functionName] = [];
604620
$this->data[$file]['paths'][$functionName] = [];
605621

606-
foreach ($functionData['branches'] as $branch) {
607-
$this->data[$file]['branches'][$functionName][] = [
622+
foreach ($functionData['branches'] as $index => $branch) {
623+
$this->data[$file]['branches'][$functionName][$index] = [
608624
'hit' => $branch['hit'],
609625
'line_start' => $branch['line_start'],
610626
'line_end' => $branch['line_end'],

tests/PHP/CodeCoverageTest.php

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

287+
$expectedData = $this->getExpectedDataArrayForBankAccount();
288+
$expectedData[TEST_FILES_PATH . 'BankAccount.php']['branches']['BankAccount->getBalance'][0]['tests'] = [
289+
'BankAccountTest::testBalanceIsInitiallyZero',
290+
'BankAccountTest::testBalanceCannotBecomeNegative',
291+
];
292+
287293
$this->assertEquals(
288-
$this->getExpectedDataArrayForBankAccount(),
294+
$expectedData,
289295
$coverage->getData()
290296
);
291297
}

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)