Skip to content

Commit 735ceff

Browse files
committed
Add path coverage to each line
1 parent 113e17b commit 735ceff

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

src/CodeCoverage.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,8 @@ public function append(array $data, $id = null, $append = true, $linesToBeCovere
331331
$lineData = &$this->data[$file]['lines'][$line];
332332
if ($lineData === null) {
333333
$lineData = [
334-
'tests' => [$id],
334+
'pathCovered' => false,
335+
'tests' => [$id],
335336
];
336337
} elseif (!in_array($id, $lineData['tests'])) {
337338
$lineData['tests'][] = $id;
@@ -589,6 +590,7 @@ private function initializeFilesThatAreSeenTheFirstTime(array $data)
589590

590591
foreach ($functionData['branches'] as $branch) {
591592
$this->data[$file]['branches'][$functionName][] = [
593+
'hit' => $branch['hit'],
592594
'line_start' => $branch['line_start'],
593595
'line_end' => $branch['line_end'],
594596
'tests' => []
@@ -606,10 +608,21 @@ private function initializeFilesThatAreSeenTheFirstTime(array $data)
606608
$this->data[$file]['lines'][$lineNumber] = null;
607609
} else {
608610
$this->data[$file]['lines'][$lineNumber] = [
609-
'tests' => [],
611+
'pathCovered' => false,
612+
'tests' => [],
610613
];
611614
}
612615
}
616+
617+
foreach ($this->data[$file]['branches'] as $function) {
618+
foreach ($function as $branch) {
619+
for ($i = $branch['line_start']; $i < $branch['line_end']; $i++) {
620+
if (isset($this->data[$file]['lines'][$i])) {
621+
$this->data[$file]['lines'][$i]['pathCovered'] = (bool) $branch['hit'];
622+
}
623+
}
624+
}
625+
}
613626
}
614627
}
615628
}

tests/TestCase.php

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -392,47 +392,57 @@ protected function getExpectedDataArrayForBankAccount()
392392
TEST_FILES_PATH . 'BankAccount.php' => [
393393
'lines' => [
394394
8 => [
395-
'tests' => [
395+
'pathCovered' => true,
396+
'tests' => [
396397
0 => 'BankAccountTest::testBalanceIsInitiallyZero',
397398
1 => 'BankAccountTest::testDepositWithdrawMoney',
398399
],
399400
],
400401
9 => null,
401402
13 => [
402-
'tests' => [],
403+
'pathCovered' => false,
404+
'tests' => [],
403405
],
404406
14 => [
405-
'tests' => [],
407+
'pathCovered' => false,
408+
'tests' => [],
406409
],
407410
15 => [
408-
'tests' => [],
411+
'pathCovered' => false,
412+
'tests' => [],
409413
],
410414
16 => [
411-
'tests' => [],
415+
'pathCovered' => false,
416+
'tests' => [],
412417
],
413418
18 => [
414-
'tests' => [],
419+
'pathCovered' => false,
420+
'tests' => [],
415421
],
416422
22 => [
417-
'tests' => [
423+
'pathCovered' => false,
424+
'tests' => [
418425
0 => 'BankAccountTest::testBalanceCannotBecomeNegative2',
419426
1 => 'BankAccountTest::testDepositWithdrawMoney',
420427
],
421428
],
422429
24 => [
423-
'tests' => [
430+
'pathCovered' => false,
431+
'tests' => [
424432
0 => 'BankAccountTest::testDepositWithdrawMoney',
425433
],
426434
],
427435
25 => null,
428436
29 => [
429-
'tests' => [
437+
'pathCovered' => false,
438+
'tests' => [
430439
0 => 'BankAccountTest::testBalanceCannotBecomeNegative',
431440
1 => 'BankAccountTest::testDepositWithdrawMoney',
432441
],
433442
],
434443
31 => [
435-
'tests' => [
444+
'pathCovered' => false,
445+
'tests' => [
436446
0 => 'BankAccountTest::testDepositWithdrawMoney',
437447
],
438448
],
@@ -444,42 +454,49 @@ protected function getExpectedDataArrayForBankAccount()
444454
'line_start' => 20,
445455
'line_end' => 25,
446456
'tests' => [],
457+
'hit' => 0,
447458
],
448459
],
449460
'BankAccount->getBalance' => [
450461
0 => [
451462
'line_start' => 6,
452463
'line_end' => 9,
453464
'tests' => [],
465+
'hit' => 1,
454466
],
455467
],
456468
'BankAccount->withdrawMoney' => [
457469
0 => [
458470
'line_start' => 27,
459471
'line_end' => 32,
460472
'tests' => [],
473+
'hit' => 0,
461474
],
462475
],
463476
'BankAccount->setBalance' => [
464477
0 => [
465478
'line_start' => 11,
466479
'line_end' => 13,
467480
'tests' => [],
481+
'hit' => 0,
468482
],
469483
1 => [
470484
'line_start' => 14,
471485
'line_end' => 15,
472486
'tests' => [],
487+
'hit' => 0,
473488
],
474489
2 => [
475490
'line_start' => 16,
476491
'line_end' => 16,
477492
'tests' => [],
493+
'hit' => 0,
478494
],
479495
3 => [
480496
'line_start' => 18,
481497
'line_end' => 18,
482498
'tests' => [],
499+
'hit' => 0,
483500
],
484501
],
485502
],

0 commit comments

Comments
 (0)