Skip to content

Commit 1a10dfe

Browse files
celowskysebastianbergmann
authored andcommitted
Fixes incorrect code coverage reporting
1 parent 58b8739 commit 1a10dfe

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

src/CodeCoverage.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,31 @@ public function merge(self $that): void
380380
continue;
381381
}
382382

383+
if ((count($lines) > 0)
384+
&& (count($this->data[$file]) > 0)
385+
&& (count($lines) != count($this->data[$file]))
386+
) {
387+
if (count($lines) > count($ours = $this->data[$file])) {
388+
// More lines in the one being added in
389+
$lines = array_filter(
390+
$lines,
391+
function ($value, $key) use ($ours) {
392+
return array_key_exists($key, $ours);
393+
},
394+
ARRAY_FILTER_USE_BOTH
395+
);
396+
} else {
397+
// More lines in the one we currently have
398+
$this->data[$file] = array_filter(
399+
$this->data[$file],
400+
function ($value, $key) use ($lines) {
401+
return array_key_exists($key, $lines);
402+
},
403+
ARRAY_FILTER_USE_BOTH
404+
);
405+
}
406+
}
407+
383408
foreach ($lines as $line => $data) {
384409
if ($data !== null) {
385410
if (!isset($this->data[$file][$line])) {

tests/TestCase.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,34 @@ protected function getCoverageForBankAccountForLastTwoTests()
231231
}
232232

233233
protected function getExpectedDataArrayForBankAccount()
234+
{
235+
return [
236+
TEST_FILES_PATH . 'BankAccount.php' => [
237+
8 => [
238+
0 => 'BankAccountTest::testBalanceIsInitiallyZero',
239+
1 => 'BankAccountTest::testDepositWithdrawMoney'
240+
],
241+
13 => [],
242+
16 => [],
243+
22 => [
244+
0 => 'BankAccountTest::testBalanceCannotBecomeNegative2',
245+
1 => 'BankAccountTest::testDepositWithdrawMoney'
246+
],
247+
24 => [
248+
0 => 'BankAccountTest::testDepositWithdrawMoney',
249+
],
250+
29 => [
251+
0 => 'BankAccountTest::testBalanceCannotBecomeNegative',
252+
1 => 'BankAccountTest::testDepositWithdrawMoney'
253+
],
254+
31 => [
255+
0 => 'BankAccountTest::testDepositWithdrawMoney'
256+
],
257+
]
258+
];
259+
}
260+
261+
protected function getExpectedDataArrayForBankAccount2()
234262
{
235263
return [
236264
TEST_FILES_PATH . 'BankAccount.php' => [

tests/tests/CodeCoverageTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public function testCollect()
215215
$coverage = $this->getCoverageForBankAccount();
216216

217217
$this->assertEquals(
218-
$this->getExpectedDataArrayForBankAccount(),
218+
$this->getExpectedDataArrayForBankAccount2(),
219219
$coverage->getData()
220220
);
221221

@@ -251,7 +251,7 @@ public function testMerge2()
251251
$coverage->merge($this->getCoverageForBankAccount());
252252

253253
$this->assertEquals(
254-
$this->getExpectedDataArrayForBankAccount(),
254+
$this->getExpectedDataArrayForBankAccount2(),
255255
$coverage->getData()
256256
);
257257
}

0 commit comments

Comments
 (0)