From 8604ff365fb0f82902b2fec7f5be3eb5015ece8b Mon Sep 17 00:00:00 2001 From: Doug Wright Date: Fri, 24 Sep 2021 21:28:46 +0100 Subject: [PATCH] #864 Uncovered files should be ignored unless requested --- src/CodeCoverage.php | 2 + src/ProcessedCodeCoverageData.php | 12 ++ tests/TestCase.php | 124 ++++++++++++++++++ .../BankAccountWithUncovered-text-line.txt | 12 ++ .../BankAccountWithoutUncovered-text-line.txt | 12 ++ tests/tests/TextTest.php | 20 +++ 6 files changed, 182 insertions(+) create mode 100644 tests/_files/BankAccountWithUncovered-text-line.txt create mode 100644 tests/_files/BankAccountWithoutUncovered-text-line.txt diff --git a/src/CodeCoverage.php b/src/CodeCoverage.php index 6445c6d27..553ced552 100644 --- a/src/CodeCoverage.php +++ b/src/CodeCoverage.php @@ -167,6 +167,8 @@ public function getData(bool $raw = false): ProcessedCodeCoverageData $this->processUncoveredFilesFromFilter(); } elseif ($this->includeUncoveredFiles) { $this->addUncoveredFilesFromFilter(); + } else { + $this->data->removeFilesWithNoCoverage(); } } diff --git a/src/ProcessedCodeCoverageData.php b/src/ProcessedCodeCoverageData.php index 1ed29ad52..9ecb536d9 100644 --- a/src/ProcessedCodeCoverageData.php +++ b/src/ProcessedCodeCoverageData.php @@ -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) { diff --git a/tests/TestCase.php b/tests/TestCase.php index 0f48c1794..4a75f7cba 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -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; + } } diff --git a/tests/_files/BankAccountWithUncovered-text-line.txt b/tests/_files/BankAccountWithUncovered-text-line.txt new file mode 100644 index 000000000..c80e703db --- /dev/null +++ b/tests/_files/BankAccountWithUncovered-text-line.txt @@ -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) diff --git a/tests/_files/BankAccountWithoutUncovered-text-line.txt b/tests/_files/BankAccountWithoutUncovered-text-line.txt new file mode 100644 index 000000000..892d83464 --- /dev/null +++ b/tests/_files/BankAccountWithoutUncovered-text-line.txt @@ -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) diff --git a/tests/tests/TextTest.php b/tests/tests/TextTest.php index 7c63c4ff1..241d992c6 100644 --- a/tests/tests/TextTest.php +++ b/tests/tests/TextTest.php @@ -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())) + ); + } }