From b59bd21d8b2e6c99486e37709c1206548050720d Mon Sep 17 00:00:00 2001 From: Sergey Karavay Date: Fri, 28 Oct 2016 20:27:18 +0100 Subject: [PATCH] Fix #476 - https://github.com/sebastianbergmann/php-code-coverage/issues/476 Updates: - change the way of storing whitelisted files; - update tests to make them pass. --- src/CodeCoverage.php | 6 ++++++ src/Filter.php | 24 ++++++++++++++++++++---- tests/TestCase.php | 4 +++- tests/tests/BuilderTest.php | 4 +++- tests/tests/FilterTest.php | 9 ++++++++- 5 files changed, 40 insertions(+), 7 deletions(-) diff --git a/src/CodeCoverage.php b/src/CodeCoverage.php index 49332e34e..291387144 100644 --- a/src/CodeCoverage.php +++ b/src/CodeCoverage.php @@ -361,6 +361,8 @@ public function append(array $data, $id = null, $append = true, $linesToBeCovere $this->tests[$id] = ['size' => $size, 'status' => $status]; foreach ($data as $file => $lines) { + $file = $this->filter->unifyFilename($file); + if (!$this->filter->isFile($file)) { continue; } @@ -387,6 +389,8 @@ public function merge(CodeCoverage $that) ); foreach ($that->data as $file => $lines) { + $file = $this->filter->unifyFilename($file); + if (!isset($this->data[$file])) { if (!$this->filter->isFiltered($file)) { $this->data[$file] = $lines; @@ -680,6 +684,8 @@ private function applyIgnoredLinesFilter(array &$data) private function initializeFilesThatAreSeenTheFirstTime(array $data) { foreach ($data as $file => $lines) { + $file = $this->filter->unifyFilename($file); + if ($this->filter->isFile($file) && !isset($this->data[$file])) { $this->data[$file] = []; diff --git a/src/Filter.php b/src/Filter.php index 771a657ae..b641497d1 100644 --- a/src/Filter.php +++ b/src/Filter.php @@ -46,7 +46,7 @@ public function addDirectoryToWhitelist($directory, $suffix = '.php', $prefix = */ public function addFileToWhitelist($filename) { - $this->whitelistedFiles[realpath($filename)] = true; + $this->whitelistedFiles[$this->unifyFilename($filename)] = true; } /** @@ -85,7 +85,7 @@ public function removeDirectoryFromWhitelist($directory, $suffix = '.php', $pref */ public function removeFileFromWhitelist($filename) { - $filename = realpath($filename); + $filename = $this->unifyFilename($filename); unset($this->whitelistedFiles[$filename]); } @@ -126,7 +126,7 @@ public function isFiltered($filename) return true; } - $filename = realpath($filename); + $filename = $this->unifyFilename($filename); return !isset($this->whitelistedFiles[$filename]); } @@ -166,8 +166,24 @@ public function getWhitelistedFiles() * * @param array $whitelistedFiles */ - public function setWhitelistedFiles($whitelistedFiles) + public function setWhitelistedFiles(array $whitelistedFiles) { $this->whitelistedFiles = $whitelistedFiles; + + $this->addFilesToWhitelist(array_keys($whitelistedFiles)); + } + + /** + * Gets unified filename with lower-cased directory path and original name of file. + * + * @param string $filename + * + * @return string + */ + public function unifyFilename($filename) + { + $filename = realpath($filename); + + return strtolower(dirname($filename)) . DIRECTORY_SEPARATOR . basename($filename); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index cd18a8245..062ee2915 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -232,8 +232,10 @@ protected function getCoverageForBankAccountForLastTwoTests() protected function getExpectedDataArrayForBankAccount() { + $filter = new Filter(); + return [ - TEST_FILES_PATH . 'BankAccount.php' => [ + $filter->unifyFilename(TEST_FILES_PATH . 'BankAccount.php') => [ 8 => [ 0 => 'BankAccountTest::testBalanceIsInitiallyZero', 1 => 'BankAccountTest::testDepositWithdrawMoney' diff --git a/tests/tests/BuilderTest.php b/tests/tests/BuilderTest.php index 1a4b6f351..d0b87c48d 100644 --- a/tests/tests/BuilderTest.php +++ b/tests/tests/BuilderTest.php @@ -10,6 +10,7 @@ namespace SebastianBergmann\CodeCoverage\Report; +use SebastianBergmann\CodeCoverage\Filter; use SebastianBergmann\CodeCoverage\TestCase; use SebastianBergmann\CodeCoverage\Node\Builder; @@ -25,8 +26,9 @@ protected function setUp() public function testSomething() { $root = $this->getCoverageForBankAccount()->getReport(); + $filter = new Filter(); - $expectedPath = rtrim(TEST_FILES_PATH, DIRECTORY_SEPARATOR); + $expectedPath = $filter->unifyFilename(rtrim(TEST_FILES_PATH, DIRECTORY_SEPARATOR)); $this->assertEquals($expectedPath, $root->getName()); $this->assertEquals($expectedPath, $root->getPath()); $this->assertEquals(10, $root->getNumExecutableLines()); diff --git a/tests/tests/FilterTest.php b/tests/tests/FilterTest.php index 47ff21a0f..8eea699ab 100644 --- a/tests/tests/FilterTest.php +++ b/tests/tests/FilterTest.php @@ -69,6 +69,12 @@ protected function setUp() TEST_FILES_PATH . 'source_without_ignore.php', TEST_FILES_PATH . 'source_without_namespace.php' ]; + + $filter = $this->filter; + + $this->files = array_map(function($file) use ($filter) { + return $filter->unifyFilename($file); + }, $this->files); } /** @@ -78,9 +84,10 @@ protected function setUp() public function testAddingAFileToTheWhitelistWorks() { $this->filter->addFileToWhitelist($this->files[0]); + $expected = $this->filter->unifyFilename($this->files[0]); $this->assertEquals( - [$this->files[0]], + [$expected], $this->filter->getWhitelist() ); }