Skip to content

Commit

Permalink
Added optional check for missing @covers annotation when the usage …
Browse files Browse the repository at this point in the history
…of `@covers` annotations is forced
  • Loading branch information
sebastianbergmann committed Feb 8, 2016
1 parent 92f5c61 commit 9c3582b
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 1 deletion.
12 changes: 12 additions & 0 deletions ChangeLog-3.2.md
@@ -0,0 +1,12 @@
# Changes in PHP_CodeCoverage 3.2

All notable changes of the PHP_CodeCoverage 3.2 release series are documented in this file using the [Keep a CHANGELOG](http://keepachangelog.com/) principles.

## [3.2.0] - 2016-MM-DD

### Added

* Added optional check for missing `@covers` annotation when the usage of `@covers` annotations is forced

[3.2.0]: https://github.com/sebastianbergmann/php-code-coverage/compare/3.1...3.2.0

2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -44,7 +44,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "3.1.x-dev"
"dev-master": "3.2.x-dev"
}
}
}
27 changes: 27 additions & 0 deletions src/CodeCoverage.php
Expand Up @@ -47,6 +47,11 @@ class PHP_CodeCoverage
*/
private $checkForUnexecutedCoveredCode = false;

/**
* @var bool
*/
private $checkForMissingCoversAnnotation = false;

/**
* @var bool
*/
Expand Down Expand Up @@ -427,6 +432,23 @@ public function setForceCoversAnnotation($flag)
$this->forceCoversAnnotation = $flag;
}

/**
* @param bool $flag
* @throws PHP_CodeCoverage_InvalidArgumentException
* @since Method available since Release 3.2.0
*/
public function setCheckForMissingCoversAnnotation($flag)
{
if (!is_bool($flag)) {
throw PHP_CodeCoverage_InvalidArgumentException::create(
1,
'boolean'
);
}

$this->checkForMissingCoversAnnotation = $flag;
}

/**
* @param bool $flag
* @throws PHP_CodeCoverage_InvalidArgumentException
Expand Down Expand Up @@ -523,12 +545,17 @@ public function setIgnoreDeprecatedCode($flag)
* @param array $data
* @param mixed $linesToBeCovered
* @param array $linesToBeUsed
* @throws PHP_CodeCoverage_MissingCoversAnnotationException
* @throws PHP_CodeCoverage_UnintentionallyCoveredCodeException
*/
private function applyCoversAnnotationFilter(array &$data, $linesToBeCovered, array $linesToBeUsed)
{
if ($linesToBeCovered === false ||
($this->forceCoversAnnotation && empty($linesToBeCovered))) {
if ($this->checkForMissingCoversAnnotation) {
throw new PHP_CodeCoverage_MissingCoversAnnotationException;
}

$data = [];

return;
Expand Down
18 changes: 18 additions & 0 deletions src/CodeCoverage/Exception/MissingCoversAnnotationException.php
@@ -0,0 +1,18 @@
<?php
/*
* This file is part of the PHP_CodeCoverage package.
*
* (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.
*/

/**
* Exception that is raised when @covers must be used but is not.
*
* @since Class available since Release 3.2.0
*/
class PHP_CodeCoverage_MissingCoversAnnotationException extends PHP_CodeCoverage_RuntimeException
{
}
22 changes: 22 additions & 0 deletions tests/PHP/CodeCoverageTest.php
Expand Up @@ -149,6 +149,28 @@ public function testSetForceCoversAnnotationThrowsExceptionForInvalidArgument()
$this->coverage->setForceCoversAnnotation(null);
}

/**
* @covers PHP_CodeCoverage::setCheckForMissingCoversAnnotation
*/
public function testSetCheckForMissingCoversAnnotation()
{
$this->coverage->setCheckForMissingCoversAnnotation(true);
$this->assertAttributeEquals(
true,
'checkForMissingCoversAnnotation',
$this->coverage
);
}

/**
* @covers PHP_CodeCoverage::setCheckForMissingCoversAnnotation
* @expectedException PHP_CodeCoverage_Exception
*/
public function testSetCheckForMissingCoversAnnotationThrowsExceptionForInvalidArgument()
{
$this->coverage->setCheckForMissingCoversAnnotation(null);
}

/**
* @covers PHP_CodeCoverage::setForceCoversAnnotation
*/
Expand Down

0 comments on commit 9c3582b

Please sign in to comment.