From 56a7d1f0a46f6834ceeb730fe87a2c13705f9068 Mon Sep 17 00:00:00 2001 From: Maks3w Date: Tue, 3 Nov 2015 15:49:50 +0100 Subject: [PATCH 1/9] DRY and fix run individual tests --- tests/PHP/CodeCoverage/FilterTest.php | 8 +------- tests/PHP/CodeCoverage/Report/CloverTest.php | 10 +--------- tests/PHP/CodeCoverage/Report/FactoryTest.php | 10 +--------- tests/PHP/CodeCoverage/UtilTest.php | 2 ++ tests/PHP/CodeCoverageTest.php | 12 +----------- tests/TestCase.php | 5 +++++ 6 files changed, 11 insertions(+), 36 deletions(-) diff --git a/tests/PHP/CodeCoverage/FilterTest.php b/tests/PHP/CodeCoverage/FilterTest.php index c0e59e0ba..f39b95cdf 100644 --- a/tests/PHP/CodeCoverage/FilterTest.php +++ b/tests/PHP/CodeCoverage/FilterTest.php @@ -8,13 +8,7 @@ * file that was distributed with this source code. */ -if (!defined('TEST_FILES_PATH')) { - define( - 'TEST_FILES_PATH', - dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . - '_files' . DIRECTORY_SEPARATOR - ); -} +require_once dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'TestCase.php'; /** * Tests for the PHP_CodeCoverage_Filter class. diff --git a/tests/PHP/CodeCoverage/Report/CloverTest.php b/tests/PHP/CodeCoverage/Report/CloverTest.php index 8d860bd2d..1b2174ee9 100644 --- a/tests/PHP/CodeCoverage/Report/CloverTest.php +++ b/tests/PHP/CodeCoverage/Report/CloverTest.php @@ -8,15 +8,7 @@ * file that was distributed with this source code. */ -if (!defined('TEST_FILES_PATH')) { - define( - 'TEST_FILES_PATH', - dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . - '_files' . DIRECTORY_SEPARATOR - ); -} - -require_once TEST_FILES_PATH . '../TestCase.php'; +require_once dirname(dirname(dirname(dirname(__FILE__)))) . DIRECTORY_SEPARATOR . 'TestCase.php'; /** * Tests for the PHP_CodeCoverage_Report_Clover class. diff --git a/tests/PHP/CodeCoverage/Report/FactoryTest.php b/tests/PHP/CodeCoverage/Report/FactoryTest.php index 4c15f5c72..43616095d 100644 --- a/tests/PHP/CodeCoverage/Report/FactoryTest.php +++ b/tests/PHP/CodeCoverage/Report/FactoryTest.php @@ -8,15 +8,7 @@ * file that was distributed with this source code. */ -if (!defined('TEST_FILES_PATH')) { - define( - 'TEST_FILES_PATH', - dirname(dirname(dirname(dirname(__FILE__)))) . DIRECTORY_SEPARATOR . - '_files' . DIRECTORY_SEPARATOR - ); -} - -require_once TEST_FILES_PATH . '../TestCase.php'; +require_once dirname(dirname(dirname(dirname(__FILE__)))) . DIRECTORY_SEPARATOR . 'TestCase.php'; /** * Tests for the PHP_CodeCoverage_Report_Factory class. diff --git a/tests/PHP/CodeCoverage/UtilTest.php b/tests/PHP/CodeCoverage/UtilTest.php index 0b4caea97..1cd8c20b8 100644 --- a/tests/PHP/CodeCoverage/UtilTest.php +++ b/tests/PHP/CodeCoverage/UtilTest.php @@ -8,6 +8,8 @@ * file that was distributed with this source code. */ +require_once dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'TestCase.php'; + /** * Tests for the PHP_CodeCoverage_Util class. * diff --git a/tests/PHP/CodeCoverageTest.php b/tests/PHP/CodeCoverageTest.php index d6c8aa40c..b1d43f7d9 100644 --- a/tests/PHP/CodeCoverageTest.php +++ b/tests/PHP/CodeCoverageTest.php @@ -8,17 +8,7 @@ * file that was distributed with this source code. */ -if (!defined('TEST_FILES_PATH')) { - define( - 'TEST_FILES_PATH', - dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . - '_files' . DIRECTORY_SEPARATOR - ); -} - -require_once TEST_FILES_PATH . '../TestCase.php'; -require_once TEST_FILES_PATH . 'BankAccount.php'; -require_once TEST_FILES_PATH . 'BankAccountTest.php'; +require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'TestCase.php'; /** * Tests for the PHP_CodeCoverage class. diff --git a/tests/TestCase.php b/tests/TestCase.php index 01b0a5714..f6bfb9d60 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -8,6 +8,10 @@ * file that was distributed with this source code. */ +if (!defined('TEST_FILES_PATH')) { + define('TEST_FILES_PATH', dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR); +} + /** * Abstract base class for test case classes. * @@ -70,6 +74,7 @@ protected function getXdebugDataForBankAccount() protected function getCoverageForBankAccount() { $data = $this->getXdebugDataForBankAccount(); + require_once TEST_FILES_PATH . 'BankAccountTest.php'; $stub = $this->getMock('PHP_CodeCoverage_Driver_Xdebug'); $stub->expects($this->any()) From cca6f7e1d7c17c1ce583952a38522d5facbe5b04 Mon Sep 17 00:00:00 2001 From: Maks3w Date: Tue, 3 Nov 2015 10:27:56 +0100 Subject: [PATCH 2/9] Tests for XML Report --- .gitignore | 1 + tests/PHP/CodeCoverage/Report/XMLTest.php | 108 ++++++++++++++++++ tests/TestCase.php | 7 ++ .../BankAccount.php.xml | 40 +++++++ .../XML/CoverageForBankAccount/index.xml | 29 +++++ .../index.xml | 26 +++++ ..._with_class_and_anonymous_function.php.xml | 41 +++++++ .../CoverageForFileWithIgnoredLines/index.xml | 26 +++++ .../source_with_ignore.php.xml | 28 +++++ 9 files changed, 306 insertions(+) create mode 100644 tests/PHP/CodeCoverage/Report/XMLTest.php create mode 100644 tests/_files/Report/XML/CoverageForBankAccount/BankAccount.php.xml create mode 100644 tests/_files/Report/XML/CoverageForBankAccount/index.xml create mode 100644 tests/_files/Report/XML/CoverageForClassWithAnonymousFunction/index.xml create mode 100644 tests/_files/Report/XML/CoverageForClassWithAnonymousFunction/source_with_class_and_anonymous_function.php.xml create mode 100644 tests/_files/Report/XML/CoverageForFileWithIgnoredLines/index.xml create mode 100644 tests/_files/Report/XML/CoverageForFileWithIgnoredLines/source_with_ignore.php.xml diff --git a/.gitignore b/.gitignore index b386531f0..4486ead43 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ build/logs build/pdepend cache.properties phpunit.xml +/tests/_files/tmp /vendor /composer.lock /composer.phar diff --git a/tests/PHP/CodeCoverage/Report/XMLTest.php b/tests/PHP/CodeCoverage/Report/XMLTest.php new file mode 100644 index 000000000..f35ef74bd --- /dev/null +++ b/tests/PHP/CodeCoverage/Report/XMLTest.php @@ -0,0 +1,108 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +require_once dirname(dirname(dirname(dirname(__FILE__)))) . DIRECTORY_SEPARATOR . 'TestCase.php'; + +/** + * Tests for the PHP_CodeCoverage_Report_XML class. + * + * @since Class available since Release 3.0.2 + */ +class PHP_CodeCoverage_Report_XMLTest extends PHP_CodeCoverage_TestCase +{ + static private $TEST_REPORT_PATH_SOURCE; + + public static function setUpBeforeClass() + { + parent::setUpBeforeClass(); + + self::$TEST_REPORT_PATH_SOURCE = TEST_FILES_PATH . 'Report' . DIRECTORY_SEPARATOR . 'XML'; + } + + protected function tearDown() + { + parent::tearDown(); + + $tmpFilesIterator = new FilesystemIterator(self::$TEST_TMP_PATH); + foreach ($tmpFilesIterator as $path => $fileInfo) { + /* @var SplFileInfo $fileInfo */ + unlink($fileInfo->getPathname()); + } + } + + /** + * @covers PHP_CodeCoverage_Report_XML + */ + public function testForBankAccountTest() + { + $expectedFilesPath = self::$TEST_REPORT_PATH_SOURCE . DIRECTORY_SEPARATOR . 'CoverageForBankAccount'; + + $xml = new PHP_CodeCoverage_Report_XML; + $xml->process($this->getCoverageForBankAccount(), self::$TEST_TMP_PATH); + + $this->assertFilesEquals($expectedFilesPath, self::$TEST_TMP_PATH); + } + + /** + * @covers PHP_CodeCoverage_Report_XML + */ + public function testForFileWithIgnoredLines() + { + $expectedFilesPath = self::$TEST_REPORT_PATH_SOURCE . DIRECTORY_SEPARATOR . 'CoverageForFileWithIgnoredLines'; + + $xml = new PHP_CodeCoverage_Report_XML; + $xml->process($this->getCoverageForFileWithIgnoredLines(), self::$TEST_TMP_PATH); + + $this->assertFilesEquals($expectedFilesPath, self::$TEST_TMP_PATH); + } + + /** + * @covers PHP_CodeCoverage_Report_XML + */ + public function testForClassWithAnonymousFunction() + { + $expectedFilesPath = + self::$TEST_REPORT_PATH_SOURCE . DIRECTORY_SEPARATOR . 'CoverageForClassWithAnonymousFunction'; + + $xml = new PHP_CodeCoverage_Report_XML; + $xml->process($this->getCoverageForClassWithAnonymousFunction(), self::$TEST_TMP_PATH); + + $this->assertFilesEquals($expectedFilesPath, self::$TEST_TMP_PATH); + } + + /** + * @param string $expectedFilesPath + * @param string $actualFilesPath + */ + protected function assertFilesEquals($expectedFilesPath, $actualFilesPath) + { + $expectedFilesIterator = new FilesystemIterator($expectedFilesPath); + $actualFilesIterator = new FilesystemIterator($actualFilesPath); + + $this->assertEquals( + iterator_count($expectedFilesIterator), + iterator_count($actualFilesIterator), + 'Generated files and expected files not match' + ); + foreach ($expectedFilesIterator as $path => $fileInfo) { + /* @var SplFileInfo $fileInfo */ + $filename = $fileInfo->getFilename(); + + $actualFile = $actualFilesPath . DIRECTORY_SEPARATOR . $filename; + + $this->assertFileExists($actualFile); + $this->assertStringMatchesFormatFile( + $fileInfo->getPathname(), + file_get_contents($actualFile), + "${filename} not match" + ); + } + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index f6bfb9d60..d03585cef 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -19,6 +19,13 @@ */ abstract class PHP_CodeCoverage_TestCase extends PHPUnit_Framework_TestCase { + static protected $TEST_TMP_PATH; + + public static function setUpBeforeClass() + { + self::$TEST_TMP_PATH = TEST_FILES_PATH . 'tmp'; + } + protected function getXdebugDataForBankAccount() { return [ diff --git a/tests/_files/Report/XML/CoverageForBankAccount/BankAccount.php.xml b/tests/_files/Report/XML/CoverageForBankAccount/BankAccount.php.xml new file mode 100644 index 000000000..d5c5d2e5f --- /dev/null +++ b/tests/_files/Report/XML/CoverageForBankAccount/BankAccount.php.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/_files/Report/XML/CoverageForBankAccount/index.xml b/tests/_files/Report/XML/CoverageForBankAccount/index.xml new file mode 100644 index 000000000..27fc5b405 --- /dev/null +++ b/tests/_files/Report/XML/CoverageForBankAccount/index.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/_files/Report/XML/CoverageForClassWithAnonymousFunction/index.xml b/tests/_files/Report/XML/CoverageForClassWithAnonymousFunction/index.xml new file mode 100644 index 000000000..6f9cd19c4 --- /dev/null +++ b/tests/_files/Report/XML/CoverageForClassWithAnonymousFunction/index.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/_files/Report/XML/CoverageForClassWithAnonymousFunction/source_with_class_and_anonymous_function.php.xml b/tests/_files/Report/XML/CoverageForClassWithAnonymousFunction/source_with_class_and_anonymous_function.php.xml new file mode 100644 index 000000000..d42452474 --- /dev/null +++ b/tests/_files/Report/XML/CoverageForClassWithAnonymousFunction/source_with_class_and_anonymous_function.php.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/_files/Report/XML/CoverageForFileWithIgnoredLines/index.xml b/tests/_files/Report/XML/CoverageForFileWithIgnoredLines/index.xml new file mode 100644 index 000000000..35c0745d5 --- /dev/null +++ b/tests/_files/Report/XML/CoverageForFileWithIgnoredLines/index.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/_files/Report/XML/CoverageForFileWithIgnoredLines/source_with_ignore.php.xml b/tests/_files/Report/XML/CoverageForFileWithIgnoredLines/source_with_ignore.php.xml new file mode 100644 index 000000000..509990f87 --- /dev/null +++ b/tests/_files/Report/XML/CoverageForFileWithIgnoredLines/source_with_ignore.php.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + From f86a4bf82a2d4f9a3907d899d232ab5be197663e Mon Sep 17 00:00:00 2001 From: Maks3w Date: Tue, 3 Nov 2015 16:15:54 +0100 Subject: [PATCH 3/9] Tests for HTML Report --- tests/PHP/CodeCoverage/Report/HTMLTest.php | 112 +++++++ .../BankAccount.php.html | 267 ++++++++++++++++ .../CoverageForBankAccount/dashboard.html | 290 ++++++++++++++++++ .../HTML/CoverageForBankAccount/index.html | 119 +++++++ .../dashboard.html | 288 +++++++++++++++++ .../index.html | 119 +++++++ ...with_class_and_anonymous_function.php.html | 211 +++++++++++++ .../dashboard.html | 286 +++++++++++++++++ .../index.html | 119 +++++++ .../source_with_ignore.php.html | 279 +++++++++++++++++ 10 files changed, 2090 insertions(+) create mode 100644 tests/PHP/CodeCoverage/Report/HTMLTest.php create mode 100644 tests/_files/Report/HTML/CoverageForBankAccount/BankAccount.php.html create mode 100644 tests/_files/Report/HTML/CoverageForBankAccount/dashboard.html create mode 100644 tests/_files/Report/HTML/CoverageForBankAccount/index.html create mode 100644 tests/_files/Report/HTML/CoverageForClassWithAnonymousFunction/dashboard.html create mode 100644 tests/_files/Report/HTML/CoverageForClassWithAnonymousFunction/index.html create mode 100644 tests/_files/Report/HTML/CoverageForClassWithAnonymousFunction/source_with_class_and_anonymous_function.php.html create mode 100644 tests/_files/Report/HTML/CoverageForFileWithIgnoredLines/dashboard.html create mode 100644 tests/_files/Report/HTML/CoverageForFileWithIgnoredLines/index.html create mode 100644 tests/_files/Report/HTML/CoverageForFileWithIgnoredLines/source_with_ignore.php.html diff --git a/tests/PHP/CodeCoverage/Report/HTMLTest.php b/tests/PHP/CodeCoverage/Report/HTMLTest.php new file mode 100644 index 000000000..a58dcddd2 --- /dev/null +++ b/tests/PHP/CodeCoverage/Report/HTMLTest.php @@ -0,0 +1,112 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +require_once dirname(dirname(dirname(dirname(__FILE__)))) . DIRECTORY_SEPARATOR . 'TestCase.php'; + +/** + * Tests for the PHP_CodeCoverage_Report_HTML class. + * + * @since Class available since Release 3.0.2 + */ +class PHP_CodeCoverage_Report_HTMLTest extends PHP_CodeCoverage_TestCase +{ + static private $TEST_REPORT_PATH_SOURCE; + + public static function setUpBeforeClass() + { + parent::setUpBeforeClass(); + + self::$TEST_REPORT_PATH_SOURCE = TEST_FILES_PATH . 'Report' . DIRECTORY_SEPARATOR . 'HTML'; + } + + protected function tearDown() + { + parent::tearDown(); + + $tmpFilesIterator = new RecursiveIteratorIterator( + new RecursiveDirectoryIterator(self::$TEST_TMP_PATH, RecursiveDirectoryIterator::SKIP_DOTS), + RecursiveIteratorIterator::CHILD_FIRST + ); + foreach ($tmpFilesIterator as $path => $fileInfo) { + /* @var SplFileInfo $fileInfo */ + $pathname = $fileInfo->getPathname(); + $fileInfo->isDir() ? rmdir($pathname) : unlink($pathname); + } + } + + /** + * @covers PHP_CodeCoverage_Report_HTML + */ + public function testForBankAccountTest() + { + $expectedFilesPath = self::$TEST_REPORT_PATH_SOURCE . DIRECTORY_SEPARATOR . 'CoverageForBankAccount'; + + $report = new PHP_CodeCoverage_Report_HTML; + $report->process($this->getCoverageForBankAccount(), self::$TEST_TMP_PATH); + + $this->assertFilesEquals($expectedFilesPath, self::$TEST_TMP_PATH); + } + + /** + * @covers PHP_CodeCoverage_Report_HTML + */ + public function testForFileWithIgnoredLines() + { + $expectedFilesPath = self::$TEST_REPORT_PATH_SOURCE . DIRECTORY_SEPARATOR . 'CoverageForFileWithIgnoredLines'; + + $report = new PHP_CodeCoverage_Report_HTML; + $report->process($this->getCoverageForFileWithIgnoredLines(), self::$TEST_TMP_PATH); + + $this->assertFilesEquals($expectedFilesPath, self::$TEST_TMP_PATH); + } + + /** + * @covers PHP_CodeCoverage_Report_HTML + */ + public function testForClassWithAnonymousFunction() + { + $expectedFilesPath = + self::$TEST_REPORT_PATH_SOURCE . DIRECTORY_SEPARATOR . 'CoverageForClassWithAnonymousFunction'; + + $report = new PHP_CodeCoverage_Report_HTML; + $report->process($this->getCoverageForClassWithAnonymousFunction(), self::$TEST_TMP_PATH); + + $this->assertFilesEquals($expectedFilesPath, self::$TEST_TMP_PATH); + } + + /** + * @param string $expectedFilesPath + * @param string $actualFilesPath + */ + protected function assertFilesEquals($expectedFilesPath, $actualFilesPath) + { + $expectedFilesIterator = new FilesystemIterator($expectedFilesPath); + $actualFilesIterator = new RegexIterator(new FilesystemIterator($actualFilesPath), '/.html/'); + + $this->assertEquals( + iterator_count($expectedFilesIterator), + iterator_count($actualFilesIterator), + 'Generated files and expected files not match' + ); + foreach ($expectedFilesIterator as $path => $fileInfo) { + /* @var SplFileInfo $fileInfo */ + $filename = $fileInfo->getFilename(); + + $actualFile = $actualFilesPath . DIRECTORY_SEPARATOR . $filename; + + $this->assertFileExists($actualFile); + $this->assertStringMatchesFormatFile( + $fileInfo->getPathname(), + str_replace(PHP_EOL, "\n", file_get_contents($actualFile)), + "${filename} not match" + ); + } + } +} diff --git a/tests/_files/Report/HTML/CoverageForBankAccount/BankAccount.php.html b/tests/_files/Report/HTML/CoverageForBankAccount/BankAccount.php.html new file mode 100644 index 000000000..a5dcd4c2b --- /dev/null +++ b/tests/_files/Report/HTML/CoverageForBankAccount/BankAccount.php.html @@ -0,0 +1,267 @@ + + + + + Code Coverage for %s/BankAccount.php + + + + + + +
+
+
+
+ +
+
+
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
 
Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
+
+ 0.00% covered (danger) +
+
+
0.00%
0 / 1
+
+ 75.00% covered (warning) +
+
+
75.00%
3 / 4
CRAP
+
+ 50.00% covered (danger) +
+
+
50.00%
5 / 10
BankAccount
+
+ 0.00% covered (danger) +
+
+
0.00%
0 / 1
+
+ 75.00% covered (warning) +
+
+
75.00%
3 / 4
8.12
+
+ 50.00% covered (danger) +
+
+
50.00%
5 / 10
 getBalance
+
+ 100.00% covered (success) +
+
+
100.00%
1 / 1
1
+
+ 100.00% covered (success) +
+
+
100.00%
1 / 1
 setBalance
+
+ 0.00% covered (danger) +
+
+
0.00%
0 / 1
6
+
+ 0.00% covered (danger) +
+
+
0.00%
0 / 5
 depositMoney
+
+ 100.00% covered (success) +
+
+
100.00%
1 / 1
1
+
+ 100.00% covered (success) +
+
+
100.00%
2 / 2
 withdrawMoney
+
+ 100.00% covered (success) +
+
+
100.00%
1 / 1
1
+
+ 100.00% covered (success) +
+
+
100.00%
2 / 2
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
<?php
class BankAccount
{
    protected $balance = 0;
    public function getBalance()
    {
        return $this->balance;
    }
    protected function setBalance($balance)
    {
        if ($balance >= 0) {
            $this->balance = $balance;
        } else {
            throw new RuntimeException;
        }
    }
    public function depositMoney($balance)
    {
        $this->setBalance($this->getBalance() + $balance);
        return $this->getBalance();
    }
    public function withdrawMoney($balance)
    {
        $this->setBalance($this->getBalance() - $balance);
        return $this->getBalance();
    }
}
+
+
+

Legend

+

+ Executed + Not Executed + Dead Code +

+

+ Generated by PHP_CodeCoverage %s using %s at %s. +

+ +
+
+ + + + + + diff --git a/tests/_files/Report/HTML/CoverageForBankAccount/dashboard.html b/tests/_files/Report/HTML/CoverageForBankAccount/dashboard.html new file mode 100644 index 000000000..651217a99 --- /dev/null +++ b/tests/_files/Report/HTML/CoverageForBankAccount/dashboard.html @@ -0,0 +1,290 @@ + + + + + Dashboard for %s + + + + + + + +
+
+
+
+ +
+
+
+
+
+
+
+

Classes

+
+
+
+
+

Coverage Distribution

+
+ +
+
+
+

Complexity

+
+ +
+
+
+
+
+

Insufficient Coverage

+
+ + + + + + + + + + + +
ClassCoverage
BankAccount50%
+
+
+
+

Project Risks

+
+ + + + + + + + + + + +
ClassCRAP
BankAccount8
+
+
+
+
+
+

Methods

+
+
+
+
+

Coverage Distribution

+
+ +
+
+
+

Complexity

+
+ +
+
+
+
+
+

Insufficient Coverage

+
+ + + + + + + + + + + +
MethodCoverage
setBalance0%
+
+
+
+

Project Risks

+
+ + + + + + + + + + + +
MethodCRAP
setBalance6
+
+
+
+ +
+ + + + + + + + diff --git a/tests/_files/Report/HTML/CoverageForBankAccount/index.html b/tests/_files/Report/HTML/CoverageForBankAccount/index.html new file mode 100644 index 000000000..5c99c2e42 --- /dev/null +++ b/tests/_files/Report/HTML/CoverageForBankAccount/index.html @@ -0,0 +1,119 @@ + + + + + Code Coverage for %s + + + + + + +
+
+
+
+ +
+
+
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
 
Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
+
+ 50.00% covered (danger) +
+
+
50.00%
5 / 10
+
+ 75.00% covered (warning) +
+
+
75.00%
3 / 4
+
+ 0.00% covered (danger) +
+
+
0.00%
0 / 1
BankAccount.php
+
+ 50.00% covered (danger) +
+
+
50.00%
5 / 10
+
+ 75.00% covered (warning) +
+
+
75.00%
3 / 4
+
+ 0.00% covered (danger) +
+
+
0.00%
0 / 1
+
+
+

Legend

+

+ Low: 0% to 50% + Medium: 50% to 90% + High: 90% to 100% +

+

+ Generated by PHP_CodeCoverage %s using %s at %s. +

+
+
+ + + + + diff --git a/tests/_files/Report/HTML/CoverageForClassWithAnonymousFunction/dashboard.html b/tests/_files/Report/HTML/CoverageForClassWithAnonymousFunction/dashboard.html new file mode 100644 index 000000000..229b1675a --- /dev/null +++ b/tests/_files/Report/HTML/CoverageForClassWithAnonymousFunction/dashboard.html @@ -0,0 +1,288 @@ + + + + + Dashboard for %s + + + + + + + +
+
+
+
+ +
+
+
+
+
+
+
+

Classes

+
+
+
+
+

Coverage Distribution

+
+ +
+
+
+

Complexity

+
+ +
+
+
+
+
+

Insufficient Coverage

+
+ + + + + + + + + + + +
ClassCoverage
CoveredClassWithAnonymousFunctionInStaticMethod87%
+
+
+
+

Project Risks

+
+ + + + + + + + + + +
ClassCRAP
+
+
+
+
+
+

Methods

+
+
+
+
+

Coverage Distribution

+
+ +
+
+
+

Complexity

+
+ +
+
+
+
+
+

Insufficient Coverage

+
+ + + + + + + + + + + +
MethodCoverage
runAnonymous66%
+
+
+
+

Project Risks

+
+ + + + + + + + + + +
MethodCRAP
+
+
+
+ +
+ + + + + + + + diff --git a/tests/_files/Report/HTML/CoverageForClassWithAnonymousFunction/index.html b/tests/_files/Report/HTML/CoverageForClassWithAnonymousFunction/index.html new file mode 100644 index 000000000..ace676be4 --- /dev/null +++ b/tests/_files/Report/HTML/CoverageForClassWithAnonymousFunction/index.html @@ -0,0 +1,119 @@ + + + + + Code Coverage for %s + + + + + + +
+
+
+
+ +
+
+
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
 
Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
+
+ 87.50% covered (warning) +
+
+
87.50%
7 / 8
+
+ 50.00% covered (danger) +
+
+
50.00%
1 / 2
+
+ 0.00% covered (danger) +
+
+
0.00%
0 / 1
source_with_class_and_anonymous_function.php
+
+ 87.50% covered (warning) +
+
+
87.50%
7 / 8
+
+ 50.00% covered (danger) +
+
+
50.00%
1 / 2
+
+ 0.00% covered (danger) +
+
+
0.00%
0 / 1
+
+
+

Legend

+

+ Low: 0% to 50% + Medium: 50% to 90% + High: 90% to 100% +

+

+ Generated by PHP_CodeCoverage %s using %s at %s. +

+
+
+ + + + + diff --git a/tests/_files/Report/HTML/CoverageForClassWithAnonymousFunction/source_with_class_and_anonymous_function.php.html b/tests/_files/Report/HTML/CoverageForClassWithAnonymousFunction/source_with_class_and_anonymous_function.php.html new file mode 100644 index 000000000..ce90096b9 --- /dev/null +++ b/tests/_files/Report/HTML/CoverageForClassWithAnonymousFunction/source_with_class_and_anonymous_function.php.html @@ -0,0 +1,211 @@ + + + + + Code Coverage for %s/source_with_class_and_anonymous_function.php + + + + + + +
+
+
+
+ +
+
+
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
 
Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
+
+ 0.00% covered (danger) +
+
+
0.00%
0 / 1
+
+ 50.00% covered (danger) +
+
+
50.00%
1 / 2
CRAP
+
+ 87.50% covered (warning) +
+
+
87.50%
7 / 8
CoveredClassWithAnonymousFunctionInStaticMethod
+
+ 0.00% covered (danger) +
+
+
0.00%
0 / 1
+
+ 50.00% covered (danger) +
+
+
50.00%
1 / 2
2.01
+
+ 87.50% covered (warning) +
+
+
87.50%
7 / 8
 runAnonymous
+
+ 0.00% covered (danger) +
+
+
0.00%
0 / 1
1.04
+
+ 66.67% covered (warning) +
+
+
66.67%
2 / 3
 anonymous function
+
+ 100.00% covered (success) +
+
+
100.00%
1 / 1
1
+
+ 100.00% covered (success) +
+
+
100.00%
2 / 2
+ + + + + + + + + + + + + + + + + + + + + + + +
<?php
class CoveredClassWithAnonymousFunctionInStaticMethod
{
    public static function runAnonymous()
    {
        $filter = ['abc124', 'abc123', '123'];
        array_walk(
            $filter,
            function (&$val, $key) {
                $val = preg_replace('|[^0-9]|', '', $val);
            }
        );
        // Should be covered
        $extravar = true;
    }
}
+
+
+

Legend

+

+ Executed + Not Executed + Dead Code +

+

+ Generated by PHP_CodeCoverage %s using %s at %s. +

+ +
+
+ + + + + + diff --git a/tests/_files/Report/HTML/CoverageForFileWithIgnoredLines/dashboard.html b/tests/_files/Report/HTML/CoverageForFileWithIgnoredLines/dashboard.html new file mode 100644 index 000000000..54deaeaba --- /dev/null +++ b/tests/_files/Report/HTML/CoverageForFileWithIgnoredLines/dashboard.html @@ -0,0 +1,286 @@ + + + + + Dashboard for %s + + + + + + + +
+
+
+
+ +
+
+
+
+
+
+
+

Classes

+
+
+
+
+

Coverage Distribution

+
+ +
+
+
+

Complexity

+
+ +
+
+
+
+
+

Insufficient Coverage

+
+ + + + + + + + + + +
ClassCoverage
+
+
+
+

Project Risks

+
+ + + + + + + + + + +
ClassCRAP
+
+
+
+
+
+

Methods

+
+
+
+
+

Coverage Distribution

+
+ +
+
+
+

Complexity

+
+ +
+
+
+
+
+

Insufficient Coverage

+
+ + + + + + + + + + +
MethodCoverage
+
+
+
+

Project Risks

+
+ + + + + + + + + + +
MethodCRAP
+
+
+
+ +
+ + + + + + + + diff --git a/tests/_files/Report/HTML/CoverageForFileWithIgnoredLines/index.html b/tests/_files/Report/HTML/CoverageForFileWithIgnoredLines/index.html new file mode 100644 index 000000000..2687444ff --- /dev/null +++ b/tests/_files/Report/HTML/CoverageForFileWithIgnoredLines/index.html @@ -0,0 +1,119 @@ + + + + + Code Coverage for %s + + + + + + +
+
+
+
+ +
+
+
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
 
Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
+
+ 50.00% covered (danger) +
+
+
50.00%
1 / 2
+
+ 100.00% covered (success) +
+
+
100.00%
0 / 0
+
+ 100.00% covered (success) +
+
+
100.00%
2 / 2
source_with_ignore.php
+
+ 50.00% covered (danger) +
+
+
50.00%
1 / 2
+
+ 100.00% covered (success) +
+
+
100.00%
0 / 0
+
+ 100.00% covered (success) +
+
+
100.00%
2 / 2
+
+
+

Legend

+

+ Low: 0% to 50% + Medium: 50% to 90% + High: 90% to 100% +

+

+ Generated by PHP_CodeCoverage %s using %s at %s. +

+
+
+ + + + + diff --git a/tests/_files/Report/HTML/CoverageForFileWithIgnoredLines/source_with_ignore.php.html b/tests/_files/Report/HTML/CoverageForFileWithIgnoredLines/source_with_ignore.php.html new file mode 100644 index 000000000..251b5c309 --- /dev/null +++ b/tests/_files/Report/HTML/CoverageForFileWithIgnoredLines/source_with_ignore.php.html @@ -0,0 +1,279 @@ + + + + + Code Coverage for %s/source_with_ignore.php + + + + + + +
+
+
+
+ +
+
+
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
 
Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
+
+ 100.00% covered (success) +
+
+
100.00%
2 / 2
+
+ 100.00% covered (success) +
+
+
100.00%
0 / 0
CRAP
+
+ 50.00% covered (danger) +
+
+
50.00%
1 / 2
baz
+
+ 100.00% covered (success) +
+
+
100.00%
1 / 1
0
+
+ 100.00% covered (success) +
+
+
100.00%
0 / 0
Foo
+
+ 100.00% covered (success) +
+
+
100.00%
1 / 1
+
+ 100.00% covered (success) +
+
+
100.00%
1 / 1
1
+
+ 100.00% covered (success) +
+
+
100.00%
0 / 0
 bar
+
+ 100.00% covered (success) +
+
+
100.00%
1 / 1
1
+
+ 100.00% covered (success) +
+
+
100.00%
0 / 0
Bar
+
+ 100.00% covered (success) +
+
+
100.00%
1 / 1
+
+ 100.00% covered (success) +
+
+
100.00%
1 / 1
1
+
+ 100.00% covered (success) +
+
+
100.00%
0 / 0
 foo
+
+ 100.00% covered (success) +
+
+
100.00%
1 / 1
1
+
+ 100.00% covered (success) +
+
+
100.00%
0 / 0
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
<?php
if ($neverHappens) {
    // @codeCoverageIgnoreStart
    print '*';
    // @codeCoverageIgnoreEnd
}
/**
 * @codeCoverageIgnore
 */
class Foo
{
    public function bar()
    {
    }
}
class Bar
{
    /**
     * @codeCoverageIgnore
     */
    public function foo()
    {
    }
}
function baz()
{
    print '*'; // @codeCoverageIgnore
}
interface Bor
{
    public function foo();
}
+
+
+

Legend

+

+ Executed + Not Executed + Dead Code +

+

+ Generated by PHP_CodeCoverage %s using %s at %s. +

+ +
+
+ + + + + + From 0f374e196106181fd619d804a5f5119f438ab045 Mon Sep 17 00:00:00 2001 From: Maks3w Date: Tue, 3 Nov 2015 11:37:41 +0100 Subject: [PATCH 4/9] Tests for Crap4j Report --- tests/PHP/CodeCoverage/Report/Crap4jTest.php | 58 ++++++++++++++++++ tests/_files/BankAccount-crap4j.xml | 59 +++++++++++++++++++ .../class-with-anonymous-function-crap4j.xml | 37 ++++++++++++ tests/_files/ignored-lines-crap4j.xml | 37 ++++++++++++ 4 files changed, 191 insertions(+) create mode 100644 tests/PHP/CodeCoverage/Report/Crap4jTest.php create mode 100644 tests/_files/BankAccount-crap4j.xml create mode 100644 tests/_files/class-with-anonymous-function-crap4j.xml create mode 100644 tests/_files/ignored-lines-crap4j.xml diff --git a/tests/PHP/CodeCoverage/Report/Crap4jTest.php b/tests/PHP/CodeCoverage/Report/Crap4jTest.php new file mode 100644 index 000000000..2822aaea5 --- /dev/null +++ b/tests/PHP/CodeCoverage/Report/Crap4jTest.php @@ -0,0 +1,58 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +require_once dirname(dirname(dirname(dirname(__FILE__)))) . DIRECTORY_SEPARATOR . 'TestCase.php'; + +/** + * Tests for the PHP_CodeCoverage_Report_Crap4j class. + * + * @since Class available since Release 3.0.2 + */ +class PHP_CodeCoverage_Report_Crap4jTest extends PHP_CodeCoverage_TestCase +{ + /** + * @covers PHP_CodeCoverage_Report_Crap4j + */ + public function testForBankAccountTest() + { + $crap4j = new PHP_CodeCoverage_Report_Crap4j; + + $this->assertStringMatchesFormatFile( + TEST_FILES_PATH . 'BankAccount-crap4j.xml', + $crap4j->process($this->getCoverageForBankAccount(), null, 'BankAccount') + ); + } + + /** + * @covers PHP_CodeCoverage_Report_Crap4j + */ + public function testForFileWithIgnoredLines() + { + $crap4j = new PHP_CodeCoverage_Report_Crap4j; + + $this->assertStringMatchesFormatFile( + TEST_FILES_PATH . 'ignored-lines-crap4j.xml', + $crap4j->process($this->getCoverageForFileWithIgnoredLines(), null, 'CoverageForFileWithIgnoredLines') + ); + } + + /** + * @covers PHP_CodeCoverage_Report_Crap4j + */ + public function testForClassWithAnonymousFunction() + { + $crap4j = new PHP_CodeCoverage_Report_Crap4j; + + $this->assertStringMatchesFormatFile( + TEST_FILES_PATH . 'class-with-anonymous-function-crap4j.xml', + $crap4j->process($this->getCoverageForClassWithAnonymousFunction(), null, 'CoverageForClassWithAnonymousFunction') + ); + } +} diff --git a/tests/_files/BankAccount-crap4j.xml b/tests/_files/BankAccount-crap4j.xml new file mode 100644 index 000000000..f2f56eabb --- /dev/null +++ b/tests/_files/BankAccount-crap4j.xml @@ -0,0 +1,59 @@ + + + BankAccount + %s + + Method Crap Stats + 4 + 0 + 0 + 9 + 0 + + + + global + BankAccount + getBalance + getBalance() + getBalance() + 1 + 1 + 100 + 0 + + + global + BankAccount + setBalance + setBalance($balance) + setBalance($balance) + 6 + 2 + 0 + 0 + + + global + BankAccount + depositMoney + depositMoney($balance) + depositMoney($balance) + 1 + 1 + 100 + 0 + + + global + BankAccount + withdrawMoney + withdrawMoney($balance) + withdrawMoney($balance) + 1 + 1 + 100 + 0 + + + diff --git a/tests/_files/class-with-anonymous-function-crap4j.xml b/tests/_files/class-with-anonymous-function-crap4j.xml new file mode 100644 index 000000000..696496dab --- /dev/null +++ b/tests/_files/class-with-anonymous-function-crap4j.xml @@ -0,0 +1,37 @@ + + + CoverageForClassWithAnonymousFunction + %s + + Method Crap Stats + 2 + 0 + 0 + 2.04 + 0 + + + + global + CoveredClassWithAnonymousFunctionInStaticMethod + runAnonymous + runAnonymous() + runAnonymous() + 1.04 + 1 + 66.67 + 0 + + + global + CoveredClassWithAnonymousFunctionInStaticMethod + anonymous function + anonymous function (&$val, $key) + anonymous function (&$val, $key) + 1 + 1 + 100 + 0 + + + diff --git a/tests/_files/ignored-lines-crap4j.xml b/tests/_files/ignored-lines-crap4j.xml new file mode 100644 index 000000000..2607b59ac --- /dev/null +++ b/tests/_files/ignored-lines-crap4j.xml @@ -0,0 +1,37 @@ + + + CoverageForFileWithIgnoredLines + %s + + Method Crap Stats + 2 + 0 + 0 + 2 + 0 + + + + global + Foo + bar + bar() + bar() + 1 + 1 + 100 + 0 + + + global + Bar + foo + foo() + foo() + 1 + 1 + 100 + 0 + + + From afa0dfb6a311a23b0ef4ccc6465e40d62aa0f9cb Mon Sep 17 00:00:00 2001 From: Maks3w Date: Tue, 3 Nov 2015 11:41:51 +0100 Subject: [PATCH 5/9] Typo --- src/CodeCoverage/Report/Text.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/CodeCoverage/Report/Text.php b/src/CodeCoverage/Report/Text.php index 174546fd8..d37a6d77c 100644 --- a/src/CodeCoverage/Report/Text.php +++ b/src/CodeCoverage/Report/Text.php @@ -223,9 +223,9 @@ protected function getCoverageColor($numberOfCoveredElements, $totalNumberOfElem return $this->colors['red']; } - protected function printCoverageCounts($numberOfCoveredElements, $totalNumberOfElements, $presicion) + protected function printCoverageCounts($numberOfCoveredElements, $totalNumberOfElements, $precision) { - $format = '%' . $presicion . 's'; + $format = '%' . $precision . 's'; return PHP_CodeCoverage_Util::percent( $numberOfCoveredElements, From e3efb1229bb6386edf4fda2fdcca2d81de8d2e33 Mon Sep 17 00:00:00 2001 From: Maks3w Date: Tue, 3 Nov 2015 18:07:16 +0100 Subject: [PATCH 6/9] Tests for Text Report --- tests/PHP/CodeCoverage/Report/TextTest.php | 58 +++++++++++++++++++ tests/_files/BankAccount-text.txt | 12 ++++ .../class-with-anonymous-function-text.txt | 12 ++++ tests/_files/ignored-lines-text.txt | 10 ++++ 4 files changed, 92 insertions(+) create mode 100644 tests/PHP/CodeCoverage/Report/TextTest.php create mode 100644 tests/_files/BankAccount-text.txt create mode 100644 tests/_files/class-with-anonymous-function-text.txt create mode 100644 tests/_files/ignored-lines-text.txt diff --git a/tests/PHP/CodeCoverage/Report/TextTest.php b/tests/PHP/CodeCoverage/Report/TextTest.php new file mode 100644 index 000000000..18e4125d8 --- /dev/null +++ b/tests/PHP/CodeCoverage/Report/TextTest.php @@ -0,0 +1,58 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +require_once dirname(dirname(dirname(dirname(__FILE__)))) . DIRECTORY_SEPARATOR . 'TestCase.php'; + +/** + * Tests for the PHP_CodeCoverage_Report_Text class. + * + * @since Class available since Release 3.0.2 + */ +class PHP_CodeCoverage_Report_TextTest extends PHP_CodeCoverage_TestCase +{ + /** + * @covers PHP_CodeCoverage_Report_Text + */ + public function testTextForBankAccountTest() + { + $text = new PHP_CodeCoverage_Report_Text(50, 90, false, false); + + $this->assertStringMatchesFormatFile( + TEST_FILES_PATH . 'BankAccount-text.txt', + str_replace(PHP_EOL, "\n", $text->process($this->getCoverageForBankAccount())) + ); + } + + /** + * @covers PHP_CodeCoverage_Report_Text + */ + public function testTextForFileWithIgnoredLines() + { + $text = new PHP_CodeCoverage_Report_Text(50, 90, false, false); + + $this->assertStringMatchesFormatFile( + TEST_FILES_PATH . 'ignored-lines-text.txt', + str_replace(PHP_EOL, "\n", $text->process($this->getCoverageForFileWithIgnoredLines())) + ); + } + + /** + * @covers PHP_CodeCoverage_Report_Text + */ + public function testTextForClassWithAnonymousFunction() + { + $text = new PHP_CodeCoverage_Report_Text(50, 90, false, false); + + $this->assertStringMatchesFormatFile( + TEST_FILES_PATH . 'class-with-anonymous-function-text.txt', + str_replace(PHP_EOL, "\n", $text->process($this->getCoverageForClassWithAnonymousFunction())) + ); + } +} diff --git a/tests/_files/BankAccount-text.txt b/tests/_files/BankAccount-text.txt new file mode 100644 index 000000000..892d83464 --- /dev/null +++ b/tests/_files/BankAccount-text.txt @@ -0,0 +1,12 @@ + + +Code Coverage Report: + %s + + Summary: + Classes: 0.00% (0/1) + Methods: 75.00% (3/4) + Lines: 50.00% (5/10) + +BankAccount + Methods: 75.00% ( 3/ 4) Lines: 50.00% ( 5/ 10) diff --git a/tests/_files/class-with-anonymous-function-text.txt b/tests/_files/class-with-anonymous-function-text.txt new file mode 100644 index 000000000..0eb257e5f --- /dev/null +++ b/tests/_files/class-with-anonymous-function-text.txt @@ -0,0 +1,12 @@ + + +Code Coverage Report: + %s + + Summary: + Classes: 0.00% (0/1) + Methods: 50.00% (1/2) + Lines: 87.50% (7/8) + +CoveredClassWithAnonymousFunctionInStaticMethod + Methods: 50.00% ( 1/ 2) Lines: 80.00% ( 4/ 5) diff --git a/tests/_files/ignored-lines-text.txt b/tests/_files/ignored-lines-text.txt new file mode 100644 index 000000000..428303873 --- /dev/null +++ b/tests/_files/ignored-lines-text.txt @@ -0,0 +1,10 @@ + + +Code Coverage Report: + %s + + Summary: + Classes: 100.00% (2/2) + Methods: (0/0) + Lines: 50.00% (1/2) + From d881cf28fa2713936887ac38d66897aaecba0309 Mon Sep 17 00:00:00 2001 From: Maks3w Date: Wed, 4 Nov 2015 12:42:18 +0100 Subject: [PATCH 7/9] DRY Method definition --- src/CodeCoverage/Report/Node/File.php | 49 ++++++++++++++------------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/src/CodeCoverage/Report/Node/File.php b/src/CodeCoverage/Report/Node/File.php index 538fb6754..5b837cf75 100644 --- a/src/CodeCoverage/Report/Node/File.php +++ b/src/CodeCoverage/Report/Node/File.php @@ -558,18 +558,7 @@ protected function processClasses(PHP_Token_Stream $tokens) $this->endLines[$class['endLine']] = &$this->classes[$className]; foreach ($class['methods'] as $methodName => $method) { - $this->classes[$className]['methods'][$methodName] = [ - 'methodName' => $methodName, - 'signature' => $method['signature'], - 'startLine' => $method['startLine'], - 'endLine' => $method['endLine'], - 'executableLines' => 0, - 'executedLines' => 0, - 'ccn' => $method['ccn'], - 'coverage' => 0, - 'crap' => 0, - 'link' => $link . $method['startLine'] - ]; + $this->classes[$className]['methods'][$methodName] = $this->newMethod($methodName, $method, $link); $this->startLines[$method['startLine']] = &$this->classes[$className]['methods'][$methodName]; $this->endLines[$method['endLine']] = &$this->classes[$className]['methods'][$methodName]; @@ -605,18 +594,7 @@ protected function processTraits(PHP_Token_Stream $tokens) $this->endLines[$trait['endLine']] = &$this->traits[$traitName]; foreach ($trait['methods'] as $methodName => $method) { - $this->traits[$traitName]['methods'][$methodName] = [ - 'methodName' => $methodName, - 'signature' => $method['signature'], - 'startLine' => $method['startLine'], - 'endLine' => $method['endLine'], - 'executableLines' => 0, - 'executedLines' => 0, - 'ccn' => $method['ccn'], - 'coverage' => 0, - 'crap' => 0, - 'link' => $link . $method['startLine'] - ]; + $this->traits[$traitName]['methods'][$methodName] = $this->newMethod($methodName, $method, $link); $this->startLines[$method['startLine']] = &$this->traits[$traitName]['methods'][$methodName]; $this->endLines[$method['endLine']] = &$this->traits[$traitName]['methods'][$methodName]; @@ -676,4 +654,27 @@ protected function crap($ccn, $coverage) pow($ccn, 2) * pow(1 - $coverage/100, 3) + $ccn ); } + + /** + * @param string $methodName + * @param array $method + * @param string $link + * + * @return array + */ + private function newMethod($methodName, array $method, $link) + { + return [ + 'methodName' => $methodName, + 'signature' => $method['signature'], + 'startLine' => $method['startLine'], + 'endLine' => $method['endLine'], + 'executableLines' => 0, + 'executedLines' => 0, + 'ccn' => $method['ccn'], + 'coverage' => 0, + 'crap' => 0, + 'link' => $link . $method['startLine'] + ]; + } } From f521e2002a54bdb5008cadec486cfacc30dded57 Mon Sep 17 00:00:00 2001 From: Maks3w Date: Wed, 4 Nov 2015 12:53:44 +0100 Subject: [PATCH 8/9] Add method visibility to Clover report --- src/CodeCoverage/Report/Clover.php | 13 ++++++++---- src/CodeCoverage/Report/Node/File.php | 21 ++++++++++--------- tests/PHP/CodeCoverage/Report/FactoryTest.php | 12 +++++++---- tests/_files/BankAccount-clover.xml | 8 +++---- .../class-with-anonymous-function-clover.xml | 2 +- 5 files changed, 33 insertions(+), 23 deletions(-) diff --git a/src/CodeCoverage/Report/Clover.php b/src/CodeCoverage/Report/Clover.php index aed52d71d..ec16d6fb0 100644 --- a/src/CodeCoverage/Report/Clover.php +++ b/src/CodeCoverage/Report/Clover.php @@ -85,10 +85,11 @@ public function process(PHP_CodeCoverage $coverage, $target = null, $name = null } $lines[$method['startLine']] = [ - 'count' => $methodCount, - 'crap' => $method['crap'], - 'type' => 'method', - 'name' => $methodName + 'count' => $methodCount, + 'crap' => $method['crap'], + 'type' => 'method', + 'visibility' => $method['visibility'], + 'name' => $methodName ]; } @@ -176,6 +177,10 @@ public function process(PHP_CodeCoverage $coverage, $target = null, $name = null $xmlLine->setAttribute('name', $data['name']); } + if (isset($data['visibility'])) { + $xmlLine->setAttribute('visibility', $data['visibility']); + } + if (isset($data['crap'])) { $xmlLine->setAttribute('crap', $data['crap']); } diff --git a/src/CodeCoverage/Report/Node/File.php b/src/CodeCoverage/Report/Node/File.php index 5b837cf75..f5b2ffb48 100644 --- a/src/CodeCoverage/Report/Node/File.php +++ b/src/CodeCoverage/Report/Node/File.php @@ -657,7 +657,7 @@ protected function crap($ccn, $coverage) /** * @param string $methodName - * @param array $method + * @param array $method * @param string $link * * @return array @@ -665,16 +665,17 @@ protected function crap($ccn, $coverage) private function newMethod($methodName, array $method, $link) { return [ - 'methodName' => $methodName, - 'signature' => $method['signature'], - 'startLine' => $method['startLine'], - 'endLine' => $method['endLine'], + 'methodName' => $methodName, + 'visibility' => $method['visibility'], + 'signature' => $method['signature'], + 'startLine' => $method['startLine'], + 'endLine' => $method['endLine'], 'executableLines' => 0, - 'executedLines' => 0, - 'ccn' => $method['ccn'], - 'coverage' => 0, - 'crap' => 0, - 'link' => $link . $method['startLine'] + 'executedLines' => 0, + 'ccn' => $method['ccn'], + 'coverage' => 0, + 'crap' => 0, + 'link' => $link . $method['startLine'], ]; } } diff --git a/tests/PHP/CodeCoverage/Report/FactoryTest.php b/tests/PHP/CodeCoverage/Report/FactoryTest.php index 43616095d..9d1aeba60 100644 --- a/tests/PHP/CodeCoverage/Report/FactoryTest.php +++ b/tests/PHP/CodeCoverage/Report/FactoryTest.php @@ -61,7 +61,8 @@ public function testSomething() 'coverage' => 100, 'crap' => '1', 'link' => 'BankAccount.php.html#6', - 'methodName' => 'getBalance' + 'methodName' => 'getBalance', + 'visibility' => 'public', ], 'setBalance' => [ 'signature' => 'setBalance($balance)', @@ -73,7 +74,8 @@ public function testSomething() 'coverage' => 0, 'crap' => 6, 'link' => 'BankAccount.php.html#11', - 'methodName' => 'setBalance' + 'methodName' => 'setBalance', + 'visibility' => 'protected', ], 'depositMoney' => [ 'signature' => 'depositMoney($balance)', @@ -85,7 +87,8 @@ public function testSomething() 'coverage' => 100, 'crap' => '1', 'link' => 'BankAccount.php.html#20', - 'methodName' => 'depositMoney' + 'methodName' => 'depositMoney', + 'visibility' => 'public', ], 'withdrawMoney' => [ 'signature' => 'withdrawMoney($balance)', @@ -97,7 +100,8 @@ public function testSomething() 'coverage' => 100, 'crap' => '1', 'link' => 'BankAccount.php.html#27', - 'methodName' => 'withdrawMoney' + 'methodName' => 'withdrawMoney', + 'visibility' => 'public', ], ], 'startLine' => 2, diff --git a/tests/_files/BankAccount-clover.xml b/tests/_files/BankAccount-clover.xml index 578a7ccc1..a2abc9752 100644 --- a/tests/_files/BankAccount-clover.xml +++ b/tests/_files/BankAccount-clover.xml @@ -5,18 +5,18 @@ - + - + - + - + diff --git a/tests/_files/class-with-anonymous-function-clover.xml b/tests/_files/class-with-anonymous-function-clover.xml index ac43b8061..45668eb2e 100644 --- a/tests/_files/class-with-anonymous-function-clover.xml +++ b/tests/_files/class-with-anonymous-function-clover.xml @@ -5,7 +5,7 @@ - + From c8a0477dc13cb20e491bb48905116e7e1edc1ffa Mon Sep 17 00:00:00 2001 From: Maks3w Date: Wed, 4 Nov 2015 16:46:39 +0100 Subject: [PATCH 9/9] Add cyclomatic complexity to Clover report --- src/CodeCoverage/Report/Clover.php | 6 ++++++ tests/_files/BankAccount-clover.xml | 10 +++++----- tests/_files/class-with-anonymous-function-clover.xml | 6 +++--- tests/_files/ignored-lines-clover.xml | 4 ++-- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/CodeCoverage/Report/Clover.php b/src/CodeCoverage/Report/Clover.php index ec16d6fb0..0d56f9ad3 100644 --- a/src/CodeCoverage/Report/Clover.php +++ b/src/CodeCoverage/Report/Clover.php @@ -85,6 +85,7 @@ public function process(PHP_CodeCoverage $coverage, $target = null, $name = null } $lines[$method['startLine']] = [ + 'ccn' => $method['ccn'], 'count' => $methodCount, 'crap' => $method['crap'], 'type' => 'method', @@ -132,6 +133,7 @@ public function process(PHP_CodeCoverage $coverage, $target = null, $name = null $xmlFile->appendChild($xmlClass); $xmlMetrics = $xmlDocument->createElement('metrics'); + $xmlMetrics->setAttribute('complexity', $class['ccn']); $xmlMetrics->setAttribute('methods', $classMethods); $xmlMetrics->setAttribute('coveredmethods', $coveredMethods); $xmlMetrics->setAttribute('conditionals', 0); @@ -181,6 +183,10 @@ public function process(PHP_CodeCoverage $coverage, $target = null, $name = null $xmlLine->setAttribute('visibility', $data['visibility']); } + if (isset($data['ccn'])) { + $xmlLine->setAttribute('complexity', $data['ccn']); + } + if (isset($data['crap'])) { $xmlLine->setAttribute('crap', $data['crap']); } diff --git a/tests/_files/BankAccount-clover.xml b/tests/_files/BankAccount-clover.xml index a2abc9752..0986fdf71 100644 --- a/tests/_files/BankAccount-clover.xml +++ b/tests/_files/BankAccount-clover.xml @@ -3,20 +3,20 @@ - + - + - + - + - + diff --git a/tests/_files/class-with-anonymous-function-clover.xml b/tests/_files/class-with-anonymous-function-clover.xml index 45668eb2e..d6a8b4085 100644 --- a/tests/_files/class-with-anonymous-function-clover.xml +++ b/tests/_files/class-with-anonymous-function-clover.xml @@ -3,13 +3,13 @@ - + - + - + diff --git a/tests/_files/ignored-lines-clover.xml b/tests/_files/ignored-lines-clover.xml index cda929cd3..81a9aaa3d 100644 --- a/tests/_files/ignored-lines-clover.xml +++ b/tests/_files/ignored-lines-clover.xml @@ -3,10 +3,10 @@ - + - +