Skip to content

Commit 1ad62f2

Browse files
Merge branch '7.2'
2 parents b2f113c + 49a4028 commit 1ad62f2

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

ChangeLog-7.2.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ All notable changes of the PHPUnit 7.2 release series are documented in this fil
77
### Fixed
88

99
* Fixed [#3218](https://github.com/sebastianbergmann/phpunit/issues/3218): `prefix` attribute for `directory` node missing from `phpunit.xml` XSD
10+
* Fixed [#3222](https://github.com/sebastianbergmann/phpunit/pull/3222): Priority of `@covers` and `@coversNothing` is wrong
1011

1112
## [7.2.7] - 2018-07-15
1213

src/Framework/TestResult.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,25 @@ class TestResult implements Countable
189189
*/
190190
private $registerMockObjectsFromTestArgumentsRecursively = false;
191191

192+
public static function isAnyCoverageRequired(TestCase $test)
193+
{
194+
$annotations = $test->getAnnotations();
195+
196+
// If any methods have covers, coverage must me generated
197+
if (isset($annotations['method']['covers'])) {
198+
return true;
199+
}
200+
201+
// If there are no explicit covers, and the test class is
202+
// marked as covers nothing, all coverage can be skipped
203+
if (isset($annotations['class']['coversNothing'])) {
204+
return false;
205+
}
206+
207+
// Otherwise each test method can generate coverage
208+
return true;
209+
}
210+
192211
/**
193212
* Registers a TestListener.
194213
*/
@@ -571,11 +590,7 @@ public function run(Test $test): void
571590
$this->registerMockObjectsFromTestArgumentsRecursively
572591
);
573592

574-
$annotations = $test->getAnnotations();
575-
576-
if (isset($annotations['class']['coversNothing']) || isset($annotations['method']['coversNothing'])) {
577-
$coversNothing = true;
578-
}
593+
$isAnyCoverageRequired = self::isAnyCoverageRequired($test);
579594
}
580595

581596
$error = false;
@@ -604,7 +619,7 @@ public function run(Test $test): void
604619

605620
$collectCodeCoverage = $this->codeCoverage !== null &&
606621
!$test instanceof WarningTestCase &&
607-
!$coversNothing;
622+
$isAnyCoverageRequired;
608623

609624
if ($collectCodeCoverage) {
610625
$this->codeCoverage->start($test);

tests/Framework/TestResultTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,25 @@ public function testAddErrorOfTypeIncompleteTest()
6262
$this->assertAttributeEquals(true, 'lastTestFailed', $result);
6363
$this->assertAttributeContainsOnly(TestFailure::class, 'notImplemented', $result);
6464
}
65+
66+
public function canSkipCoverageProvider()
67+
{
68+
return [
69+
['CoverageClassTest', true],
70+
['CoverageNothingTest', true],
71+
['CoverageCoversOverridesCoversNothingTest', false],
72+
];
73+
}
74+
75+
/**
76+
* @dataProvider canSkipCoverageProvider
77+
*/
78+
public function testCanSkipCoverage($testCase, $expectedCanSkip)
79+
{
80+
require_once __DIR__ . '/../_files/' . $testCase . '.php';
81+
82+
$test = new $testCase();
83+
$canSkipCoverage = TestResult::isAnyCoverageRequired($test);
84+
$this->assertEquals($expectedCanSkip, $canSkipCoverage);
85+
}
6586
}

0 commit comments

Comments
 (0)