Skip to content

Commit 9e63c72

Browse files
committed
Prepare for extension. Add test key to line data
1 parent 93e232b commit 9e63c72

File tree

6 files changed

+67
-29
lines changed

6 files changed

+67
-29
lines changed

src/CodeCoverage.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -331,10 +331,15 @@ public function append(array $data, $id = null, $append = true, $linesToBeCovere
331331
continue;
332332
}
333333

334-
foreach ($fileData['lines'] as $k => $v) {
335-
if ($v == PHP_CodeCoverage_Driver::LINE_EXECUTED) {
336-
if (empty($this->data[$file]['lines'][$k]) || !in_array($id, $this->data[$file]['lines'][$k])) {
337-
$this->data[$file]['lines'][$k][] = $id;
334+
foreach ($fileData['lines'] as $line => $flag) {
335+
if ($flag === PHP_CodeCoverage_Driver::LINE_EXECUTED) {
336+
$lineData = &$this->data[$file]['lines'][$line];
337+
if ($lineData === null) {
338+
$lineData = [
339+
'tests' => [$id],
340+
];
341+
} elseif (!in_array($id, $lineData['tests'])) {
342+
$lineData['tests'][] = $id;
338343
}
339344
}
340345
}
@@ -366,8 +371,8 @@ public function merge(PHP_CodeCoverage $that)
366371
if (!isset($this->data[$file]['lines'][$line])) {
367372
$this->data[$file]['lines'][$line] = $data;
368373
} else {
369-
$this->data[$file]['lines'][$line] = array_unique(
370-
array_merge($this->data[$file]['lines'][$line], $data)
374+
$this->data[$file]['lines'][$line]['tests'] = array_unique(
375+
array_merge($this->data[$file]['lines'][$line]['tests'], $data['tests'])
371376
);
372377
}
373378
}
@@ -609,7 +614,13 @@ private function initializeFilesThatAreSeenTheFirstTime(array $data)
609614
}
610615

611616
foreach ($fileData['lines'] as $lineNumber => $flag) {
612-
$this->data[$file]['lines'][$lineNumber] = $flag == -2 ? null : [];
617+
if ($flag === PHP_CodeCoverage_Driver::LINE_NOT_EXECUTABLE) {
618+
$this->data[$file]['lines'][$lineNumber] = null;
619+
} else {
620+
$this->data[$file]['lines'][$lineNumber] = [
621+
'tests' => [],
622+
];
623+
}
613624
}
614625
}
615626
}

src/CodeCoverage/Report/Clover.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function process(PHP_CodeCoverage $coverage, $target = null, $name = null
8080
$i <= $method['endLine'];
8181
$i++) {
8282
if (isset($coverage['lines'][$i])) {
83-
$methodCount = max($methodCount, count($coverage['lines'][$i]));
83+
$methodCount = max($methodCount, count($coverage['lines'][$i]['tests']));
8484
}
8585
}
8686

@@ -164,7 +164,8 @@ public function process(PHP_CodeCoverage $coverage, $target = null, $name = null
164164
}
165165

166166
$lines[$line] = [
167-
'count' => count($data), 'type' => 'stmt'
167+
'count' => count($data['tests']),
168+
'type' => 'stmt',
168169
];
169170
}
170171

src/CodeCoverage/Report/HTML/Renderer/File.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -338,23 +338,24 @@ protected function renderSource(PHP_CodeCoverage_Report_Node_File $node)
338338
$popoverTitle = '';
339339

340340
if (array_key_exists($i, $coverageData['lines'])) {
341-
$numTests = count($coverageData['lines'][$i]);
341+
$lineData = $coverageData['lines'][$i];
342342

343-
if ($coverageData['lines'][$i] === null) {
343+
if ($lineData === null) {
344344
$trClass = ' class="warning"';
345-
} elseif ($numTests == 0) {
345+
} elseif (empty($lineData['tests'])) {
346346
$trClass = ' class="danger"';
347347
} else {
348348
$lineCss = 'covered-by-large-tests';
349349
$popoverContent = '<ul>';
350350

351+
$numTests = count($lineData['tests']);
351352
if ($numTests > 1) {
352353
$popoverTitle = $numTests . ' tests cover line ' . $i;
353354
} else {
354355
$popoverTitle = '1 test covers line ' . $i;
355356
}
356357

357-
foreach ($coverageData['lines'][$i] as $test) {
358+
foreach ($lineData['tests'] as $test) {
358359
if ($lineCss == 'covered-by-large-tests' && $testData[$test]['size'] == 'medium') {
359360
$lineCss = 'covered-by-medium-tests';
360361
} elseif ($testData[$test]['size'] == 'small') {

src/CodeCoverage/Report/Node/File.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ protected function calculateStatistics()
439439

440440
$this->numExecutableLines++;
441441

442-
if (count($this->coverageData['lines'][$lineNumber]) > 0) {
442+
if (!empty($this->coverageData['lines'][$lineNumber]['tests'])) {
443443
if (isset($currentClass)) {
444444
$currentClass['executedLines']++;
445445
}

src/CodeCoverage/Report/XML.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,13 @@ private function processFile(PHP_CodeCoverage_Report_Node_File $file, PHP_CodeCo
116116

117117
$fileData = $file->getCoverageData();
118118

119-
foreach ($fileData['lines'] as $line => $tests) {
120-
if (!is_array($tests) || count($tests) == 0) {
119+
foreach ($fileData['lines'] as $line => $lineData) {
120+
if ($lineData === null) {
121+
continue;
122+
}
123+
124+
$tests = $lineData['tests'];
125+
if (empty($tests)) {
121126
continue;
122127
}
123128

tests/TestCase.php

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -392,29 +392,49 @@ protected function getExpectedDataArrayForBankAccount()
392392
TEST_FILES_PATH . 'BankAccount.php' => [
393393
'lines' => [
394394
8 => [
395-
0 => 'BankAccountTest::testBalanceIsInitiallyZero',
396-
1 => 'BankAccountTest::testDepositWithdrawMoney',
395+
'tests' => [
396+
0 => 'BankAccountTest::testBalanceIsInitiallyZero',
397+
1 => 'BankAccountTest::testDepositWithdrawMoney',
398+
],
397399
],
398400
9 => null,
399-
13 => [],
400-
14 => [],
401-
15 => [],
402-
16 => [],
403-
18 => [],
401+
13 => [
402+
'tests' => [],
403+
],
404+
14 => [
405+
'tests' => [],
406+
],
407+
15 => [
408+
'tests' => [],
409+
],
410+
16 => [
411+
'tests' => [],
412+
],
413+
18 => [
414+
'tests' => [],
415+
],
404416
22 => [
405-
0 => 'BankAccountTest::testBalanceCannotBecomeNegative2',
406-
1 => 'BankAccountTest::testDepositWithdrawMoney',
417+
'tests' => [
418+
0 => 'BankAccountTest::testBalanceCannotBecomeNegative2',
419+
1 => 'BankAccountTest::testDepositWithdrawMoney',
420+
],
407421
],
408422
24 => [
409-
0 => 'BankAccountTest::testDepositWithdrawMoney',
423+
'tests' => [
424+
0 => 'BankAccountTest::testDepositWithdrawMoney',
425+
],
410426
],
411427
25 => null,
412428
29 => [
413-
0 => 'BankAccountTest::testBalanceCannotBecomeNegative',
414-
1 => 'BankAccountTest::testDepositWithdrawMoney',
429+
'tests' => [
430+
0 => 'BankAccountTest::testBalanceCannotBecomeNegative',
431+
1 => 'BankAccountTest::testDepositWithdrawMoney',
432+
],
415433
],
416434
31 => [
417-
0 => 'BankAccountTest::testDepositWithdrawMoney',
435+
'tests' => [
436+
0 => 'BankAccountTest::testDepositWithdrawMoney',
437+
],
418438
],
419439
32 => null,
420440
],

0 commit comments

Comments
 (0)