Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport of 3697 Respect @coversNothing at coverage driver start #3709

Merged
merged 2 commits into from May 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Framework/TestResult.php
Expand Up @@ -198,6 +198,12 @@ public static function isAnyCoverageRequired(TestCase $test)
{
$annotations = $test->getAnnotations();

// If there is 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 any methods have covers, coverage must me generated
if (isset($annotations['method']['covers'])) {
return true;
Expand Down
22 changes: 22 additions & 0 deletions tests/_files/CoverageClassNothingTest.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 CoverageClassNothingTest extends TestCase
{
public function testSomething(): void
{
$o = new CoveredClass;
$o->publicMethod();
}
}
19 changes: 19 additions & 0 deletions tests/_files/CoverageClassWithoutAnnotationsTest.php
@@ -0,0 +1,19 @@
<?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 CoverageClassWithoutAnnotationsTest extends TestCase
{
public function testSomething(): void
{
$o = new CoveredClass;
$o->publicMethod();
}
}
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
Expand All @@ -9,7 +9,7 @@
*/
use PHPUnit\Framework\TestCase;

class CoverageNothingTest extends TestCase
class CoverageMethodNothingCoversMethod extends TestCase
{
/**
* @covers CoveredClass::publicMethod
Expand Down
22 changes: 22 additions & 0 deletions tests/_files/CoverageMethodNothingTest.php
@@ -0,0 +1,22 @@
<?php
/*
* 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 CoverageMethodNothingTest extends TestCase
{
/**
* @coversNothing
*/
public function testSomething(): void
{
$o = new CoveredClass;
$o->publicMethod();
}
}
12 changes: 8 additions & 4 deletions tests/unit/Framework/TestResultTest.php
Expand Up @@ -66,9 +66,11 @@ public function testAddErrorOfTypeIncompleteTest(): void
public function canSkipCoverageProvider(): array
{
return [
['CoverageClassTest', true],
['CoverageNothingTest', true],
['CoverageClassTest', false],
['CoverageClassWithoutAnnotationsTest', false],
['CoverageCoversOverridesCoversNothingTest', false],
['CoverageClassNothingTest', true],
['CoverageMethodNothingTest', true],
];
}

Expand All @@ -79,8 +81,10 @@ public function testCanSkipCoverage($testCase, $expectedCanSkip): void
{
require_once TEST_FILES_PATH . $testCase . '.php';

$test = new $testCase;
$canSkipCoverage = TestResult::isAnyCoverageRequired($test);
$test = new $testCase('testSomething');
$coverageRequired = TestResult::isAnyCoverageRequired($test);
$canSkipCoverage = !$coverageRequired;

$this->assertEquals($expectedCanSkip, $canSkipCoverage);
}
}
16 changes: 14 additions & 2 deletions tests/unit/Util/TestTest.php
Expand Up @@ -782,11 +782,15 @@ public function testGetLinesToBeCovered($test, $lines): void
$expected = [
TEST_FILES_PATH . 'NamespaceCoveredClass.php' => $lines,
];
} elseif ($test === 'CoverageMethodNothingCoversMethod') {
$expected = false;
} elseif ($test === 'CoverageCoversOverridesCoversNothingTest') {
$expected = [TEST_FILES_PATH . 'CoveredClass.php' => $lines];
} elseif ($test === 'CoverageNoneTest') {
$expected = [];
} elseif ($test === 'CoverageNothingTest') {
} elseif ($test === 'CoverageClassNothingTest') {
$expected = false;
} elseif ($test === 'CoverageMethodNothingTest') {
$expected = false;
} elseif ($test === 'CoverageFunctionTest') {
$expected = [
Expand Down Expand Up @@ -1009,13 +1013,21 @@ public function getLinesToBeCoveredProvider(): array
\range(31, 35),
],
[
'CoverageNothingTest',
'CoverageClassNothingTest',
false,
],
[
'CoverageMethodNothingTest',
false,
],
[
'CoverageCoversOverridesCoversNothingTest',
\range(29, 33),
],
[
'CoverageMethodNothingCoversMethod',
false,
],
];
}

Expand Down