diff --git a/ChangeLog-7.2.md b/ChangeLog-7.2.md index 5fba7547d7f..8af396b0233 100644 --- a/ChangeLog-7.2.md +++ b/ChangeLog-7.2.md @@ -7,6 +7,7 @@ All notable changes of the PHPUnit 7.2 release series are documented in this fil ### Fixed * Fixed [#3218](https://github.com/sebastianbergmann/phpunit/issues/3218): `prefix` attribute for `directory` node missing from `phpunit.xml` XSD +* Fixed [#3222](https://github.com/sebastianbergmann/phpunit/pull/3222): Priority of `@covers` and `@coversNothing` is wrong ## [7.2.7] - 2018-07-15 diff --git a/src/Framework/TestResult.php b/src/Framework/TestResult.php index a85fb40193a..297645adeb1 100644 --- a/src/Framework/TestResult.php +++ b/src/Framework/TestResult.php @@ -189,6 +189,25 @@ class TestResult implements Countable */ private $registerMockObjectsFromTestArgumentsRecursively = false; + public static function isAnyCoverageRequired(TestCase $test) + { + $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; + } + /** * Registers a TestListener. */ @@ -571,11 +590,7 @@ public function run(Test $test): void $this->registerMockObjectsFromTestArgumentsRecursively ); - $annotations = $test->getAnnotations(); - - if (isset($annotations['class']['coversNothing']) || isset($annotations['method']['coversNothing'])) { - $coversNothing = true; - } + $isAnyCoverageRequired = self::isAnyCoverageRequired($test); } $error = false; @@ -604,7 +619,7 @@ public function run(Test $test): void $collectCodeCoverage = $this->codeCoverage !== null && !$test instanceof WarningTestCase && - !$coversNothing; + $isAnyCoverageRequired; if ($collectCodeCoverage) { $this->codeCoverage->start($test); diff --git a/tests/Framework/TestResultTest.php b/tests/Framework/TestResultTest.php index b88e03f36ff..ff8abbeb57c 100644 --- a/tests/Framework/TestResultTest.php +++ b/tests/Framework/TestResultTest.php @@ -62,4 +62,25 @@ public function testAddErrorOfTypeIncompleteTest() $this->assertAttributeEquals(true, 'lastTestFailed', $result); $this->assertAttributeContainsOnly(TestFailure::class, 'notImplemented', $result); } + + public function canSkipCoverageProvider() + { + return [ + ['CoverageClassTest', true], + ['CoverageNothingTest', true], + ['CoverageCoversOverridesCoversNothingTest', false], + ]; + } + + /** + * @dataProvider canSkipCoverageProvider + */ + public function testCanSkipCoverage($testCase, $expectedCanSkip) + { + require_once __DIR__ . '/../_files/' . $testCase . '.php'; + + $test = new $testCase(); + $canSkipCoverage = TestResult::isAnyCoverageRequired($test); + $this->assertEquals($expectedCanSkip, $canSkipCoverage); + } }