From 82af7ae4307ff7904ce6304243b8d51915b65477 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Wed, 22 May 2019 08:52:08 +0800 Subject: [PATCH] Fix #3701 by respecting forceCoversAnnotation before test --- src/Framework/TestCase.php | 15 +++++++++++++++ src/Framework/TestSuite.php | 11 +++++++++++ src/Util/Test.php | 8 ++++++-- tests/unit/Util/TestTest.php | 19 +++++++++++++------ 4 files changed, 45 insertions(+), 8 deletions(-) diff --git a/src/Framework/TestCase.php b/src/Framework/TestCase.php index d931d3a5ad3..7ed3cce4ae1 100644 --- a/src/Framework/TestCase.php +++ b/src/Framework/TestCase.php @@ -84,6 +84,11 @@ abstract class TestCase extends Assert implements SelfDescribing, Test */ protected $preserveGlobalState = true; + /** + * @var bool + */ + protected $forceCoversAnnotation = true; + /** * @var bool */ @@ -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; diff --git a/src/Framework/TestSuite.php b/src/Framework/TestSuite.php index f55c67fb539..86147f51e65 100644 --- a/src/Framework/TestSuite.php +++ b/src/Framework/TestSuite.php @@ -40,6 +40,11 @@ class TestSuite implements \IteratorAggregate, SelfDescribing, Test */ protected $runTestInSeparateProcess = false; + /** + * @var bool + */ + protected $forceCoversAnnotation = false; + /** * The name of the test suite. * @@ -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); @@ -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; diff --git a/src/Util/Test.php b/src/Util/Test.php index f8339ba8f82..2c28fae815e 100644 --- a/src/Util/Test.php +++ b/src/Util/Test.php @@ -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; } diff --git a/tests/unit/Util/TestTest.php b/tests/unit/Util/TestTest.php index d92ff7ddb7b..3225097cfc5 100644 --- a/tests/unit/Util/TestTest.php +++ b/tests/unit/Util/TestTest.php @@ -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; @@ -1342,11 +1343,17 @@ public function testCanSkipCoverage($testCase, $expectedCanSkip): void public function canSkipCoverageProvider(): array { return [ - ['CoverageClassTest', false], - ['CoverageClassNothingTest', true], - ['CoverageMethodNothingTest', false], - ['CoverageClassWithoutAnnotationsTest', false], - ['CoverageCoversOverridesCoversNothingTest', false], + ['CoverageClassTest', false, false], + ['CoverageClassNothingTest', true, false], + ['CoverageMethodNothingTest', false, false], + ['CoverageClassWithoutAnnotationsTest', false, false], + ['CoverageCoversOverridesCoversNothingTest', false, false], + + ['CoverageClassTest', false, true], + ['CoverageClassNothingTest', true, false], + ['CoverageMethodNothingTest', false, true], + ['CoverageClassWithoutAnnotationsTest', true, true], + ['CoverageCoversOverridesCoversNothingTest', false, true], ]; }