Skip to content
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

#864 Uncovered files should be ignored unless requested #868

Merged
Merged
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
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This addition does nothing: unsetting $file does not alter $this->lineCoverage.

Commenting the whole removeFilesWithNoCoverage content still makes the tests pass

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, that's embarrassing...

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not only for you, also for me, because I reviewed this ;)

}
}

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()))
);
}
}