Skip to content

Commit d1657b4

Browse files
committed
Added option to ignore @Covers annotation
1 parent 117fa19 commit d1657b4

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed

src/CodeCoverage.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ final class CodeCoverage
4545
private bool $checkForUnintentionallyCoveredCode = false;
4646
private bool $includeUncoveredFiles = true;
4747
private bool $ignoreDeprecatedCode = false;
48+
private bool $ignoreCoversAnnotation = false;
4849
private ?string $currentId = null;
4950
private ?TestSize $currentSize = null;
5051
private ProcessedCodeCoverageData $data;
@@ -278,6 +279,16 @@ public function doNotIgnoreDeprecatedCode(): void
278279
$this->ignoreDeprecatedCode = false;
279280
}
280281

282+
public function ignoreCoversAnnotation(): void
283+
{
284+
$this->ignoreCoversAnnotation = true;
285+
}
286+
287+
public function doNotIgnoreCoversAnnotation(): void
288+
{
289+
$this->ignoreCoversAnnotation = false;
290+
}
291+
281292
/**
282293
* @psalm-assert-if-true !null $this->cacheDirectory
283294
*/
@@ -344,6 +355,10 @@ public function detectsDeadCode(): bool
344355
*/
345356
private function applyCoversAndUsesFilter(RawCodeCoverageData $rawData, array|false $linesToBeCovered, array $linesToBeUsed, TestSize $size): void
346357
{
358+
if ($this->ignoreCoversAnnotation) {
359+
return;
360+
}
361+
347362
if ($linesToBeCovered === false) {
348363
$rawData->clear();
349364

tests/TestCase.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2001,4 +2001,73 @@ protected function getCoverageForFilesWithUncoveredExcluded(): CodeCoverage
20012001

20022002
return $coverage;
20032003
}
2004+
2005+
protected function getCoverageForFilesUseIgnoreCoversAnnotation(?bool $ignoreCoversAnnotation): CodeCoverage
2006+
{
2007+
$data = $this->getLineCoverageXdebugDataForBankAccount();
2008+
2009+
$stub = $this->createStub(Driver::class);
2010+
2011+
$stub->method('stop')
2012+
->will($this->onConsecutiveCalls(...$data));
2013+
2014+
$filter = new Filter;
2015+
$filter->includeFile(TEST_FILES_PATH . 'BankAccount.php');
2016+
$filter->includeFile(TEST_FILES_PATH . 'NamespacedBankAccount.php');
2017+
2018+
$coverage = new CodeCoverage($stub, $filter);
2019+
if ($ignoreCoversAnnotation !== null) {
2020+
$ignoreCoversAnnotation ? $coverage->ignoreCoversAnnotation() : $coverage->doNotIgnoreCoversAnnotation();
2021+
}
2022+
2023+
$coverage->start(
2024+
'BankAccountTest::testBalanceIsInitiallyZero',
2025+
null,
2026+
true
2027+
);
2028+
2029+
$coverage->stop(
2030+
true,
2031+
null,
2032+
[TEST_FILES_PATH . 'BankAccount.php' => range(6, 9)]
2033+
);
2034+
2035+
$coverage->start(
2036+
'BankAccountTest::testBalanceCannotBecomeNegative'
2037+
);
2038+
2039+
$coverage->stop(
2040+
true,
2041+
null,
2042+
[TEST_FILES_PATH . 'BankAccount.php' => range(27, 32)]
2043+
);
2044+
2045+
$coverage->start(
2046+
'BankAccountTest::testBalanceCannotBecomeNegative2'
2047+
);
2048+
2049+
$coverage->stop(
2050+
true,
2051+
null,
2052+
[TEST_FILES_PATH . 'BankAccount.php' => range(20, 25)]
2053+
);
2054+
2055+
$coverage->start(
2056+
'BankAccountTest::testDepositWithdrawMoney'
2057+
);
2058+
2059+
$coverage->stop(
2060+
true,
2061+
null,
2062+
[
2063+
TEST_FILES_PATH . 'BankAccount.php' => array_merge(
2064+
range(6, 9),
2065+
range(20, 25),
2066+
range(27, 32)
2067+
),
2068+
]
2069+
);
2070+
2071+
return $coverage;
2072+
}
20042073
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
3+
Code Coverage Report:
4+
%s
5+
6+
Summary:
7+
Classes: 50.00% (1/2)
8+
Methods: 50.00% (4/8)
9+
Lines: 50.00% (8/16)
10+
11+
BankAccount
12+
Methods: 100.00% ( 4/ 4) Lines: 100.00% ( 8/ 8)

tests/tests/Report/TextTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,24 @@ public function testUncoveredFilesAreExcludedWhenConfiguredTest(): void
9696
str_replace(PHP_EOL, "\n", $text->process($this->getCoverageForFilesWithUncoveredExcluded()))
9797
);
9898
}
99+
100+
/**
101+
* @dataProvider ignoreCoversAnnotationProvider
102+
*/
103+
public function testWithIgnoreCoversAnnotationWhenConfiguredTest($isUseIgnoreCoversAnnotation, $file): void
104+
{
105+
$text = new Text(Thresholds::default(), false, false);
106+
$codeCoverage = $this->getCoverageForFilesUseIgnoreCoversAnnotation($isUseIgnoreCoversAnnotation);
107+
108+
$this->assertStringMatchesFormatFile($file, str_replace(PHP_EOL, "\n", $text->process($codeCoverage)));
109+
}
110+
111+
private function ignoreCoversAnnotationProvider(): array
112+
{
113+
return [
114+
'with' => [true, TEST_FILES_PATH . 'BankAccountWithUncoveredAndIgnoreCoversAnnotation-text-line.txt'],
115+
'without' => [false, TEST_FILES_PATH . 'BankAccountWithUncovered-text-line.txt'],
116+
'default' => [null, TEST_FILES_PATH . 'BankAccountWithUncovered-text-line.txt'],
117+
];
118+
}
99119
}

0 commit comments

Comments
 (0)