Skip to content

Fix for #476 #478

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/CodeCoverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -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] = [];

Expand Down
24 changes: 20 additions & 4 deletions src/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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]);
}
Expand Down Expand Up @@ -126,7 +126,7 @@ public function isFiltered($filename)
return true;
}

$filename = realpath($filename);
$filename = $this->unifyFilename($filename);

return !isset($this->whitelistedFiles[$filename]);
}
Expand Down Expand Up @@ -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);
}
}
4 changes: 3 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 3 additions & 1 deletion tests/tests/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace SebastianBergmann\CodeCoverage\Report;

use SebastianBergmann\CodeCoverage\Filter;
use SebastianBergmann\CodeCoverage\TestCase;
use SebastianBergmann\CodeCoverage\Node\Builder;

Expand All @@ -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());
Expand Down
9 changes: 8 additions & 1 deletion tests/tests/FilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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()
);
}
Expand Down