Skip to content

Commit

Permalink
Fix sebastianbergmann#3697 Respect @coversNothing at coverage driver …
Browse files Browse the repository at this point in the history
…start
  • Loading branch information
andrewnicols committed May 21, 2019
1 parent 41a5260 commit 86cb787
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 14 deletions.
18 changes: 12 additions & 6 deletions src/Util/Test.php
Expand Up @@ -162,12 +162,18 @@ public static function requiresCodeCoverageDataCollection(TestCase $test): bool
return true;
}

// If there is no @covers annotation but a @coversNothing annotation
// then code coverage data does not need to be collected
// If there is no @covers annotation but a @coversNothing annotation on
// the test class then code coverage data does not need to be collected
if (isset($annotations['class']['coversNothing'])) {
return false;
}

// If there is no @covers annotation but a @coversNothing annotation on
// the test method then code coverage data does not need to be collected
if (isset($annotations['method']['coversNothing'])) {
return false;
}

// If there is no @coversNothing annotation then
// code coverage data may be collected
return true;
Expand Down Expand Up @@ -1270,14 +1276,14 @@ private static function sanitizeVersionNumber(string $version)

private static function shouldCoversAnnotationBeUsed(array $annotations): bool
{
if (isset($annotations['method']['coversNothing'])) {
return false;
}

if (isset($annotations['method']['covers'])) {
return true;
}

if (isset($annotations['method']['coversNothing'])) {
return false;
}

if (isset($annotations['class']['coversNothing'])) {
return false;
}
Expand Down
Expand Up @@ -9,7 +9,7 @@
*/
use PHPUnit\Framework\TestCase;

class CoverageNothingTest extends TestCase
class CoverageCoversOverridesCoversNothingSameDocblockTest extends TestCase
{
/**
* @covers CoveredClass::publicMethod
Expand Down
22 changes: 22 additions & 0 deletions tests/_files/CoverageNothingClassTest.php
@@ -0,0 +1,22 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use PHPUnit\Framework\TestCase;

/**
* @coversNothing
*/
class CoverageNothingClassTest extends TestCase
{
public function testSomething(): void
{
$o = new CoveredClass;
$o->publicMethod();
}
}
22 changes: 22 additions & 0 deletions tests/_files/CoverageNothingMethodTest.php
@@ -0,0 +1,22 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use PHPUnit\Framework\TestCase;

class CoverageNothingMethodTest extends TestCase
{
/**
* @coversNothing
*/
public function testSomething(): void
{
$o = new CoveredClass;
$o->publicMethod();
}
}
28 changes: 21 additions & 7 deletions tests/unit/Util/TestTest.php
Expand Up @@ -1065,11 +1065,15 @@ public function testGetLinesToBeCovered($test, $lines): void
$expected = [
TEST_FILES_PATH . 'NamespaceCoveredClass.php' => $lines,
];
} elseif ($test === 'CoverageCoversOverridesCoversNothingSameDocblockTest') {
$expected = [TEST_FILES_PATH . 'CoveredClass.php' => $lines];
} elseif ($test === 'CoverageCoversOverridesCoversNothingTest') {
$expected = [TEST_FILES_PATH . 'CoveredClass.php' => $lines];
} elseif ($test === 'CoverageNoneTest') {
$expected = [];
} elseif ($test === 'CoverageNothingTest') {
} elseif ($test === 'CoverageNothingMethodTest') {
$expected = false;
} elseif ($test === 'CoverageNothingClassTest') {
$expected = false;
} elseif ($test === 'CoverageFunctionTest') {
$expected = [
Expand Down Expand Up @@ -1292,13 +1296,21 @@ public function getLinesToBeCoveredProvider(): array
\range(31, 35),
],
[
'CoverageNothingTest',
'CoverageNothingClassTest',
false,
],
[
'CoverageNothingMethodTest',
false,
],
[
'CoverageCoversOverridesCoversNothingTest',
\range(29, 33),
],
[
'CoverageCoversOverridesCoversNothingSameDocblockTest',
\range(29, 33),
],
];
}

Expand Down Expand Up @@ -1332,18 +1344,20 @@ public function testCanSkipCoverage($testCase, $expectedCanSkip): void
{
require_once TEST_FILES_PATH . $testCase . '.php';

$test = new $testCase;
$canSkipCoverage = Test::requiresCodeCoverageDataCollection($test);
$test = new $testCase('testSomething');
$coverageRequired = Test::requiresCodeCoverageDataCollection($test);

$this->assertEquals($expectedCanSkip, $canSkipCoverage);
$this->assertEquals($expectedCanSkip, !$coverageRequired);
}

public function canSkipCoverageProvider(): array
{
return [
['CoverageClassTest', true],
['CoverageNothingTest', true],
['CoverageClassTest', false],
['CoverageCoversOverridesCoversNothingSameDocblockTest', false],
['CoverageCoversOverridesCoversNothingTest', false],
['CoverageNothingClassTest', true],
['CoverageNothingMethodTest', true],
];
}

Expand Down

0 comments on commit 86cb787

Please sign in to comment.