diff --git a/src/CodeCoverage.php b/src/CodeCoverage.php index 61b527411..fff282c52 100644 --- a/src/CodeCoverage.php +++ b/src/CodeCoverage.php @@ -380,6 +380,31 @@ public function merge(self $that): void continue; } + if ((count($lines) > 0) + && (count($this->data[$file]) > 0) + && (count($lines) != count($this->data[$file])) + ) { + if (count($lines) > count($ours = $this->data[$file])) { + // More lines in the one being added in + $lines = array_filter( + $lines, + function ($value, $key) use ($ours) { + return array_key_exists($key, $ours); + }, + ARRAY_FILTER_USE_BOTH + ); + } else { + // More lines in the one we currently have + $this->data[$file] = array_filter( + $this->data[$file], + function ($value, $key) use ($lines) { + return array_key_exists($key, $lines); + }, + ARRAY_FILTER_USE_BOTH + ); + } + } + foreach ($lines as $line => $data) { if ($data !== null) { if (!isset($this->data[$file][$line])) { diff --git a/tests/TestCase.php b/tests/TestCase.php index b20b8c7e5..ee25360de 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -231,6 +231,34 @@ protected function getCoverageForBankAccountForLastTwoTests() } protected function getExpectedDataArrayForBankAccount() + { + return [ + TEST_FILES_PATH . 'BankAccount.php' => [ + 8 => [ + 0 => 'BankAccountTest::testBalanceIsInitiallyZero', + 1 => 'BankAccountTest::testDepositWithdrawMoney' + ], + 13 => [], + 16 => [], + 22 => [ + 0 => 'BankAccountTest::testBalanceCannotBecomeNegative2', + 1 => 'BankAccountTest::testDepositWithdrawMoney' + ], + 24 => [ + 0 => 'BankAccountTest::testDepositWithdrawMoney', + ], + 29 => [ + 0 => 'BankAccountTest::testBalanceCannotBecomeNegative', + 1 => 'BankAccountTest::testDepositWithdrawMoney' + ], + 31 => [ + 0 => 'BankAccountTest::testDepositWithdrawMoney' + ], + ] + ]; + } + + protected function getExpectedDataArrayForBankAccount2() { return [ TEST_FILES_PATH . 'BankAccount.php' => [ diff --git a/tests/tests/CodeCoverageTest.php b/tests/tests/CodeCoverageTest.php index 091b78bc1..806c7e9e1 100644 --- a/tests/tests/CodeCoverageTest.php +++ b/tests/tests/CodeCoverageTest.php @@ -215,7 +215,7 @@ public function testCollect() $coverage = $this->getCoverageForBankAccount(); $this->assertEquals( - $this->getExpectedDataArrayForBankAccount(), + $this->getExpectedDataArrayForBankAccount2(), $coverage->getData() ); @@ -251,7 +251,7 @@ public function testMerge2() $coverage->merge($this->getCoverageForBankAccount()); $this->assertEquals( - $this->getExpectedDataArrayForBankAccount(), + $this->getExpectedDataArrayForBankAccount2(), $coverage->getData() ); }