Skip to content

Commit

Permalink
Fixed: Handling of coverage with ignore annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Pichler committed Sep 9, 2014
1 parent d0d0a78 commit 3e67aa2
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 10 deletions.
4 changes: 4 additions & 0 deletions src/main/php/PDepend/Util/Coverage/CloverReport.php
Expand Up @@ -125,6 +125,10 @@ public function getCoverage(AbstractASTArtifact $artifact)
++$executed;
}
}

if (0 === $executed && 1 === $executable && 0 < ($endLine - $startLine)) {
return 100;
}
if ($executed === 0) {
return 0;
}
Expand Down
48 changes: 38 additions & 10 deletions src/test/php/PDepend/Util/Coverage/CloverReportTest.php
Expand Up @@ -63,7 +63,7 @@ class CloverReportTest extends AbstractTest
public function testReportReturnsExpected0PercentCoverage()
{
$report = $this->_createCloverReport();
$coverage = $report->getCoverage($this->_createMethodMock(__FUNCTION__));
$coverage = $report->getCoverage($this->createMethodMock(__FUNCTION__));

$this->assertEquals(0, $coverage);
}
Expand All @@ -76,7 +76,7 @@ public function testReportReturnsExpected0PercentCoverage()
public function testReportReturnsExpected50PercentCoverage()
{
$report = $this->_createCloverReport();
$coverage = $report->getCoverage($this->_createMethodMock(__FUNCTION__));
$coverage = $report->getCoverage($this->createMethodMock(__FUNCTION__));

$this->assertEquals(50, $coverage);
}
Expand All @@ -89,11 +89,37 @@ public function testReportReturnsExpected50PercentCoverage()
public function testReportReturnsExpected100PercentCoverage()
{
$report = $this->_createCloverReport();
$coverage = $report->getCoverage($this->_createMethodMock(__FUNCTION__));
$coverage = $report->getCoverage($this->createMethodMock(__FUNCTION__));

$this->assertEquals(100, $coverage);
}

/**
* testReportReturnsExpected100PercentCoverageWithCoverageIgnore
*
* @return void
*/
public function testReportReturnsExpected100PercentCoverageWithCoverageIgnore()
{
$report = $this->_createCloverReport();
$coverage = $report->getCoverage($this->createMethodMock(__FUNCTION__));

$this->assertEquals(100, $coverage);
}

/**
* testReportReturnsExpected0PercentCoverageForOneLineMethod
*
* @return void
*/
public function testReportReturnsExpected0PercentCoverageForOneLineMethod()
{
$report = $this->_createCloverReport();
$coverage = $report->getCoverage($this->createMethodMock(__FUNCTION__));

$this->assertEquals(0, $coverage);
}

/**
* testNamespacedReportReturnsExpected0PercentCoverage
*
Expand All @@ -102,7 +128,7 @@ public function testReportReturnsExpected100PercentCoverage()
public function testNamespacedReportReturnsExpected0PercentCoverage()
{
$report = $this->_createNamespacedCloverReport();
$coverage = $report->getCoverage($this->_createMethodMock(__FUNCTION__));
$coverage = $report->getCoverage($this->createMethodMock(__FUNCTION__));

$this->assertEquals(0, $coverage);
}
Expand All @@ -115,7 +141,7 @@ public function testNamespacedReportReturnsExpected0PercentCoverage()
public function testNamespacedReportReturnsExpected50PercentCoverage()
{
$report = $this->_createNamespacedCloverReport();
$coverage = $report->getCoverage($this->_createMethodMock(__FUNCTION__));
$coverage = $report->getCoverage($this->createMethodMock(__FUNCTION__));

$this->assertEquals(50, $coverage);
}
Expand All @@ -128,7 +154,7 @@ public function testNamespacedReportReturnsExpected50PercentCoverage()
public function testNamespacedReportReturnsExpected100PercentCoverage()
{
$report = $this->_createNamespacedCloverReport();
$coverage = $report->getCoverage($this->_createMethodMock(__FUNCTION__));
$coverage = $report->getCoverage($this->createMethodMock(__FUNCTION__));

$this->assertEquals(100, $coverage);
}
Expand All @@ -141,7 +167,7 @@ public function testNamespacedReportReturnsExpected100PercentCoverage()
public function testGetCoverageReturnsZeroCoverageWhenNoMatchingEntryExists()
{
$report = $this->_createCloverReport();
$coverage = $report->getCoverage($this->_createMethodMock(__FUNCTION__));
$coverage = $report->getCoverage($this->createMethodMock(__FUNCTION__));

$this->assertEquals(0, $coverage);
}
Expand Down Expand Up @@ -172,9 +198,11 @@ private function _createNamespacedCloverReport()
* Creates a mocked method instance.
*
* @param string $name Name of the mock method.
* @param int $startLine
* @param int $endLine
* @return \PDepend\Source\AST\ASTMethod
*/
private function _createMethodMock($name)
private function createMethodMock($name, $startLine = 1, $endLine = 4)
{
$file = $this->getMock('\\PDepend\\Source\\AST\\ASTCompilationUnit', array(), array(null));
$file->expects($this->any())
Expand All @@ -187,10 +215,10 @@ private function _createMethodMock($name)
->will($this->returnValue($file));
$method->expects($this->once())
->method('getStartLine')
->will($this->returnValue(1));
->will($this->returnValue($startLine));
$method->expects($this->once())
->method('getEndLine')
->will($this->returnValue(4));
->will($this->returnValue($endLine));

return $method;
}
Expand Down
12 changes: 12 additions & 0 deletions src/test/php/PDepend/Util/Coverage/_files/clover.xml
Expand Up @@ -27,5 +27,17 @@
<line num="3" type="stmt" count="1"/>
<line num="4" type="stmt" count="1"/>
</file>
<file name="/testReportReturnsExpected100PercentCoverageWithCoverageIgnore.php">
<class name="testReportReturnsExpected100PercentCoverage" fullPackage="Package_foo" category="PHP" package="Package_foo">
<metrics methods="4" coveredmethods="4" statements="15" coveredstatements="15" elements="19" coveredelements="19"/>
</class>
<line num="1" type="method" name="foo" count="0"/>
</file>
<file name="testReportReturnsExpected0PercentCoverageForOneLineMethod.php">
<class name="testReportReturnsExpected100PercentCoverage" fullPackage="Package_foo" category="PHP" package="Package_foo">
<metrics methods="4" coveredmethods="4" statements="15" coveredstatements="15" elements="19" coveredelements="19"/>
</class>
<line num="1" type="method" name="foo" count="0"/>
</file>
</project>
</coverage>

0 comments on commit 3e67aa2

Please sign in to comment.