Skip to content

Commit 113e17b

Browse files
committed
Prepare for extension. Add test key to line data
1 parent c6e5a4b commit 113e17b

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
@@ -326,10 +326,15 @@ public function append(array $data, $id = null, $append = true, $linesToBeCovere
326326
continue;
327327
}
328328

329-
foreach ($fileData['lines'] as $k => $v) {
330-
if ($v == PHP_CodeCoverage_Driver::LINE_EXECUTED) {
331-
if (empty($this->data[$file]['lines'][$k]) || !in_array($id, $this->data[$file]['lines'][$k])) {
332-
$this->data[$file]['lines'][$k][] = $id;
329+
foreach ($fileData['lines'] as $line => $flag) {
330+
if ($flag === PHP_CodeCoverage_Driver::LINE_EXECUTED) {
331+
$lineData = &$this->data[$file]['lines'][$line];
332+
if ($lineData === null) {
333+
$lineData = [
334+
'tests' => [$id],
335+
];
336+
} elseif (!in_array($id, $lineData['tests'])) {
337+
$lineData['tests'][] = $id;
333338
}
334339
}
335340
}
@@ -361,8 +366,8 @@ public function merge(PHP_CodeCoverage $that)
361366
if (!isset($this->data[$file]['lines'][$line])) {
362367
$this->data[$file]['lines'][$line] = $data;
363368
} else {
364-
$this->data[$file]['lines'][$line] = array_unique(
365-
array_merge($this->data[$file]['lines'][$line], $data)
369+
$this->data[$file]['lines'][$line]['tests'] = array_unique(
370+
array_merge($this->data[$file]['lines'][$line]['tests'], $data['tests'])
366371
);
367372
}
368373
}
@@ -597,7 +602,13 @@ private function initializeFilesThatAreSeenTheFirstTime(array $data)
597602
}
598603

599604
foreach ($fileData['lines'] as $lineNumber => $flag) {
600-
$this->data[$file]['lines'][$lineNumber] = $flag == -2 ? null : [];
605+
if ($flag === PHP_CodeCoverage_Driver::LINE_NOT_EXECUTABLE) {
606+
$this->data[$file]['lines'][$lineNumber] = null;
607+
} else {
608+
$this->data[$file]['lines'][$lineNumber] = [
609+
'tests' => [],
610+
];
611+
}
601612
}
602613
}
603614
}

src/CodeCoverage/Report/Clover.php

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

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

165165
$lines[$line] = [
166-
'count' => count($data), 'type' => 'stmt'
166+
'count' => count($data['tests']),
167+
'type' => 'stmt',
167168
];
168169
}
169170

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

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

316316
if (array_key_exists($i, $coverageData['lines'])) {
317-
$numTests = count($coverageData['lines'][$i]);
317+
$lineData = $coverageData['lines'][$i];
318318

319-
if ($coverageData['lines'][$i] === null) {
319+
if ($lineData === null) {
320320
$trClass = ' class="warning"';
321-
} elseif ($numTests == 0) {
321+
} elseif (empty($lineData['tests'])) {
322322
$trClass = ' class="danger"';
323323
} else {
324324
$lineCss = 'covered-by-large-tests';
325325
$popoverContent = '<ul>';
326326

327+
$numTests = count($lineData['tests']);
327328
if ($numTests > 1) {
328329
$popoverTitle = $numTests . ' tests cover line ' . $i;
329330
} else {
330331
$popoverTitle = '1 test covers line ' . $i;
331332
}
332333

333-
foreach ($coverageData['lines'][$i] as $test) {
334+
foreach ($lineData['tests'] as $test) {
334335
if ($lineCss == 'covered-by-large-tests' && $testData[$test]['size'] == 'medium') {
335336
$lineCss = 'covered-by-medium-tests';
336337
} 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)