Skip to content

Commit

Permalink
Fix sebastianbergmann#3701 by respecting forceCoversAnnotation before…
Browse files Browse the repository at this point in the history
… test
  • Loading branch information
andrewnicols committed May 22, 2019
1 parent 24e7530 commit 00ea7b1
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
15 changes: 15 additions & 0 deletions src/Framework/TestCase.php
Expand Up @@ -84,6 +84,11 @@ abstract class TestCase extends Assert implements SelfDescribing, Test
*/
protected $preserveGlobalState = true;

/**
* @var bool
*/
protected $forceCoversAnnotation = true;

/**
* @var bool
*/
Expand Down Expand Up @@ -1046,6 +1051,16 @@ public function isInIsolation(): bool
return $this->inIsolation;
}

public function setForceCoversAnnotation(bool $forceCoversAnnotation): void
{
$this->forceCoversAnnotation = $forceCoversAnnotation;
}

public function getForceCoversAnnotation(): bool
{
return $this->forceCoversAnnotation;
}

public function getResult()
{
return $this->testResult;
Expand Down
11 changes: 11 additions & 0 deletions src/Framework/TestSuite.php
Expand Up @@ -40,6 +40,11 @@ class TestSuite implements \IteratorAggregate, SelfDescribing, Test
*/
protected $runTestInSeparateProcess = false;

/**
* @var bool
*/
protected $forceCoversAnnotation = false;

/**
* The name of the test suite.
*
Expand Down Expand Up @@ -542,6 +547,7 @@ public function run(TestResult $result = null): TestResult
$test->setBackupGlobals($this->backupGlobals);
$test->setBackupStaticAttributes($this->backupStaticAttributes);
$test->setRunTestInSeparateProcess($this->runTestInSeparateProcess);
$test->setForceCoversAnnotation($this->forceCoversAnnotation);
}

$test->run($result);
Expand Down Expand Up @@ -577,6 +583,11 @@ public function setRunTestInSeparateProcess(bool $runTestInSeparateProcess): voi
$this->runTestInSeparateProcess = $runTestInSeparateProcess;
}

public function setForceCoversAnnotation(bool $forceCoversAnnotation): void
{
$this->forceCoversAnnotation = $forceCoversAnnotation;
}

public function setName(string $name): void
{
$this->name = $name;
Expand Down
8 changes: 6 additions & 2 deletions src/Util/Test.php
Expand Up @@ -162,14 +162,18 @@ public static function requiresCodeCoverageDataCollection(TestCase $test): bool
return true;
}

// If there is no @covers annotation and forceCoversAnnotation is set,
// then code coverage data does not need to be collected.
if ($test->getForceCoversAnnotation()) {
return false;
}

// 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;
}

Expand Down
16 changes: 11 additions & 5 deletions tests/unit/Util/TestTest.php
Expand Up @@ -1328,11 +1328,12 @@ public function testCoversAnnotationIncludesTraitsUsedByClass(): void
/**
* @dataProvider canSkipCoverageProvider
*/
public function testCanSkipCoverage($testCase, $expectedCanSkip): void
public function testCanSkipCoverage($testCase, $expectedCanSkip, $forceCoversAnnotation): void
{
require_once TEST_FILES_PATH . $testCase . '.php';

$test = new $testCase('testSomething');
$test->setForceCoversAnnotation($forceCoversAnnotation);
$coverageRequired = Test::requiresCodeCoverageDataCollection($test);
$canSkipCoverage = !$coverageRequired;

Expand All @@ -1342,10 +1343,15 @@ public function testCanSkipCoverage($testCase, $expectedCanSkip): void
public function canSkipCoverageProvider(): array
{
return [
['CoverageClassWithoutAnnotationsTest', false],
['CoverageClassTest', false],
['CoverageNothingTest', false],
['CoverageCoversOverridesCoversNothingTest', false],
['CoverageClassTest', false, false],
['CoverageClassWithoutAnnotationsTest', false, false],
['CoverageNothingTest', false, false],
['CoverageCoversOverridesCoversNothingTest', false, false],

['CoverageClassTest', false, true],
['CoverageClassWithoutAnnotationsTest', true, true],
['CoverageNothingTest', false, true],
['CoverageCoversOverridesCoversNothingTest', false, true],
];
}

Expand Down

0 comments on commit 00ea7b1

Please sign in to comment.