Skip to content

Commit

Permalink
#864 Uncovered files should be ignored unless requested
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdoug committed Sep 24, 2021
1 parent 7cecb7b commit 8604ff3
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/CodeCoverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ public function getData(bool $raw = false): ProcessedCodeCoverageData
$this->processUncoveredFilesFromFilter();
} elseif ($this->includeUncoveredFiles) {
$this->addUncoveredFilesFromFilter();
} else {
$this->data->removeFilesWithNoCoverage();
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/ProcessedCodeCoverageData.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,18 @@ public function renameFile(string $oldFile, string $newFile): void
unset($this->lineCoverage[$oldFile], $this->functionCoverage[$oldFile]);
}

public function removeFilesWithNoCoverage(): void
{
foreach ($this->lineCoverage as $file => $lines) {
foreach ($lines as $line) {
if (is_array($line) && !empty($line)) {
continue 2;
}
}
unset($file);
}
}

public function merge(self $newData): void
{
foreach ($newData->lineCoverage as $file => $lines) {
Expand Down
124 changes: 124 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1812,4 +1812,128 @@ protected function removeTemporaryFiles(): void
$fileInfo->isDir() ? rmdir($pathname) : unlink($pathname);
}
}

protected function getCoverageForFilesWithUncoveredIncluded(): CodeCoverage
{
$data = $this->getLineCoverageXdebugDataForBankAccount();

$stub = $this->createStub(Driver::class);

$stub->method('stop')
->will($this->onConsecutiveCalls(...$data));

$filter = new Filter;
$filter->includeFile(TEST_FILES_PATH . 'BankAccount.php');
$filter->includeFile(TEST_FILES_PATH . 'NamespacedBankAccount.php');

$coverage = new CodeCoverage($stub, $filter);
$coverage->includeUncoveredFiles();

$coverage->start(
new BankAccountTest('testBalanceIsInitiallyZero'),
true
);

$coverage->stop(
true,
[TEST_FILES_PATH . 'BankAccount.php' => range(6, 9)]
);

$coverage->start(
new BankAccountTest('testBalanceCannotBecomeNegative')
);

$coverage->stop(
true,
[TEST_FILES_PATH . 'BankAccount.php' => range(27, 32)]
);

$coverage->start(
new BankAccountTest('testBalanceCannotBecomeNegative2')
);

$coverage->stop(
true,
[TEST_FILES_PATH . 'BankAccount.php' => range(20, 25)]
);

$coverage->start(
new BankAccountTest('testDepositWithdrawMoney')
);

$coverage->stop(
true,
[
TEST_FILES_PATH . 'BankAccount.php' => array_merge(
range(6, 9),
range(20, 25),
range(27, 32)
),
]
);

return $coverage;
}

protected function getCoverageForFilesWithUncoveredExcluded(): CodeCoverage
{
$data = $this->getLineCoverageXdebugDataForBankAccount();

$stub = $this->createStub(Driver::class);

$stub->method('stop')
->will($this->onConsecutiveCalls(...$data));

$filter = new Filter;
$filter->includeFile(TEST_FILES_PATH . 'BankAccount.php');
$filter->includeFile(TEST_FILES_PATH . 'NamespacedBankAccount.php');

$coverage = new CodeCoverage($stub, $filter);
$coverage->excludeUncoveredFiles();

$coverage->start(
new BankAccountTest('testBalanceIsInitiallyZero'),
true
);

$coverage->stop(
true,
[TEST_FILES_PATH . 'BankAccount.php' => range(6, 9)]
);

$coverage->start(
new BankAccountTest('testBalanceCannotBecomeNegative')
);

$coverage->stop(
true,
[TEST_FILES_PATH . 'BankAccount.php' => range(27, 32)]
);

$coverage->start(
new BankAccountTest('testBalanceCannotBecomeNegative2')
);

$coverage->stop(
true,
[TEST_FILES_PATH . 'BankAccount.php' => range(20, 25)]
);

$coverage->start(
new BankAccountTest('testDepositWithdrawMoney')
);

$coverage->stop(
true,
[
TEST_FILES_PATH . 'BankAccount.php' => array_merge(
range(6, 9),
range(20, 25),
range(27, 32)
),
]
);

return $coverage;
}
}
12 changes: 12 additions & 0 deletions tests/_files/BankAccountWithUncovered-text-line.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@


Code Coverage Report:
%s

Summary:
Classes: 0.00% (0/2)
Methods: 37.50% (3/8)
Lines: 26.32% (5/19)

BankAccount
Methods: 75.00% ( 3/ 4) Lines: 50.00% ( 5/ 10)
12 changes: 12 additions & 0 deletions tests/_files/BankAccountWithoutUncovered-text-line.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@


Code Coverage Report:
%s

Summary:
Classes: 0.00% (0/1)
Methods: 75.00% (3/4)
Lines: 50.00% (5/10)

BankAccount
Methods: 75.00% ( 3/ 4) Lines: 50.00% ( 5/ 10)
20 changes: 20 additions & 0 deletions tests/tests/TextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,24 @@ public function testTextForClassWithAnonymousFunction(): void
str_replace(PHP_EOL, "\n", $text->process($this->getCoverageForClassWithAnonymousFunction()))
);
}

public function testUncoveredFilesAreIncludedWhenConfiguredTest(): void
{
$text = new Text(50, 90, false, false);

$this->assertStringMatchesFormatFile(
TEST_FILES_PATH . 'BankAccountWithUncovered-text-line.txt',
str_replace(PHP_EOL, "\n", $text->process($this->getCoverageForFilesWithUncoveredIncluded()))
);
}

public function testUncoveredFilesAreExcludedWhenConfiguredTest(): void
{
$text = new Text(50, 90, false, false);

$this->assertStringMatchesFormatFile(
TEST_FILES_PATH . 'BankAccountWithoutUncovered-text-line.txt',
str_replace(PHP_EOL, "\n", $text->process($this->getCoverageForFilesWithUncoveredExcluded()))
);
}
}

0 comments on commit 8604ff3

Please sign in to comment.