Skip to content

Commit b59bd21

Browse files
author
Sergey Karavay
committed
Updates: - change the way of storing whitelisted files; - update tests to make them pass.
1 parent 8ca687f commit b59bd21

File tree

5 files changed

+40
-7
lines changed

5 files changed

+40
-7
lines changed

src/CodeCoverage.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,8 @@ public function append(array $data, $id = null, $append = true, $linesToBeCovere
361361
$this->tests[$id] = ['size' => $size, 'status' => $status];
362362

363363
foreach ($data as $file => $lines) {
364+
$file = $this->filter->unifyFilename($file);
365+
364366
if (!$this->filter->isFile($file)) {
365367
continue;
366368
}
@@ -387,6 +389,8 @@ public function merge(CodeCoverage $that)
387389
);
388390

389391
foreach ($that->data as $file => $lines) {
392+
$file = $this->filter->unifyFilename($file);
393+
390394
if (!isset($this->data[$file])) {
391395
if (!$this->filter->isFiltered($file)) {
392396
$this->data[$file] = $lines;
@@ -680,6 +684,8 @@ private function applyIgnoredLinesFilter(array &$data)
680684
private function initializeFilesThatAreSeenTheFirstTime(array $data)
681685
{
682686
foreach ($data as $file => $lines) {
687+
$file = $this->filter->unifyFilename($file);
688+
683689
if ($this->filter->isFile($file) && !isset($this->data[$file])) {
684690
$this->data[$file] = [];
685691

src/Filter.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function addDirectoryToWhitelist($directory, $suffix = '.php', $prefix =
4646
*/
4747
public function addFileToWhitelist($filename)
4848
{
49-
$this->whitelistedFiles[realpath($filename)] = true;
49+
$this->whitelistedFiles[$this->unifyFilename($filename)] = true;
5050
}
5151

5252
/**
@@ -85,7 +85,7 @@ public function removeDirectoryFromWhitelist($directory, $suffix = '.php', $pref
8585
*/
8686
public function removeFileFromWhitelist($filename)
8787
{
88-
$filename = realpath($filename);
88+
$filename = $this->unifyFilename($filename);
8989

9090
unset($this->whitelistedFiles[$filename]);
9191
}
@@ -126,7 +126,7 @@ public function isFiltered($filename)
126126
return true;
127127
}
128128

129-
$filename = realpath($filename);
129+
$filename = $this->unifyFilename($filename);
130130

131131
return !isset($this->whitelistedFiles[$filename]);
132132
}
@@ -166,8 +166,24 @@ public function getWhitelistedFiles()
166166
*
167167
* @param array $whitelistedFiles
168168
*/
169-
public function setWhitelistedFiles($whitelistedFiles)
169+
public function setWhitelistedFiles(array $whitelistedFiles)
170170
{
171171
$this->whitelistedFiles = $whitelistedFiles;
172+
173+
$this->addFilesToWhitelist(array_keys($whitelistedFiles));
174+
}
175+
176+
/**
177+
* Gets unified filename with lower-cased directory path and original name of file.
178+
*
179+
* @param string $filename
180+
*
181+
* @return string
182+
*/
183+
public function unifyFilename($filename)
184+
{
185+
$filename = realpath($filename);
186+
187+
return strtolower(dirname($filename)) . DIRECTORY_SEPARATOR . basename($filename);
172188
}
173189
}

tests/TestCase.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,10 @@ protected function getCoverageForBankAccountForLastTwoTests()
232232

233233
protected function getExpectedDataArrayForBankAccount()
234234
{
235+
$filter = new Filter();
236+
235237
return [
236-
TEST_FILES_PATH . 'BankAccount.php' => [
238+
$filter->unifyFilename(TEST_FILES_PATH . 'BankAccount.php') => [
237239
8 => [
238240
0 => 'BankAccountTest::testBalanceIsInitiallyZero',
239241
1 => 'BankAccountTest::testDepositWithdrawMoney'

tests/tests/BuilderTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace SebastianBergmann\CodeCoverage\Report;
1212

13+
use SebastianBergmann\CodeCoverage\Filter;
1314
use SebastianBergmann\CodeCoverage\TestCase;
1415
use SebastianBergmann\CodeCoverage\Node\Builder;
1516

@@ -25,8 +26,9 @@ protected function setUp()
2526
public function testSomething()
2627
{
2728
$root = $this->getCoverageForBankAccount()->getReport();
29+
$filter = new Filter();
2830

29-
$expectedPath = rtrim(TEST_FILES_PATH, DIRECTORY_SEPARATOR);
31+
$expectedPath = $filter->unifyFilename(rtrim(TEST_FILES_PATH, DIRECTORY_SEPARATOR));
3032
$this->assertEquals($expectedPath, $root->getName());
3133
$this->assertEquals($expectedPath, $root->getPath());
3234
$this->assertEquals(10, $root->getNumExecutableLines());

tests/tests/FilterTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ protected function setUp()
6969
TEST_FILES_PATH . 'source_without_ignore.php',
7070
TEST_FILES_PATH . 'source_without_namespace.php'
7171
];
72+
73+
$filter = $this->filter;
74+
75+
$this->files = array_map(function($file) use ($filter) {
76+
return $filter->unifyFilename($file);
77+
}, $this->files);
7278
}
7379

7480
/**
@@ -78,9 +84,10 @@ protected function setUp()
7884
public function testAddingAFileToTheWhitelistWorks()
7985
{
8086
$this->filter->addFileToWhitelist($this->files[0]);
87+
$expected = $this->filter->unifyFilename($this->files[0]);
8188

8289
$this->assertEquals(
83-
[$this->files[0]],
90+
[$expected],
8491
$this->filter->getWhitelist()
8592
);
8693
}

0 commit comments

Comments
 (0)