Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed May 21, 2019
1 parent a79e18f commit 35253e0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 57 deletions.
22 changes: 2 additions & 20 deletions src/Framework/TestResult.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -619,7 +601,7 @@ public function run(Test $test): void
$this->registerMockObjectsFromTestArgumentsRecursively
);

$isAnyCoverageRequired = self::isAnyCoverageRequired($test);
$isAnyCoverageRequired = TestUtil::requiresCodeCoverageDataCollection($test);
}

$error = false;
Expand Down
21 changes: 21 additions & 0 deletions src/Util/Test.php
Expand Up @@ -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.
*
Expand Down
37 changes: 0 additions & 37 deletions tests/unit/Framework/TestResultTest.php

This file was deleted.

22 changes: 22 additions & 0 deletions tests/unit/Util/TestTest.php
Expand Up @@ -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) {
Expand Down

0 comments on commit 35253e0

Please sign in to comment.