From d1657b4116a29578be4bac86e8575e6d2e5e07b9 Mon Sep 17 00:00:00 2001 From: vodevel Date: Sun, 5 Mar 2023 00:47:32 +0300 Subject: [PATCH] Added option to ignore @covers annotation --- src/CodeCoverage.php | 15 ++++ tests/TestCase.php | 69 +++++++++++++++++++ ...redAndIgnoreCoversAnnotation-text-line.txt | 12 ++++ tests/tests/Report/TextTest.php | 20 ++++++ 4 files changed, 116 insertions(+) create mode 100644 tests/_files/BankAccountWithUncoveredAndIgnoreCoversAnnotation-text-line.txt diff --git a/src/CodeCoverage.php b/src/CodeCoverage.php index 6164b9b8c..9aae452e0 100644 --- a/src/CodeCoverage.php +++ b/src/CodeCoverage.php @@ -45,6 +45,7 @@ final class CodeCoverage private bool $checkForUnintentionallyCoveredCode = false; private bool $includeUncoveredFiles = true; private bool $ignoreDeprecatedCode = false; + private bool $ignoreCoversAnnotation = false; private ?string $currentId = null; private ?TestSize $currentSize = null; private ProcessedCodeCoverageData $data; @@ -278,6 +279,16 @@ public function doNotIgnoreDeprecatedCode(): void $this->ignoreDeprecatedCode = false; } + public function ignoreCoversAnnotation(): void + { + $this->ignoreCoversAnnotation = true; + } + + public function doNotIgnoreCoversAnnotation(): void + { + $this->ignoreCoversAnnotation = false; + } + /** * @psalm-assert-if-true !null $this->cacheDirectory */ @@ -344,6 +355,10 @@ public function detectsDeadCode(): bool */ private function applyCoversAndUsesFilter(RawCodeCoverageData $rawData, array|false $linesToBeCovered, array $linesToBeUsed, TestSize $size): void { + if ($this->ignoreCoversAnnotation) { + return; + } + if ($linesToBeCovered === false) { $rawData->clear(); diff --git a/tests/TestCase.php b/tests/TestCase.php index e6e34660c..3e260dcee 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2001,4 +2001,73 @@ protected function getCoverageForFilesWithUncoveredExcluded(): CodeCoverage return $coverage; } + + protected function getCoverageForFilesUseIgnoreCoversAnnotation(?bool $ignoreCoversAnnotation): 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); + if ($ignoreCoversAnnotation !== null) { + $ignoreCoversAnnotation ? $coverage->ignoreCoversAnnotation() : $coverage->doNotIgnoreCoversAnnotation(); + } + + $coverage->start( + 'BankAccountTest::testBalanceIsInitiallyZero', + null, + true + ); + + $coverage->stop( + true, + null, + [TEST_FILES_PATH . 'BankAccount.php' => range(6, 9)] + ); + + $coverage->start( + 'BankAccountTest::testBalanceCannotBecomeNegative' + ); + + $coverage->stop( + true, + null, + [TEST_FILES_PATH . 'BankAccount.php' => range(27, 32)] + ); + + $coverage->start( + 'BankAccountTest::testBalanceCannotBecomeNegative2' + ); + + $coverage->stop( + true, + null, + [TEST_FILES_PATH . 'BankAccount.php' => range(20, 25)] + ); + + $coverage->start( + 'BankAccountTest::testDepositWithdrawMoney' + ); + + $coverage->stop( + true, + null, + [ + TEST_FILES_PATH . 'BankAccount.php' => array_merge( + range(6, 9), + range(20, 25), + range(27, 32) + ), + ] + ); + + return $coverage; + } } diff --git a/tests/_files/BankAccountWithUncoveredAndIgnoreCoversAnnotation-text-line.txt b/tests/_files/BankAccountWithUncoveredAndIgnoreCoversAnnotation-text-line.txt new file mode 100644 index 000000000..5620535d3 --- /dev/null +++ b/tests/_files/BankAccountWithUncoveredAndIgnoreCoversAnnotation-text-line.txt @@ -0,0 +1,12 @@ + + +Code Coverage Report: + %s + + Summary: + Classes: 50.00% (1/2) + Methods: 50.00% (4/8) + Lines: 50.00% (8/16) + +BankAccount + Methods: 100.00% ( 4/ 4) Lines: 100.00% ( 8/ 8) diff --git a/tests/tests/Report/TextTest.php b/tests/tests/Report/TextTest.php index 9194712d7..b15be507c 100644 --- a/tests/tests/Report/TextTest.php +++ b/tests/tests/Report/TextTest.php @@ -96,4 +96,24 @@ public function testUncoveredFilesAreExcludedWhenConfiguredTest(): void str_replace(PHP_EOL, "\n", $text->process($this->getCoverageForFilesWithUncoveredExcluded())) ); } + + /** + * @dataProvider ignoreCoversAnnotationProvider + */ + public function testWithIgnoreCoversAnnotationWhenConfiguredTest($isUseIgnoreCoversAnnotation, $file): void + { + $text = new Text(Thresholds::default(), false, false); + $codeCoverage = $this->getCoverageForFilesUseIgnoreCoversAnnotation($isUseIgnoreCoversAnnotation); + + $this->assertStringMatchesFormatFile($file, str_replace(PHP_EOL, "\n", $text->process($codeCoverage))); + } + + private function ignoreCoversAnnotationProvider(): array + { + return [ + 'with' => [true, TEST_FILES_PATH . 'BankAccountWithUncoveredAndIgnoreCoversAnnotation-text-line.txt'], + 'without' => [false, TEST_FILES_PATH . 'BankAccountWithUncovered-text-line.txt'], + 'default' => [null, TEST_FILES_PATH . 'BankAccountWithUncovered-text-line.txt'], + ]; + } }