diff --git a/src/Framework/TestResult.php b/src/Framework/TestResult.php index e315b97c985..10c4265100f 100644 --- a/src/Framework/TestResult.php +++ b/src/Framework/TestResult.php @@ -16,6 +16,7 @@ use PHPUnit\Util\Blacklist; use PHPUnit\Util\ErrorHandler; use PHPUnit\Util\Printer; +use PHPUnit\Util\Test as TestUtil; use SebastianBergmann\CodeCoverage\CodeCoverage; use SebastianBergmann\CodeCoverage\CoveredCodeNotExecutedException as OriginalCoveredCodeNotExecutedException; use SebastianBergmann\CodeCoverage\Exception as OriginalCodeCoverageException; @@ -196,25 +197,6 @@ final class TestResult implements Countable */ private $registerMockObjectsFromTestArgumentsRecursively = false; - public static function isAnyCoverageRequired(TestCase $test): bool - { - $annotations = $test->getAnnotations(); - - // If any methods have covers, coverage must me generated - if (isset($annotations['method']['covers'])) { - return true; - } - - // If there are no explicit covers, and the test class is - // marked as covers nothing, all coverage can be skipped - if (isset($annotations['class']['coversNothing'])) { - return false; - } - - // Otherwise each test method can generate coverage - return true; - } - /** * @deprecated Use the `TestHook` interfaces instead * @@ -619,7 +601,7 @@ public function run(Test $test): void $this->registerMockObjectsFromTestArgumentsRecursively ); - $isAnyCoverageRequired = self::isAnyCoverageRequired($test); + $isAnyCoverageRequired = TestUtil::requiresCodeCoverageDataCollection($test); } $error = false; diff --git a/src/Util/Test.php b/src/Util/Test.php index 5697b1c0ce8..8d9a89a1333 100644 --- a/src/Util/Test.php +++ b/src/Util/Test.php @@ -158,6 +158,27 @@ public static function getLinesToBeUsed(string $className, string $methodName): return self::getLinesToBeCoveredOrUsed($className, $methodName, 'uses'); } + public static function requiresCodeCoverageDataCollection(TestCase $test): bool + { + $annotations = $test->getAnnotations(); + + // If there is at least one @covers annotation then + // code coverage data needs to be collected + if (isset($annotations['method']['covers'])) { + return true; + } + + // If there is no @covers annotation but a @coversNothing annotation + // then code coverage data does not need to be collected + if (isset($annotations['class']['coversNothing'])) { + return false; + } + + // If there is no @coversNothing annotation then + // code coverage data may be collected + return true; + } + /** * Returns the requirements for a test. * diff --git a/tests/unit/Framework/TestResultTest.php b/tests/unit/Framework/TestResultTest.php deleted file mode 100644 index 0a4f87c9ad9..00000000000 --- a/tests/unit/Framework/TestResultTest.php +++ /dev/null @@ -1,37 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace PHPUnit\Framework; - -/** - * @small - */ -final class TestResultTest extends TestCase -{ - /** - * @dataProvider canSkipCoverageProvider - */ - public function testCanSkipCoverage($testCase, $expectedCanSkip): void - { - require_once TEST_FILES_PATH . $testCase . '.php'; - - $test = new $testCase; - $canSkipCoverage = TestResult::isAnyCoverageRequired($test); - $this->assertEquals($expectedCanSkip, $canSkipCoverage); - } - - public function canSkipCoverageProvider(): array - { - return [ - ['CoverageClassTest', true], - ['CoverageNothingTest', true], - ['CoverageCoversOverridesCoversNothingTest', false], - ]; - } -} diff --git a/tests/unit/Util/TestTest.php b/tests/unit/Util/TestTest.php index 397c41c3aba..01b2058da94 100644 --- a/tests/unit/Util/TestTest.php +++ b/tests/unit/Util/TestTest.php @@ -1326,6 +1326,28 @@ public function testCoversAnnotationIncludesTraitsUsedByClass(): void ); } + /** + * @dataProvider canSkipCoverageProvider + */ + public function testCanSkipCoverage($testCase, $expectedCanSkip): void + { + require_once TEST_FILES_PATH . $testCase . '.php'; + + $test = new $testCase; + $canSkipCoverage = Test::requiresCodeCoverageDataCollection($test); + + $this->assertEquals($expectedCanSkip, $canSkipCoverage); + } + + public function canSkipCoverageProvider(): array + { + return [ + ['CoverageClassTest', true], + ['CoverageNothingTest', true], + ['CoverageCoversOverridesCoversNothingTest', false], + ]; + } + private function getRequirementsTestClassFile(): string { if (!$this->fileRequirementsTest) {