diff --git a/PHP/CodeCoverage/Util.php b/PHP/CodeCoverage/Util.php index 5a4673ccd..699cf7ad4 100644 --- a/PHP/CodeCoverage/Util.php +++ b/PHP/CodeCoverage/Util.php @@ -184,9 +184,11 @@ public static function getLinesToBeIgnored($filename, $cacheTokens = TRUE) $classes[$token->getName()]['methods'] ); - $lastMethod = array_pop( - $classes[$token->getName()]['methods'] - ); + do { + $lastMethod = array_pop( + $classes[$token->getName()]['methods'] + ); + } while ($lastMethod !== NULL && substr($lastMethod['signature'], 0, 18) == 'anonymous function'); if ($lastMethod === NULL) { $lastMethod = $firstMethod; diff --git a/Tests/PHP/CodeCoverage/FilterTest.php b/Tests/PHP/CodeCoverage/FilterTest.php index be8b71ee5..a6b4a9b6b 100644 --- a/Tests/PHP/CodeCoverage/FilterTest.php +++ b/Tests/PHP/CodeCoverage/FilterTest.php @@ -109,6 +109,7 @@ protected function setUp() TEST_FILES_PATH . 'NamespaceCoveragePublicTest.php', TEST_FILES_PATH . 'NamespaceCoveredClass.php', TEST_FILES_PATH . 'NotExistingCoveredElementTest.php', + TEST_FILES_PATH . 'source_with_class_and_anonymous_function.php', TEST_FILES_PATH . 'source_with_ignore.php', TEST_FILES_PATH . 'source_with_namespace.php', TEST_FILES_PATH . 'source_with_oneline_annotations.php', diff --git a/Tests/PHP/CodeCoverage/Report/CloverTest.php b/Tests/PHP/CodeCoverage/Report/CloverTest.php index 1008ebced..03c6dfd34 100644 --- a/Tests/PHP/CodeCoverage/Report/CloverTest.php +++ b/Tests/PHP/CodeCoverage/Report/CloverTest.php @@ -93,4 +93,17 @@ public function testCloverForFileWithIgnoredLines() $clover->process($this->getCoverageForFileWithIgnoredLines()) ); } + + /** + * @covers PHP_CodeCoverage_Report_Clover + */ + public function testCloverForClassWithAnonymousFunction() + { + $clover = new PHP_CodeCoverage_Report_Clover; + + $this->assertStringMatchesFormatFile( + TEST_FILES_PATH . 'class-with-anonymous-function-clover.xml', + $clover->process($this->getCoverageForClassWithAnonymousFunction()) + ); + } } diff --git a/Tests/PHP/CodeCoverage/UtilTest.php b/Tests/PHP/CodeCoverage/UtilTest.php index f23ad7236..1106de77d 100644 --- a/Tests/PHP/CodeCoverage/UtilTest.php +++ b/Tests/PHP/CodeCoverage/UtilTest.php @@ -159,6 +159,25 @@ public function testGetLinesToBeIgnored2() ); } + public function testGetLinesToBeIgnored3() + { + $this->assertEquals( + array( + 1 => TRUE, + 2 => TRUE, + 8 => TRUE, + 15 => TRUE, + 3 => TRUE, + 4 => TRUE, + 19 => TRUE, + 16 => TRUE + ), + PHP_CodeCoverage_Util::getLinesToBeIgnored( + TEST_FILES_PATH . 'source_with_class_and_anonymous_function.php' + ) + ); + } + /** * @covers PHP_CodeCoverage_Util::getLinesToBeIgnored */ diff --git a/Tests/TestCase.php b/Tests/TestCase.php index 6a04513f8..b250c1f82 100644 --- a/Tests/TestCase.php +++ b/Tests/TestCase.php @@ -263,4 +263,41 @@ protected function setUpXdebugStubForFileWithIgnoredLines() return $stub; } + + protected function getCoverageForClassWithAnonymousFunction() + { + $coverage = new PHP_CodeCoverage( + $this->setUpXdebugStubForClassWithAnonymousFunction(), + new PHP_CodeCoverage_Filter + ); + + $coverage->start('ClassWithAnonymousFunction', TRUE); + $coverage->stop(); + + return $coverage; + } + + protected function setUpXdebugStubForClassWithAnonymousFunction() + { + $stub = $this->getMock('PHP_CodeCoverage_Driver_Xdebug'); + $stub->expects($this->any()) + ->method('stop') + ->will($this->returnValue( + array( + TEST_FILES_PATH . 'source_with_class_and_anonymous_function.php' => array( + 7 => 1, + 9 => 1, + 10 => -1, + 11 => 1, + 12 => 1, + 13 => 1, + 14 => 1, + 17 => 1, + 18 => 1 + ) + ) + )); + + return $stub; + } } diff --git a/Tests/_files/class-with-anonymous-function-clover.xml b/Tests/_files/class-with-anonymous-function-clover.xml new file mode 100644 index 000000000..11e9c1856 --- /dev/null +++ b/Tests/_files/class-with-anonymous-function-clover.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/Tests/_files/source_with_class_and_anonymous_function.php b/Tests/_files/source_with_class_and_anonymous_function.php new file mode 100644 index 000000000..eaba387d6 --- /dev/null +++ b/Tests/_files/source_with_class_and_anonymous_function.php @@ -0,0 +1,19 @@ +